# paulfioravanti's solution

## to Word Count in the Elm Track

Published at Jul 06 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Given a phrase, count the occurrences of each word in that phrase.

For example for the input `"olly olly in come free"`

``````olly: 2
in: 1
come: 1
free: 1
``````

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

## Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (tests)

import Dict exposing (Dict)
import Expect
import Test exposing (..)
import WordCount exposing (wordCount)

tests : Test
tests =
describe "Word Count"
[ test "count one word" <|
\() ->
Expect.equal [ ( "word", 1 ) ]
(wordCount "word" |> Dict.toList)
, skip <|
test "count one of each word" <|
\() ->
Expect.equal [ ( "each", 1 ), ( "of", 1 ), ( "one", 1 ) ]
(wordCount "one of each" |> Dict.toList)
, skip <|
test "multiple occurrences of a word" <|
\() ->
Expect.equal [ ( "blue", 1 ), ( "fish", 4 ), ( "one", 1 ), ( "red", 1 ), ( "two", 1 ) ]
(wordCount "one fish two fish red fish blue fish" |> Dict.toList)
, skip <|
test "ignore punctuation" <|
\() ->
Expect.equal [ ( "as", 1 ), ( "car", 1 ), ( "carpet", 1 ), ( "java", 1 ), ( "javascript", 1 ) ]
(wordCount "car : carpet as java : javascript!!&@\$%^&" |> Dict.toList)
, skip <|
test "include numbers" <|
\() ->
Expect.equal [ ( "1", 1 ), ( "2", 1 ), ( "testing", 2 ) ]
(wordCount "testing, 1, 2 testing" |> Dict.toList)
, skip <|
test "normalize case" <|
\() ->
Expect.equal [ ( "go", 3 ), ( "stop", 2 ) ]
(wordCount "go Go GO Stop stop" |> Dict.toList)
]``````
``````module WordCount exposing (wordCount)

import Dict exposing (Dict)

wordCount : String -> Dict String Int
wordCount sentence =
sentence
|> convertToWords
|> List.foldl incrementTallyForWord Dict.empty

-- PRIVATE

convertToWords : String -> List String
convertToWords sentence =
let
alphaNumOrSpaceOnly : Char -> Bool
alphaNumOrSpaceOnly char =
Char.isAlphaNum char || char == ' '
in
sentence
|> String.toLower
|> String.filter alphaNumOrSpaceOnly
|> String.words

incrementTallyForWord : String -> Dict String Int -> Dict String Int
incrementTallyForWord word acc =
let
incrementTally : Maybe Int -> Maybe Int
incrementTally tally =
case tally of
Just count ->
Just (count + 1)

Nothing ->
Just 1
in
Dict.update word incrementTally acc``````