Top Banner
https://github.com/rtfeldman/elm-workshop welcome to elm! please follow these instructions to get set up:
234

welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Jun 04, 2018

Download

Documents

dothuan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

https://github.com/rtfeldman/elm-workshop

welcome to elm!

please follow these instructions to get set up:

Page 2: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

elm@rtfeldman

Page 3: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

1. Rendering a Page

Page 4: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

elmcompiles to

Page 5: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural
Page 6: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

functions

Page 7: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural
Page 8: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural
Page 9: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

arguments

Page 10: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

no parentheses

Page 11: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural
Page 12: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

required

Page 13: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

expression

Page 14: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

expression

JS: quantity === 1 ? singular : plural

Page 15: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

call pluralize passing 3 arguments

Page 16: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

call text passing 1 argument

Page 17: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

import the Html module

Page 18: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural
Page 19: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

why bother?

Page 20: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

comments about comments

Page 21: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

-- this is a comment

Page 22: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

-- this is a comment

{- this is ablock comment -}

Page 23: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Html

Virtual DOM

Page 24: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

<ul class="highway"> <li>danger</li> <li>zone</li></ul>

Page 25: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

<ul class="highway"> <li>danger</li> <li>zone</li></ul>

ulclass="highway"

li li

text text“danger” “zone”

Page 26: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

<ul class="highway"> <li>danger</li> <li>zone</li></ul>

ulclass="highway"

li li

text text“danger” “zone”

ul [ class "highway" ] [ li [] [ text "danger" ], li [] [ text "zone" ] ]

Page 27: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

<ul class="highway"> <li>danger</li> <li>zone</li></ul>

ulclass="highway"

li li

text text“danger” “zone”

ul [ class "highway" ] [ li [] [ text "danger" ] , li [] [ text "zone" ] ]

Page 28: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Exercise: resolve the TODOs in part1/Main.elm

<ul class="highway"> <li>danger</li> <li>zone</li></ul>

ul [ class "highway" ] [ li [] [ text "danger" ] , li [] [ text "zone" ] ]

Page 29: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

2. Basic Data Structures

Page 30: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

strings

Page 31: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

"foo" ++ "bar" == "foobar"

Page 32: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

"foo" ++ "bar" == "foobar"

"foo" + "bar" === "foobar" // JS

Page 33: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

toString 5 == "5"

Page 34: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

let-expressions

Page 35: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pluralize singular plural quantity = if quantity == 1 then singular else plural

Page 36: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pluralize singular plural quantity = if quantity == 1 then toString quantity ++ " " ++ singular else toString quantity ++ " " ++ plural

Page 37: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pluralize singular plural quantity = if quantity == 1 then toString quantity ++ " " ++ singular else toString quantity ++ " " ++ plural

code duplication

Page 38: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pluralize singular plural quantity = let quantityStr = toString quantity

prefix = quantityStr ++ " " in if quantity == 1 then prefix ++ singular else prefix ++ plural

Page 39: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pluralize singular plural quantity = let quantityStr = toString quantity

prefix = quantityStr ++ " " in if quantity == 1 then prefix ++ singular else prefix ++ plural

let-expression

Page 40: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pluralize singular plural quantity = let quantityStr = toString quantity

prefix = quantityStr ++ " " in if quantity == 1 then prefix ++ singular else prefix ++ plural

inaccessible inoutside scope

Page 41: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pluralize singular plural quantity = let quantityStr = toString quantity

prefix = quantityStr ++ " " in if quantity == 1 then prefix ++ singular else prefix ++ plural

entire expressionevaluates to this

Page 42: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

collections

Records, Tuples, and Lists

Page 43: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

record = { name = "thing", x = 1, y = 3 }

Page 44: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

record = { name = "thing", x = 1, y = 3 }

record.name == "thing"record.x == 1record.y == 3

Page 45: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

record = { name = "thing", x = 1, y = 3 }

tuple = ( "thing", 1, 3 )

a tuple is shorthand for a record

Page 46: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

record = { name = "thing", x = 1, y = 3 }

