elm/exercises/word-count/WordCount.example

25 lines
490 B
Text
Raw Normal View History

2016-03-12 11:34:35 +00:00
module WordCount (..) where
import String
import Dict exposing (Dict)
import Regex
wordCount : String -> Dict String Int
wordCount sentence =
sentence
|> String.toLower
|> depunctuate
|> String.words
|> List.foldl (\w d -> Dict.update w incrMaybe d) Dict.empty
depunctuate : String -> String
depunctuate =
Regex.replace Regex.All (Regex.regex "[^a-z0-9 ]") (\_ -> "")
incrMaybe : Maybe Int -> Maybe Int
incrMaybe maybe =
(Maybe.withDefault 0 maybe) + 1 |> Just