refac(haskel): clean solution

This commit is contained in:
Dmitriy Pleshevskiy 2022-04-19 22:43:15 +03:00
parent 11b969d169
commit a7f05aa3c7
1 changed files with 11 additions and 12 deletions

View File

@ -3,24 +3,23 @@ module TitleCase (titleCase) where
import qualified Data.Char as Char
titleCase :: String -> String -> String
titleCase minor title =
unwords $
map toTitle (take 1 titleWords)
<> map maybeToTitle (drop 1 titleWords)
titleCase minor = unwords . process . words . toLower
where
titleWords, minorWords :: [String]
titleWords = words . toLower $ title
minorWords = words . toLower $ minor
process :: [String] -> [String]
process [] = []
process (head:tail) = toTitle head : map maybeToTitle tail
lowerMinorWords :: [String]
lowerMinorWords = words $ toLower minor
maybeToTitle :: String -> String
maybeToTitle word
| word `elem` minorWords = word
| otherwise = toTitle word
| word `elem` lowerMinorWords = word
| otherwise = toTitle word
toTitle :: String -> String
toTitle t = case t of
"" -> ""
(x:xs) -> Char.toUpper x : xs
toTitle [] = []
toTitle (head:tail) = Char.toUpper head : toLower tail
toLower :: String -> String
toLower = map Char.toLower