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

Fluent Conf 2017: Tame the frontend with Elm

Fluent Conf 2017: Tame the frontend with Elm

Jeremy Fairbank

June 21, 2017
Tweet

More Decks by Jeremy Fairbank

Other Decks in Programming

Transcript

  1. elm

  2. elm

  3. greet name = "Hello, " ++ name greet "Fluent Conf"

    -- Hello, Fluent Conf Single Expression
  4. add x y = x + y add 2 3

    == 5 add 2 3 == 5 add 2 3 == 5
  5. add x y = x + y add 2 3

    == 5 add 2 3 == 5 add 2 3 == 5
  6. add x y = x + y add 2 3

    == 5 add 2 3 == 5 add 2 3 == 5
  7. myList = [1, 2, 3, 4, 5] double n =

    n * 2 doubleNumbers list = List.map double list doubleNumbers myList -- [2, 4, 6, 8, 10]
  8. myList = [1, 2, 3, 4, 5] double n =

    n * 2 doubleNumbers list = List.map double list doubleNumbers myList -- [2, 4, 6, 8, 10]
  9. myList = [1, 2, 3, 4, 5] double n =

    n * 2 doubleNumbers list = List.map double list doubleNumbers myList -- [2, 4, 6, 8, 10]
  10. myList = [1, 2, 3, 4, 5] double n =

    n * 2 doubleNumbers list = List.map double list doubleNumbers myList -- [2, 4, 6, 8, 10]
  11. myList = [1, 2, 3, 4, 5] double n =

    n * 2 doubleNumbers list = List.map double list doubleNumbers myList -- [2, 4, 6, 8, 10]
  12. myList = [1, 2, 3, 4, 5] double n =

    n * 2 doubleNumbers list = List.map double list doubleNumbers myList -- [2, 4, 6, 8, 10]
  13. add x y = x + y add 1 2

    -- 3 (add 1) 2 -- 3
  14. add x y = x + y add 1 2

    -- 3 (add 1) 2 -- 3
  15. add x y = x + y add 1 2

    -- 3 (add 1) 2 -- 3
  16. add x y = x + y add 1 2

    -- 3 (add 1) 2 -- 3 New function created
  17. Strong Static Types life : Int life = 42 greeting

    : String greeting = "Hello World" isTrue : Bool isTrue = True numbers : List Int numbers = [1, 2, 3]
  18. Strong Static Types life : Int life = 42 greeting

    : String greeting = "Hello World" isTrue : Bool isTrue = True numbers : List Int numbers = [1, 2, 3]
  19. Strong Static Types life : Int life = 42 greeting

    : String greeting = "Hello World" isTrue : Bool isTrue = True numbers : List Int numbers = [1, 2, 3]
  20. greet : String -> String greet name = "Hello, "

    ++ name add : Int -> Int -> Int add x y = x + y
  21. greet : String -> String greet name = "Hello, "

    ++ name add : Int -> Int -> Int add x y = x + y
  22. greet : String -> String greet name = "Hello, "

    ++ name add : Int -> Int -> Int add x y = x + y
  23. greet : String -> String greet name = "Hello, "

    ++ name add : Int -> (Int -> Int) add x y = x + y
  24. The argument to function `greet` is causing a mismatch. 11|

    greet 42 ^^ Function `greet` is expecting the argument to be: String But it is: number Compile time static type checks
  25. dog : { name : String, age : Int }

    dog = { name = "Tucker" , age = 11 } dog.name -- "Tucker" dog.age -- 11 Records
  26. dog : { name : String, age : Int }

    dog = { name = "Tucker" , age = 11 } dog.name -- "Tucker" dog.age -- 11 Records
  27. dog : { name : String, age : Int }

    dog = { name = "Tucker" , age = 11 } dog.name -- "Tucker" dog.age -- 11 Records
  28. dog : { name : String, age : Int }

    dog = { name = "Tucker" , age = 11 } dog.name -- "Tucker" dog.age -- 11 Records
  29. haveBirthday dog = { dog | age = dog.age +

    1 } dog = { name = "Tucker", age = 11 } olderDog = haveBirthday dog olderDog.age -- 12 olderDog.name -- "Tucker" dog.age -- 11 dog.name -- "Tucker" Create New Data
  30. haveBirthday dog = { dog | age = dog.age +

    1 } dog = { name = "Tucker", age = 11 } olderDog = haveBirthday dog olderDog.age -- 12 olderDog.name -- "Tucker" dog.age -- 11 dog.name -- "Tucker" Create New Data
  31. haveBirthday dog = { dog | age = dog.age +

    1 } dog = { name = "Tucker", age = 11 } olderDog = haveBirthday dog olderDog.age -- 12 olderDog.name -- "Tucker" dog.age -- 11 dog.name -- "Tucker" Create New Data
  32. haveBirthday dog = { dog | age = dog.age +

    1 } dog = { name = "Tucker", age = 11 } olderDog = haveBirthday dog olderDog.age -- 12 olderDog.name -- "Tucker" dog.age -- 11 dog.name -- "Tucker" Create New Data
  33. haveBirthday dog = { dog | age = dog.age +

    1 } dog = { name = "Tucker", age = 11 } olderDog = haveBirthday dog olderDog.age -- 12 olderDog.name -- "Tucker" dog.age -- 11 dog.name -- "Tucker" Create New Data
  34. haveBirthday dog = { dog | age = dog.age +

    1 } dog = { name = "Tucker", age = 11 } olderDog = haveBirthday dog olderDog.age -- 12 olderDog.name -- "Tucker" dog.age -- 11 dog.name -- "Tucker" Create New Data
  35. type alias Dog = { name : String , age

    : Int , breed : Breed } type Breed = Sheltie | Poodle
  36. type alias Dog = { name : String , age

    : Int , breed : Breed } type Breed = Sheltie | Poodle
  37. type alias Dog = { name : String , age

    : Int , breed : Breed } type Breed = Sheltie | Poodle Union Type
  38. dog : Dog dog = { name = "Tucker" ,

    age = 11 , breed = Sheltie }
  39. dog : Dog dog = { name = "Tucker" ,

    age = 11 , breed = Sheltie }
  40. dog : Dog dog = { name = "Tucker" ,

    age = 11 , breed = Sheltie }
  41. type Maybe a = Just a | Nothing Either I

    have Just the value a, or I have Nothing.
  42. divide : Float -> Float -> Maybe Float divide x

    y = if y == 0 then Nothing else Just (x / y) divide 4 2 -- Just 2 divide 4 0 -- Nothing
  43. divide : Float -> Float -> Maybe Float divide x

    y = if y == 0 then Nothing else Just (x / y) divide 4 2 -- Just 2 divide 4 0 -- Nothing
  44. divide : Float -> Float -> Maybe Float divide x

    y = if y == 0 then Nothing else Just (x / y) divide 4 2 -- Just 2 divide 4 0 -- Nothing
  45. divide : Float -> Float -> Maybe Float divide x

    y = if y == 0 then Nothing else Just (x / y) divide 4 2 -- Just 2 divide 4 0 -- Nothing
  46. divide : Float -> Float -> Maybe Float divide x

    y = if y == 0 then Nothing else Just (x / y) divide 4 2 -- Just 2 divide 4 0 -- Nothing
  47. divide : Float -> Float -> Maybe Float divide x

    y = if y == 0 then Nothing else Just (x / y) divide 4 2 -- Just 2 divide 4 0 -- Nothing
  48. case divide 4 2 of Just n -> "Result is

    " ++ (toString n) Nothing -> "No Result"
  49. case divide 4 2 of Just n -> "Result is

    " ++ (toString n) Nothing -> "No Result"
  50. case divide 4 2 of Just n -> "Result is

    " ++ (toString n) Nothing -> "No Result"
  51. case divide 4 2 of Just n -> "Result is

    " ++ (toString n) Nothing -> "No Result"
  52. case divide 4 2 of Just n -> "Result is

    " ++ (toString n) -- Nothing -> -- "No Result"
  53. Exhaustive matching This `case` does not have branches for all

    possibilities. 21|> case divide 4 2 of 22|> Just n -> 23|> "Result is " ++ (toString n) You need to account for the following values: Maybe.Nothing Add a branch to cover this pattern!