Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exercism: Bob (Elm)

Exercism: Bob (Elm)

A presentation outlining my solution to the Exercism Bob problem in Elm (https://exercism.io/tracks/elm/exercises/bob).

Presented at the Sydney Elm meetup on 26 September 2019.

Presentation slide deck markdown and speaker notes (useable in Deckset 2):
https://github.com/paulfioravanti/presentations/tree/master/exercism_elm_bob

Paul Fioravanti

September 26, 2019
Tweet

More Decks by Paul Fioravanti

Other Decks in Programming

Transcript

  1. Bob

  2. Bob

  3. ! "Sure." ! "Whoa, chill out!" ! "Calm down, I

    know what I'm doing!" ! "Fine. Be that way!"
  4. ! "Sure." ! "Whoa, chill out!" ! "Calm down, I

    know what I'm doing!" ! "Fine. Be that way!" ! "Whatever."
  5. Bob

  6. Bob

  7. Bob ! Dis$nguish ques$ons ! Dis$nguish yelling ! Dis$nguish a

    ques$on yelled ! Understand silence ! Provide a fallback
  8. Dis$nguish Silence hey : String -> String hey message =

    if isSilence message then "Fine. Be that way!"
  9. Dis$nguish Silence hey : String -> String hey message =

    if isSilence message then "Fine. Be that way!" isSilence : String -> Bool isSilence message = String.trim message == ""
  10. Dis$nguish a Ques$on Yelled hey : String -> String hey

    message = if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!"
  11. Dis$nguish a Ques$on Yelled hey : String -> String hey

    message = if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!" isYelling : String -> Bool isYelling message = (String.toUpper message == message)
  12. Dis$nguish a Ques$on Yelled hey : String -> String hey

    message = if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!" isYelling : String -> Bool isYelling message = (String.toUpper message == message) && not (onlyDigitsOrNonWords message)
  13. Dis$nguish a Ques$on Yelled isYelling : String -> Bool isYelling

    message = (String.toUpper message == message) && not (onlyDigitsOrNonWords message) onlyDigitsOrNonWords : String -> Bool onlyDigitsOrNonWords message = let regex = "^([0-9]|[^a-zA-Z])+$" |> Regex.fromString |> Maybe.withDefault Regex.never in Regex.contains regex message
  14. Dis$nguish a Ques$on Yelled hey : String -> String hey

    message = if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!"
  15. Dis$nguish a Ques$on Yelled hey : String -> String hey

    message = if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!" isQuestion : String -> Bool isQuestion message = message |> String.trim |> String.endsWith "?"
  16. Dis$nguish Yelled Ques$on hey : String -> String hey message

    = if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!"
  17. Dis$nguish Yelling hey : String -> String hey message =

    if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!" else if isYelling message then "Whoa, chill out!"
  18. Dis$nguish Ques$ons hey : String -> String hey message =

    if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!" else if isYelling message then "Whoa, chill out!" else if isQuestion message then "Sure."
  19. Provide a Fallback hey : String -> String hey message

    = if isSilence message then "Fine. Be that way!" else if isYelling message && isQuestion message then "Calm down, I know what I'm doing!" else if isYelling message then "Whoa, chill out!" else if isQuestion message then "Sure." else "Whatever."
  20. ! Repe$$on in condi$on checking ! Repe$$on with String.trim !

    Do we really need a regex? ! Big if statements don't feel right
  21. Dis$nguish Silence hey : String -> String hey input =

    let message = String.trim input isSilence = message == ""
  22. Dis$nguish Silence hey : String -> String hey input =

    let message = String.trim input isSilence = message == "" in if isSilence then "Fine. Be that way!" else respondToVerbalMessage message
  23. Dis$nguish Ques$on respondToVerbalRemark : String -> String respondToVerbalRemark message =

    let isQuestion = String.endsWith "?" message hasLetters = String.any Char.isAlpha message
  24. Dis$nguish Yelling respondToVerbalRemark : String -> String respondToVerbalRemark message =

    let isQuestion = String.endsWith "?" message hasLetters = String.any Char.isAlpha message isYelling = hasLetters && String.toUpper message == message
  25. Dis$nguish Ques$on/Yelling Combos respondToVerbalRemark : String -> String respondToVerbalRemark message

    = case ( isQuestion, isYelling ) of ( True, True ) -> "Calm down, I know what I'm doing!"
  26. Dis$nguish Ques$on/Yelling Combos respondToVerbalRemark : String -> String respondToVerbalRemark message

    = case ( isQuestion, isYelling ) of ( True, True ) -> "Calm down, I know what I'm doing!" ( True, False ) -> "Sure."
  27. Dis$nguish Ques$on/Yelling Combos respondToVerbalRemark : String -> String respondToVerbalRemark message

    = case ( isQuestion, isYelling ) of ( True, True ) -> "Calm down, I know what I'm doing!" ( True, False ) -> "Sure." ( False, True ) -> "Whoa, chill out!"
  28. Dis$nguish Ques$on/Yelling Combos respondToVerbalRemark : String -> String respondToVerbalRemark message

    = case ( isQuestion, isYelling ) of ( True, True ) -> "Calm down, I know what I'm doing!" ( True, False ) -> "Sure." ( False, True ) -> "Whoa, chill out!" ( False, False ) -> "Whatever."