tuple = ( "thing", 1, 3 )

( name, x, y ) = ( "thing", 1, 3 )

tuple destructuring

Page 47: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

record = { name = "thing", x = 1, y = 3 }

tuple = ( "thing", 1, 3 )

( name, x, y ) = ( "thing", 1, 3 )

name == "thing"x == 1

Page 48: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

[ 1, 2, 3 ]

Page 49: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

[ 1, 2, 3 ]

[ [ "foo", "bar" ], [ "baz" ] ]

Page 50: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

[ 1, 2, 3 ]

[ [ "foo", "bar" ], [ "baz" ] ]

[ "foo", 66 ]

Page 51: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

what does this rule get us?

Page 52: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Records Tuples Lists

Page 53: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Records Tuples Lists

fixedlength

fixed length

variable length

Page 54: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Records Tuples Lists

mixedcontents

mixedcontents

uniformcontents

fixedlength

fixed length

variable length

Page 55: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Exercise: resolve the TODOs in part2/Main.elm

record = { x = 4, y = 2 }

record.x == 4record.y == 2

toString 99 == "99"

negate (5 + 9) == -14 toString 99 ++ " balloons" == "99 balloons"

Page 56: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

3. Adding Interaction

Page 57: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

booleans

Page 58: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

True

False

Page 59: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

True

False

x == y

Page 60: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

True

False

x == y

not (x == y)

Page 61: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

True

False

x == y

not (x == y)

x /= y

Page 62: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

partial application

Page 63: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

function pluralizeLeaves(quantity) { return pluralize("leaf", "leaves", quantity);}

Page 64: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

function pluralizeLeaves(quantity) { return pluralize("leaf", "leaves", quantity);}

pluralizeLeaves quantity = pluralize "leaf" "leaves" quantity

Page 65: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

function pluralizeLeaves(quantity) { return pluralize("leaf", "leaves", quantity);}

pluralizeLeaves quantity = pluralize "leaf" "leaves" quantity

pluralizeLeaves = pluralize "leaf" "leaves"

Page 66: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

function pluralizeLeaf(plural, quantity) { return pluralize("leaf", plural, quantity);}

pluralizeLeaf plural quantity = pluralize "leaf" plural quantity

pluralizeLeaf = pluralize "leaf"

Page 67: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

List.filter

Page 68: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

isKeepable num = num >= 2

List.filter isKeepable [ 1, 2, 3 ]

== [ 2, 3 ]

Page 69: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Elm: (\foo -> foo + 1)

JS: (function(foo) { return foo + 1 })

Page 70: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

List.filter (\num -> num >= 2) [ 1, 2, 3 ]

== [ 2, 3 ]

isKeepable num = num >= 2

List.filter isKeepable [ 1, 2, 3 ]

== [ 2, 3 ]

Page 71: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

List.map

Page 72: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

double num = num * 2

Page 73: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

double num = num * 2

List.map double [ 1, 2, 3 ]

Page 74: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

double num = num * 2

List.map double [ 1, 2, 3 ]

== [ 2, 4, 6 ]

Page 75: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

List.map negate [ 1, 2, -3 ]

== [ -1, -2, 3 ]

Page 76: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

List.map (pluralize "leaf" "leaves") [ 1, 2, 3 ]

== [ "1 leaf", "2 leaves", "3 leaves" ]

Page 77: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

The Elm Architecture

model view update

Page 78: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

view model =

div [ class "content" ] []

Page 79: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

view

Html

Elm Runtime

Page 80: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

view

Htmlh1 [] [ text "ElmHub" ]

Elm Runtime

Page 81: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewModel

Htmlh1 [] [ text "ElmHub" ]

Elm Runtime

Page 82: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewModel

Htmlh1 [] [ text "ElmHub" ]

{ query = "tutorial"

, results = []

}

Elm Runtime

Page 83: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Htmlh1 [] [ text "ElmHub" ]

{ query = "tutorial"

, results = []

}

Elm Runtime

Page 84: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

maxResults and Show More

potential feature:

Page 85: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

{ operation = "SHOW_MORE", data = 10}

