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

Abda861707b1e78e0fce47ced55f84ee?s=128

Paul Fioravanti

September 26, 2019
Tweet

Transcript

  1. Bob

  2. None
  3. None
  4. Bob

  5. None
  6. None
  7. ! "Sure."

  8. ! "Sure." ! "Whoa, chill out!"

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

    know what I'm doing!"
  10. ! "Sure." ! "Whoa, chill out!" ! "Calm down, I

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

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

  13. Bob

  14. Bob ! Dis$nguish ques$ons

  15. Bob ! Dis$nguish ques$ons ! Dis$nguish yelling

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

    ques$on yelled
  17. Bob ! Dis$nguish ques$ons ! Dis$nguish yelling ! Dis$nguish a

    ques$on yelled ! Understand silence
  18. Bob ! Dis$nguish ques$ons ! Dis$nguish yelling ! Dis$nguish a

    ques$on yelled ! Understand silence ! Provide a fallback
  19. ! Take 1

  20. module Bob exposing (hey) hey : String -> String hey

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

    if isSilence message then "Fine. Be that way!"
  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 == ""
  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!"
  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)
  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)
  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
  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!"
  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 "?"
  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!"
  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!"
  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."
  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."
  33. None
  34. None
  35. None
  36. exercism submit Bob.elm

  37. Done!

  38. Done?

  39. None
  40. ! Repe$$on in condi$on checking

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

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

    Do we really need a regex?
  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
  44. ! Take 2

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

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

    let message = String.trim input
  47. Dis$nguish Silence hey : String -> String hey input =

    let message = String.trim input isSilence = message == ""
  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
  49. Dis$nguish Ques$on respondToVerbalRemark : String -> String respondToVerbalRemark message =

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

    let isQuestion = String.endsWith "?" message
  51. Dis$nguish Ques$on respondToVerbalRemark : String -> String respondToVerbalRemark message =

    let isQuestion = String.endsWith "?" message hasLetters = String.any Char.isAlpha message
  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
  53. Dis$nguish Ques$on/Yelling Combos respondToVerbalRemark : String -> String respondToVerbalRemark message

    = let -- ... in case ( isQuestion, isYelling ) of
  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!"
  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."
  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!"
  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."
  58. None
  59. You'll rarely get it right the first *me

  60. Submit o)en, get feedback

  61. Read other people's code

  62. Refactor un,l you're happy

  63. github.com/paulfioravanti/exercism

  64. Thanks! @paulfioravanti