Avatar of paulfioravanti

paulfioravanti's solution

to Bob in the Elm Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

Elm Installation

Refer to the Exercism help page for Elm installation and learning resources.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

$ elm-package install --yes

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

Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (..)

import Bob
import Char
import Expect
import Random
import String
import Test exposing (..)


tests : Test
tests =
    describe "Bob"
        [ test "stating something" <|
            \() ->
                Expect.equal "Whatever."
                    (Bob.hey "Tom-ay-to, tom-aaaah-to.")
        , skip <|
            test "shouting" <|
                \() ->
                    Expect.equal
                        "Whoa, chill out!"
                        (Bob.hey "WATCH OUT!")
        , skip <|
            test "shouting gibberish" <|
                \() ->
                    Expect.equal
                        "Whoa, chill out!"
                        (Bob.hey (uppercaseGibberish 10))
        , skip <|
            test "asking a question" <|
                \() ->
                    Expect.equal
                        "Sure."
                        (Bob.hey "Does this cryogenic chamber make me look fat?")
        , skip <|
            test "asking a numeric question" <|
                \() ->
                    Expect.equal
                        "Sure."
                        (Bob.hey "You are, what, like 15?")
        , skip <|
            test "asking gibberish" <|
                \() ->
                    Expect.equal
                        "Sure."
                        (Bob.hey (gibberishQuestion 20))
        , skip <|
            test "talking forcefully" <|
                \() ->
                    Expect.equal
                        "Whatever."
                        (Bob.hey "Let's go make out behind the gym!")
        , skip <|
            test "using acronyms in regular speech" <|
                \() ->
                    Expect.equal
                        "Whatever."
                        (Bob.hey "It's OK if you don't want to go to the DMV.")
        , skip <|
            test "forceful questions" <|
                \() ->
                    Expect.equal
                        "Calm down, I know what I'm doing!"
                        (Bob.hey "WHAT THE HELL WERE YOU THINKING?")
        , skip <|
            test "shouting numbers" <|
                \() ->
                    Expect.equal
                        "Whoa, chill out!"
                        (Bob.hey "1, 2, 3 GO!")
        , skip <|
            test "only numbers" <|
                \() ->
                    Expect.equal
                        "Whatever."
                        (Bob.hey "1, 2, 3")
        , skip <|
            test "question with only numbers" <|
                \() ->
                    Expect.equal
                        "Sure."
                        (Bob.hey "4?")
        , skip <|
            test "shouting with special characters" <|
                \() ->
                    Expect.equal
                        "Whoa, chill out!"
                        (Bob.hey "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!")
        , skip <|
            test "shouting with no exclamation mark" <|
                \() ->
                    Expect.equal
                        "Whoa, chill out!"
                        (Bob.hey "I HATE YOU")
        , skip <|
            test "statement containing a question mark" <|
                \() ->
                    Expect.equal
                        "Whatever."
                        (Bob.hey "Ending with ? means a question.")
        , skip <|
            test "prattling on" <|
                \() ->
                    Expect.equal
                        "Sure."
                        (Bob.hey "Wait! Hang on. Are you going to be OK?")
        , skip <|
            test "silence" <|
                \() ->
                    Expect.equal
                        "Fine. Be that way!"
                        (Bob.hey "")
        , skip <|
            test "prolonged silence" <|
                \() ->
                    Expect.equal
                        "Fine. Be that way!"
                        (Bob.hey "       ")
        , skip <|
            test "alternate silences" <|
                \() ->
                    Expect.equal
                        "Fine. Be that way!"
                        (Bob.hey "\t  \n  \t   ")
        , skip <|
            test "on multiple line questions" <|
                \() ->
                    Expect.equal
                        "Whatever."
                        (Bob.hey "\nDoes this cryogenic chamber make me look fat?\nno")
        ]


character : Int -> Int -> Random.Generator Char
character start end =
    Random.map Char.fromCode (Random.int start end)


anyCharacter : Random.Generator Char
anyCharacter =
    character 32 126


uppercaseCharacter : Random.Generator Char
uppercaseCharacter =
    character 65 90


listOfCharacters : Int -> Random.Generator Char -> Random.Generator (List Char)
listOfCharacters length characterList =
    Random.list length characterList


gibberish : Int -> Random.Generator Char -> String
gibberish length characterList =
    Tuple.first (Random.step (Random.map String.fromList (listOfCharacters length characterList)) (Random.initialSeed 424242))


uppercaseGibberish : Int -> String
uppercaseGibberish length =
    gibberish length uppercaseCharacter


gibberishQuestion : Int -> String
gibberishQuestion length =
    gibberish length anyCharacter ++ "?"

elm-package.json

{
    "version": "3.0.0",
    "summary": "Exercism problems in Elm.",
    "repository": "https://github.com/exercism/elm.git",
    "license": "BSD3",
    "source-directories": [
        ".",
        ".."
    ],
    "exposed-modules": [],
    "dependencies": {
        "elm-lang/core": "5.0.0 <= v < 6.0.0",
        "elm-community/elm-test": "4.0.0 <= v < 5.0.0"
    },
    "elm-version": "0.18.0 <= v < 0.19.0"
}
module Bob exposing (hey)

import Regex exposing (regex)


hey : String -> String
hey message =
    if silence message then
        "Fine. Be that way!"
    else if isShouting message then
        "Whoa, chill out!"
    else if isQuestion message then
        "Sure."
    else
        "Whatever."


silence : String -> Bool
silence message =
    String.trim message == ""


isShouting : String -> Bool
isShouting message =
    not (onlyDigitsAndNonWords message)
        && String.toUpper message
        == message


onlyDigitsAndNonWords : String -> Bool
onlyDigitsAndNonWords message =
    Regex.contains (regex "^([0-9]|[^a-zA-Z])+$") message


isQuestion : String -> Bool
isQuestion message =
    String.endsWith "?" message

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?