Page 86: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

{ operation = "SHOW_MORE", data = 10}

update msg model = if msg.operation == "SHOW_MORE" then { maxResults = model.maxResults + msg.data } else model

Page 87: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

update msg model = if msg.operation == "SHOW_MORE" then { maxResults = model.maxResults + msg.data } else model

{ operation = "SHOW_MORE", data = 10}

what if there are other fields in the model?

Page 88: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

update msg model = if msg.operation == "SHOW_MORE" then { maxResults = model.maxResults + msg.data } else model

update msg model = if msg.operation == "SHOW_MORE" then { model | maxResults = model.maxResults + msg.data } else model

Page 89: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Elm Runtime

viewupdate Model

Msg Html

Page 90: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

onClick

Page 91: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

{ operation = "SHOW_MORE", data = 10 }

Page 92: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

onClick { operation = "SHOW_MORE", data = 10 }

Page 93: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

button [ onClick { operation = "SHOW_MORE", data = 10 } ] [ text "Show More" ]

Page 94: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg Htmlh1 [] [ text "ElmHub" ]

{ query = "tutorial"

, results = []

}

Elm Runtime

Page 95: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

DELETE_BY_ID

Page 96: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg Htmlh1 [] [ text "ElmHub" ]

{ query = "tutorial"

, results = []

}

{ operation = "DELETE_BY_ID"

, data = 2

}

Elm Runtime

Page 97: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Elm Runtime

viewupdate Model

Msg Html

Page 98: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Exercise: resolve the TODOs in part3/Main.elmElm: (\foo -> foo + 1)

JS: (function(foo) { return foo + 1 })

{ animals | cats = animals.cats + 1 }

List.filter (\num -> num >= 2) [ 1, 2, 3 ]

== [ 2, 3 ]

Page 99: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

4. Annotations

Page 100: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

-- query is a string

query = "tutorial"

Page 101: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

query : String

query = "tutorial"

Page 102: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

stars : Int

stars = 123

Page 103: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

searchResult : { name : String, stars : Int }

searchResult = { name = "blah", stars = 415 }

Page 104: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

list : List String

list = [ "foo", "bar", "baz" ]

Page 105: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

list : List Float

list = [ 1.1, 2.2, 3.3 ]

Page 106: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

list : List Int

list = [ 1.1, 2.2, 3.3 ]

ERROR!

Page 107: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

model :

{ query : String

, results :

List

{ id : Int

, name : String

, stars : Int

}

}

Page 108: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias SearchResult =

{ id : Int

, name : String

, stars : Int

}

Page 109: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Model =

{ query : String

, results : List SearchResult

}

type alias SearchResult =

{ id : Int

, name : String

, stars : Int

}

Page 110: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg =

{ operation : String

, data : Int

}

Page 111: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg =

{ operation : String

, data : Int

}

view : Model -> Html Msg

view model =

Page 112: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg =

{ operation : String

, data : Int

}

view : Model -> Html Msg

view model =

Page 113: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg =

{ operation : String

, data : Int

}

view : Model -> Html Msg

view model =

button [ onClick { operation = "RESET", data = "all" } ]

[ text "Reset All" ]

Page 114: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

view : Model -> Html String

view model =

button [ onClick "RESET" ]

[ text "Reset All" ]

Page 115: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

view : Model -> Html Float

view model =

button [ onClick 12.34 ]

[ text "Reset All" ]

Page 116: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

view : Model -> Html Msg

view model =

button [ onClick { operation = "RESET", data = "all" } ]

[ text "Reset All" ]

Page 117: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg =

{ operation : String

, data : Int

}

update : Msg -> Model -> Model

update msg model =

Page 118: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg =

{ operation : String

, data : Int

}

update : Msg -> Model -> Model

update msg model =

Page 119: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pluralize : String -> String -> Int -> String

Page 120: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

elm-repl

Page 121: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Exercise: resolve the TODOs in part4/Main.elm

pluralize : String -> String -> Int -> String

Page 122: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

5. Union Types

Page 123: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

case-expressions

Page 124: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

if msg.operation == "DELETE_BY_ID" then -- remove from modelelse if msg.operation == "LOAD_RESULTS" then -- load more resultselse -- default branch

Page 125: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

case msg.operation of "DELETE_BY_ID" -> -- remove from model

"LOAD_RESULTS" -> -- load more results

_ -> -- default branch

Page 126: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

union types

Page 127: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Sorting = Ascending | Descending | Randomized

Page 128: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Sorting = Ascending | Descending | Randomized

Page 129: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Bool = True | False

Page 130: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Bool = True | False

type

constantconstant

Page 131: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

case currentSorting of Ascending -> -- sort ascending here

Descending -> -- sort descending here

Randomized -> -- sort randomized here

Page 132: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Sorting = Ascending String | Descending String | Randomized

Page 133: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Sorting = Ascending String | Descending String | Randomized

type

constant

Page 134: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Sorting = Ascending String | Descending String | Randomized

type

function

functionconstant

Page 135: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Sorting = Ascending String | Descending String | Randomized

type

function

functionconstant

String -> Sorting

Page 136: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

case currentSorting of Ascending colName -> -- sort ascending here

Descending colName -> -- sort descending here

Randomized -> -- sort randomized here

Page 137: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg = { operation : String , data : Int }

Page 138: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg = { operation : String , data : Int }

{ operation = "DELETE_BY_ID" , data = 3 }

Page 139: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias Msg = { operation : String , data : Int }

{ operation = "DELETE_BY_ID" , data = 3 }

{ operation = "SET_QUERY" , data = "tutorial" }

Page 140: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Msg = SetQuery String | DeleteById Int

type alias Msg = { operation : String , data : Int }

Page 141: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

case msg of SetQuery query -> -- set query in the model here

DeleteById id -> -- delete the result with this id here

type Msg = SetQuery String | DeleteById Int

Page 142: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

function (String -> Sorting)

Exercise: resolve the TODOs in part5/Main.elm

type Sorting = Ascending String | Descending String | Randomized

type

constantfunction (String -> Sorting)

Page 143: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

6. Decoding JSON

Page 144: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

parseInt

in JavaScript

Page 145: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

parseInt("42")

Page 146: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

parseInt("42") == 42

Page 147: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

parseInt("42") == 42parseInt("halibut")

Page 148: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

parseInt("42") == 42parseInt("halibut") == NaN

Page 149: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

parseInt

String.toInt

Page 150: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

String.toInt "42"

Page 151: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

String.toInt "42" == Ok 42

Page 152: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

String.toInt "42" == Ok 42

type Result = Ok somethingGood Err somethingBad

Page 153: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

String.toInt "42" == Ok 42String.toInt "halibut"

type Result = Ok somethingGood Err somethingBad

Page 154: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

String.toInt "42" == Ok 42String.toInt "halibut" == Err "umm halibut is not an int"

type Result = Ok somethingGood Err somethingBad

Page 155: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Maybe

Page 156: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Result = Ok somethingGood Err somethingBad

Page 157: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Result = Ok somethingGood Err somethingBad

type Maybe = Just someValue Nothing

Page 158: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Maybe = Just someValue Nothing

List.head [ 5, 10, 15 ] == Just 5

Page 159: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type Maybe = Just someValue Nothing

List.head [ 5, 10, 15 ] == Just 5

List.head [] == Nothing

Page 160: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

pipelines

Page 161: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

List.filter (\num -> num < 5) [ 2, 4, 6, 8 ]

Page 162: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

List.filter (\num -> num < 5) [ 2, 4, 6, 8 ]

List.reverse (List.filter (\num -> num < 5) [ 2, 4, 6, 8 ])

Page 163: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

[ 2, 4, 6, 8 ] |> List.filter (\num -> num < 5) |> List.reverse

List.reverse (List.filter (\num -> num < 5) [ 2, 4, 6, 8 ])

Page 164: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

[ 2, 4, 6, 8 ] |> List.filter (\num -> num < 5) |> List.reverse |> List.map negate

List.reverse (List.filter (\num -> num < 5) [ 2, 4, 6, 8 ])

Page 165: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

[ 2, 4, 6, 8 ] |> List.filter (\num -> num < 5) |> List.reverse |> List.map negate |> List.head

List.reverse (List.filter (\num -> num < 5) [ 2, 4, 6, 8 ])

Page 166: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decoders

Page 167: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decodeString float "123.45"

Page 168: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decodeString float "123.45"

== Ok 123.45

Page 169: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decodeString float "123.45"

== Ok 123.45

decodeString float "blah"

Page 170: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decodeString float "123.45"

== Ok 123.45

decodeString float "blah"

== Err "blah is not a float!"

Page 171: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decodeString (list int) "[1, 2, 3]"

Page 172: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decodeString (list int) "[1, 2, 3]"

== Ok [ 1, 2, 3 ]

Page 173: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decodeString (list int) "[1, 2, 3]"

== Ok [ 1, 2, 3 ]

"[1, 2, 3]" |> decodeString (list int)

== Ok [ 1, 2, 3 ]

Page 174: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

decoding objects into records

Page 175: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

makeGameState score playing = { score = score, playing = playing }

Page 176: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

makeGameState score playing = { score = score, playing = playing }

decoder = ???

Page 177: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

makeGameState score playing = { score = score, playing = playing }

decoder = ???

decodeString decoder """{"score": 5.5, "playing": true}"""

== Ok { score = 5.5, playing = True }

Page 178: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

makeGameState score playing = { score = score, playing = playing }

decoder = decode makeGameState |> required "score" float |> required "playing" bool

decodeString decoder """{"score": 5.5, "playing": true}"""

== Ok { score = 5.5, playing = True }

Page 179: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

makeGameState score playing = { score = score, playing = playing }

decoder = decode makeGameState |> required "score" float |> required "playing" bool

decodeString decoder """{"score": 5.5, "playing": true}"""

== Ok { score = 5.5, playing = True }

Page 180: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

makeGameState score playing = { score = score, playing = playing }

decoder = decode makeGameState |> required "score" float |> required "playing" bool

decodeString decoder """{"score": 5.5, "playing": true}"""

== Ok { score = 5.5, playing = True }

Page 181: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias GameState = { score : Float, playing : Bool }

makeGameState : Float -> Bool -> GameStatemakeGameState score playing = { score = score, playing = playing }

Page 182: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias GameState = { score : Float, playing : Bool }

GameState : Float -> Bool -> GameState

makeGameState : Float -> Bool -> GameStatemakeGameState score playing = { score = score, playing = playing }

Page 183: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias GameState = { score : Float, playing : Bool }

GameState : Float -> Bool -> GameState

makeGameState : Float -> Bool -> GameStatemakeGameState score playing = { score = score, playing = playing }

makeGameState 2.3 True == GameState 2.3 True

Page 184: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias GameState = { score : Float, playing : Bool }

decoder = decode GameState |> required "score" float |> required "playing" bool

decodeString decoder """{"score": 5.5, "playing": true}"""

== Ok { score = 5.5, playing = True }

Page 185: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias GameState = { score : Float, playing : Bool }

decoder = decode GameState |> required "score" float |> required "playing" bool

decodeString decoder """{"score": 5.5, "playing": true}"""

== Ok { score = 5.5, playing = True }

Page 186: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

type alias GameState = { score : Float, playing : Bool }

decoder = decode GameState |> required "score" float |> required "playing" bool

Exercise: resolve the TODOs in part6/Main.elm

Page 187: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

7. Client-Server Communication

Page 188: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

function guarantees

Page 189: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

same arguments?

same return value

Page 190: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Math.random()

Page 191: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Random.generate

Math.random()

Page 192: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Random.generate

pickGreeting : List String -> String

Page 193: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Random.generate

pickGreeting : List String -> String

Page 194: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Random.generate

pickGreeting : List String -> Cmd Msg

pickGreeting : List String -> String

Page 195: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg

h1 [] [ text "ElmHub" ]

{ query = "tutorial"

, results = []

}

