Last active
August 29, 2015 14:10
-
-
Save kjlape/9aa3bf1d2e2c51b262f0 to your computer and use it in GitHub Desktop.
Chicken utilities for Haskell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import qualified Data.Char as Char | |
| import Data.List | |
| capitalized :: String -> String | |
| capitalized "" = "" | |
| capitalized (head:tail) = Char.toUpper head : tail | |
| trimmer :: ([a] -> a) -> ([a] -> [a]) -> (a -> Bool) -> [a] -> [a] | |
| trimmer _ _ _ [] = [] | |
| trimmer next rest pred l | |
| | length r == 0 = if pred n then r else l | |
| | not (pred n) = l | |
| | otherwise = trimmer next rest pred r | |
| where | |
| n = next l | |
| r = rest l | |
| spaceTrimmer next rest str = trimmer next rest Char.isSpace str | |
| ltrim :: String -> String | |
| ltrim = spaceTrimmer head tail | |
| rtrim :: String -> String | |
| rtrim = spaceTrimmer last init | |
| trim :: String -> String | |
| trim = ltrim . rtrim | |
| squasherRecursive :: (a -> a-> a -> Bool) -> a -> [a] -> [a] | |
| squasherRecursive _ _ [] = [] | |
| squasherRecursive pred before lst@(cur:rest) | |
| | length rest == 0 = lst | |
| | pred before cur (head rest) = squasherRecursive pred before rest | |
| | otherwise = cur:(squasherRecursive pred cur rest) | |
| shouldSquashChar pred before current after = pred current && (pred before || pred after) | |
| shouldSquashContiguousChars b c a = b == c || c == a | |
| squasher :: (a -> a -> a -> Bool) -> [a] -> [a] | |
| squasher _ [] = [] | |
| squasher pred (h:t) = h:(squasherRecursive pred h t) | |
| squashSpace = squasher $ shouldSquashChar Char.isSpace | |
| squash :: Eq a => [a] -> [a] | |
| squash = squasher shouldSquashContiguousChars | |
| times :: Int -> a -> [a] | |
| times num x = take num (repeat x) | |
| mapLast :: (a -> a) -> [a] -> [a] | |
| mapLast _ [] = [] | |
| mapLast func lst = init lst ++ [func $ last lst] | |
| listConjunction :: Bool -> String -> [String] -> String | |
| listConjunction _ _ [] = "" | |
| listConjunction hasSerialComma coordConjunction l | |
| | len == 1 = head filteredList | |
| | len == 2 = unwords $ intersperse coordConjunction filteredList | |
| | otherwise = concat | |
| $ intercalate [", "] | |
| [(intersperse ", " (fst split)), | |
| (intersperse " " $ mapLast (\x -> coordConjunction ++ " " ++ x) (snd split))] | |
| where | |
| filteredList = filter (/= "") $ map (squashSpace . trim) l | |
| len = length filteredList | |
| split = splitAt (len - if hasSerialComma then 1 else 2) filteredList | |
| listConjunctionOxford = listConjunction True | |
| listConjunctionAPA = listConjunction False | |
| sentencify :: String -> String | |
| sentencify "" = "" | |
| sentencify s = capitalized s ++ "." | |
| chickenSentence :: Int -> String | |
| chickenSentence numWords = sentencify $ listConjunctionOxford "and" $ numWords `times` "chicken" | |
| chickenSentences :: Int -> Int -> String | |
| chickenSentences 0 _ = [] | |
| chickenSentences numWords numSentences = unwords | |
| $ times numSentences | |
| $ chickenSentence numWords |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment