Last active
October 1, 2020 22:54
-
-
Save TechnoTone/d0c234b6b5613e74325304ec6535f216 to your computer and use it in GitHub Desktop.
Exercism Bob.elm
This file contains 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
module Bob exposing (hey) | |
import Parser as P exposing (..) | |
type Remark | |
= Question | |
| Shouting | |
| ShoutingQuestion | |
| Empty | |
| Default | |
hey : String -> String | |
hey remark = | |
case P.run parsed remark of | |
Ok Question -> | |
"Sure." | |
Ok Shouting -> | |
"Whoa, chill out!" | |
Ok ShoutingQuestion -> | |
"Calm down, I know what I'm doing!" | |
Ok Empty -> | |
"Fine. Be that way!" | |
Ok Default -> | |
"Whatever." | |
Err error -> | |
let | |
_ = | |
Debug.log "argh!" error | |
in | |
"error!" | |
parsed : P.Parser Remark | |
parsed = | |
P.oneOf | |
[ parseEmpty | |
, parseShoutingQuestion | |
, parseQuestion | |
, parseShouting | |
, parseDefault | |
] | |
parseEmpty : Parser Remark | |
parseEmpty = | |
P.backtrackable <| | |
P.map (always Empty) <| | |
P.chompWhile (\c -> List.member c (String.toList "\n\u{000D} \t")) | |
|. P.end | |
parseShoutingQuestion : Parser Remark | |
parseShoutingQuestion = | |
P.backtrackable <| | |
P.map (always ShoutingQuestion) <| | |
parseShouting | |
|. P.symbol "?" | |
|. P.end | |
parseQuestion : Parser Remark | |
parseQuestion = | |
P.backtrackable <| | |
P.map (always Question) <| | |
P.chompWhile ((==) '?' >> not) | |
|. P.symbol "?" | |
|. whitespace | |
|. P.end | |
whitespace : Parser () | |
whitespace = | |
chompWhile (\c -> c == ' ' || c == '\t' || c == '\n' || c == '\u{000D}') | |
parseShouting : Parser Remark | |
parseShouting = | |
P.backtrackable <| | |
(P.chompWhile ((/=) '?') | |
|> P.getChompedString | |
|> P.andThen isShouting | |
|> P.map (always Shouting) | |
) | |
isShouting : String -> Parser () | |
isShouting s = | |
if String.isEmpty s then | |
P.problem "no input" | |
else if stringHas Char.isLower s then | |
P.problem "has lowercase letter" | |
else if stringHas Char.isUpper s then | |
P.succeed () | |
else | |
P.problem "has no uppercase letters" | |
stringHas : (Char -> Bool) -> String -> Bool | |
stringHas fn s = | |
s |> String.toList |> List.filter fn |> List.isEmpty |> not | |
parseDefault : Parser Remark | |
parseDefault = | |
P.succeed Default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment