packages feed

ta-0.1: Database/TA/Helper/Text.hs

{- |
      Rutinas auxiliares.

-}

module Database.TA.Helper.Text
    ( proper
    , palabras
    )

where

import Data.Char   ( toLower, toUpper, isSpace )
import Data.List   ( mapAccumL ) 


-- | Función tomada de internet...

 
proper :: String -> String
proper = unwords . map capitalize . words
  where capitalize (x:xs) = toUpper x : map toLower xs
                                  


{- | 
       'palabras' divide una hilera en una lista
        de palabras y espacios en blanco.
       La división ocurre cuando se encuentra con algún
       caracter que representa espacio.
       Los espacios se conservan. 
       A diferencia con words:
           donde  (words x) /= concat (words x)
       'palabras' cumple con
                  (palabras x) == concat (palabras x)
-}
palabras s =  
    let (acc,l) = mapAccumL dividir [] s 
     in filter (not . null) $ l ++ [(reverse acc)]
  where
    nosp = not . isSpace
    dividir acc x 
        | null acc                         =  (x:acc, []   )
        | isSpace (head acc) && isSpace x  =  (x:acc, []   )
        | isSpace (head acc) && nosp x     =  ([x]  , reverse acc)
        | nosp    (head acc) && isSpace x  =  ([x]  , reverse acc)
        | otherwise                        =  (x:acc, []   ) 
                                  
-- eof