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

Abda861707b1e78e0fce47ced55f84ee?s=128

Paul Fioravanti

September 26, 2019
Tweet

Transcript

  1. 1.

    Bob

  2. 2.
  3. 3.
  4. 4.

    Bob

  5. 5.
  6. 6.
  7. 10.

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

    know what I'm doing!" ! "Fine. Be that way!"
  8. 11.

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

    know what I'm doing!" ! "Fine. Be that way!" ! "Whatever."
  9. 12.

    Bob

  10. 13.

    Bob

  11. 17.
  12. 18.

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

    ques$on yelled ! Understand silence ! Provide a fallback
  13. 19.
  14. 21.

    Dis$nguish Silence hey : String -> String hey message =

    if isSilence message then "Fine. Be that way!"
  15. 22.

    Dis$nguish Silence hey : String -> String hey message =

    if isSilence message then "Fine. Be that way!" isSilence : String -> Bool isSilence message = String.trim message == ""
  16. 23.

    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!"
  17. 24.

    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)
  18. 25.

    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)
  19. 26.

    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
  20. 27.

    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!"
  21. 28.

    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 "?"
  22. 29.

    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!"
  23. 30.

    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!"
  24. 31.

    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."
  25. 32.

    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."
  26. 33.
  27. 34.
  28. 35.
  29. 37.
  30. 38.
  31. 39.
  32. 43.

    ! Repe$$on in condi$on checking ! Repe$$on with String.trim !

    Do we really need a regex? ! Big if statements don't feel right
  33. 44.
  34. 46.
  35. 47.

    Dis$nguish Silence hey : String -> String hey input =

    let message = String.trim input isSilence = message == ""
  36. 48.

    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
  37. 51.

    Dis$nguish Ques$on respondToVerbalRemark : String -> String respondToVerbalRemark message =

    let isQuestion = String.endsWith "?" message hasLetters = String.any Char.isAlpha message
  38. 52.

    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
  39. 54.

    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!"
  40. 55.

    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."
  41. 56.

    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!"
  42. 57.

    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."
  43. 58.