-- Support for common text operations in languages with Latin script. module Util.Text ( capitalize, join_text, join_text2, join_spaced ) where import Data.Char import Data.List -- Capitalize the first letter of a string. capitalize :: String -> String capitalize "" = "" capitalize (c : cs) = (toUpper c : cs) -- Concatenate a list of strings, filtering out the empty strings, and -- insert the delimiter d between the strings. join_text :: String -> [String] -> String join_text d = concat . intersperse d . filter (not . null) -- Concatenate a list of strings, filtering out the empty strings, and -- insert the delimiter d between all strings except the last pair, -- which is separated with the delimiter lastd. join_text2 :: String -> String -> [String] -> String join_text2 d lastd = concat . intersperse2 d lastd . filter (not . null) join_spaced :: [String] -> String join_spaced = join_text " " -- Just like intersperse, but treat the last two elements of the -- list specially: insert lastd instead of d between those. intersperse2 :: a -> a -> [a] -> [a] intersperse2 _ _ [] = [] intersperse2 _ _ [s] = [s] intersperse2 _ lastd [s, s'] = [s, lastd, s'] intersperse2 d lastd (s : ss) = s : d : intersperse2 d lastd ss