{ operation = "DELETE_BY_ID"

, data = 2

}

Elm Runtime

Html

Page 196: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg

h1 [] [ text "ElmHub" ]

{ query = "tutorial"

, results = []

}

DeleteById 2

Elm Runtime

Html Msg

Page 197: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg

Elm Runtime

Html Msg

Page 198: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg

Elm Runtime

how the Elm Runtime talks to update Html Msg

Page 199: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg Html Msg

Elm Runtime

Cmd Msg

Page 200: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg Html Msg

Elm Runtime

Cmd MsgSetGreeting 5

pickGreeting : List String -> Cmd Msg

Page 201: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Html.beginnerProgram

update : Msg -> Model -> Model

Page 202: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Html.program

update : Msg -> Model -> ( Model, Cmd Msg )

Page 203: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate Model

Msg Html Msg

Elm Runtime

Cmd MsgSetGreeting 5

pickGreeting : List String -> Cmd Msg

Page 204: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate

Msg Html Msg

Elm Runtime

Cmd MsgSetGreeting 5

pickGreeting : List String -> Cmd Msg

( model, pickGreeting greetings )

Model

Page 205: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

viewupdate

Msg Html Msg

Elm Runtime

Cmd Msg

( model, Cmd.none )

Model

Page 206: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

another function guarantee

Page 207: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

no side effects

Page 208: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

no modifying external state

Page 209: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

fire-and-forget HTTP POST

Page 210: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

fire-and-forget HTTP POST

✓ same arguments, same result

Page 211: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

fire-and-forget HTTP POST

✗ does not modify external state

✓ same arguments, same result

Page 212: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

side effects

Page 213: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

side effects

managed effects

Page 214: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Cmd

Page 215: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Http.getString

Page 216: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

getString : String -> Cmd String

what if it looked like this?

Page 217: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

getString : String -> Cmd String

what if it looked like this?

it always produces a string?

Page 218: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

getString : String -> Task Http.Error String

Page 219: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

getString : String -> Task Http.Error String

result if it fails

Page 220: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

getString : String -> Task Http.Error String

result if it succeeds

Page 221: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Task.perform

Task Cmd

Page 222: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Task.perform

Task Cmdtranslate failure into a Msg

Page 223: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Task.perform

Task Cmdtranslate failure into a Msg

translate success into a Msg

Page 224: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Task.perform (\err -> ShowError err) (\json -> SetJson json) (Http.getString "https://api.github.com?q=blah")

Task Cmdtranslate failure into a Msg

translate success into a Msg

Page 225: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Task.perform ShowError SetJson (Http.getString "https://api.github.com?q=blah")

Task Cmdtranslate failure into a Msg

translate success into a Msg

Page 226: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Http.getString "https://api.github.com?q=blah" |> Task.perform ShowError SetJson

Task Cmdtranslate failure into a Msg

translate success into a Msg

Page 227: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

cmd : Cmd Msgcmd = Http.getString "https://api.github.com?q=blah" |> Task.perform ShowError SetJson

Task Cmdtranslate failure into a Msg

translate success into a Msg

Page 228: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

-- success gives you a StringHttp.getString url

Page 229: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

-- success gives you a StringHttp.getString url

-- success gives you a SearchResultHttp.get searchResultDecoder url

Page 230: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Exercise: resolve the TODOs in part7/Main.elm

cmd : Cmd Msgcmd = Http.getString "https://api.github.com?q=blah" |> Task.perform ShowError SetJson

-- success gives you a SearchResultHttp.get searchResultDecoder url

Page 231: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

day 1 wrap

Page 232: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

Slack: elmlang.herokuapp.com

Helpful Resources

Weekly News: elmweekly.nl

Google Group: elm-discuss

Page 233: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural
Page 234: welcome to elm! - Frontend Masters · welcome to elm! please follow these ... let-expressions. pluralize singular plural quantity = if quantity == 1 then ... pluralize singular plural

http://elm-conf.us

Sept 15, 2016

Keynote: Evan CzaplickiLocation: St. Louis, MO

Tickets: $100