pluralize singular plural quantity = if quantity == 1 then singular else plural... pluralize singular plural quantity =... pluralize singular plural quantity =... pluralize singular p
Trang 1welcome to elm!
please follow these instructions to get set up:
Trang 2@rtfeldman
Trang 31 Rendering a Page
Trang 4compiles to
Trang 6functions
Trang 9arguments
Trang 10no parentheses
Trang 12required
Trang 13expression
Trang 14JS: quantity === 1 ? singular : plural
Trang 15call pluralize passing 3 arguments
Trang 16call text passing 1 argument
Trang 17import the Html module
Trang 19why bother?
Trang 20comments about comments
Trang 21this is a comment
Trang 22{- this is a
block comment -}
Trang 23Virtual DOM
Trang 24<ul class="highway"> <li>danger</li> <li>zone</li>
</ul>
Trang 25<ul class="highway">
<li>danger</li>
<li>zone</li>
Trang 26<ul class="highway">
<li>danger</li>
<li>zone</li>
Trang 27<ul class="highway">
<li>danger</li>
<li>zone</li>
Trang 28Exercise: 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" ] ]
Trang 292 Basic Data Structures
Trang 30strings
Trang 31"foo" ++ "bar" == "foobar"
Trang 32"foo" ++ "bar" == "foobar"
"foo" + "bar" === "foobar" // JS
Trang 33toString 5 == "5"
Trang 34let-expressions
Trang 35pluralize singular plural quantity =
if quantity == 1 then
singular
else
plural
Trang 36pluralize singular plural quantity =
Trang 37pluralize singular plural quantity =
Trang 38pluralize singular plural quantity =
Trang 39pluralize singular plural quantity =
let quantityStr = toString quantity
prefix = quantityStr ++ " "
in
if quantity == 1 then
else prefix ++ plural
let-expression
Trang 40pluralize singular plural quantity =
Trang 41pluralize singular plural quantity =
Trang 42Records, Tuples, and Lists
Trang 43record =
{ name = "thing", x = 1, y = 3 }
Trang 48[ 1, 2, 3 ]
Trang 49[ [ "foo", "bar" ], [ "baz" ] ]
Trang 50[ 1, 2, 3 ] [ [ "foo", "bar" ], [ "baz" ] ]
[ "foo", 66 ]
Trang 51what does this rule get us?
Trang 52Records Tuples Lists
Trang 53Records Tuples Lists
fixed
length
fixed length
variable length
Trang 54Records Tuples Lists
mixed
contents
mixed contents
uniform contents
fixed
length
fixed length
variable length
Trang 55Exercise: resolve the TODOs in part2/Main.elm
negate (5 + 9) == -14 toString 99 ++ " balloons"
== "99 balloons"
Trang 563 Adding Interaction
Trang 57booleans
Trang 58TrueFalse
Trang 59x == y
Trang 60False
x == y
not (x == y)
Trang 62partial application
Trang 63function pluralizeLeaves(quantity) {
return pluralize("leaf", "leaves", quantity);}
Trang 66function pluralizeLeaf(plural, quantity) {
return pluralize("leaf", plural, quantity);}
pluralizeLeaf plural quantity =
pluralize "leaf" plural quantity
pluralizeLeaf =
pluralize "leaf"
Trang 67List.filter
Trang 68isKeepable num = num >= 2
List.filter isKeepable [ 1, 2, 3 ]
== [ 2, 3 ]
Trang 69Elm: (\foo -> foo + 1)
JS: (function(foo) { return foo + 1 })
Trang 70List.filter (\num -> num >= 2) [ 1, 2, 3 ]
== [ 2, 3 ]
isKeepable num = num >= 2
List.filter isKeepable [ 1, 2, 3 ]
== [ 2, 3 ]
Trang 71List.map
Trang 72double num = num * 2
Trang 73double num =
num * 2
List.map double [ 1, 2, 3 ]
Trang 74double num =
num * 2
List.map double [ 1, 2, 3 ]
== [ 2, 4, 6 ]
Trang 75List.map negate [ 1, 2, -3 ]
Trang 76List.map (pluralize "leaf" "leaves") [ 1, 2, 3 ]
== [ "1 leaf", "2 leaves", "3 leaves" ]
Trang 77The Elm Architecture
model view update
Trang 78view model =
div [ class "content" ] []
Trang 79Html
Elm Runtime
Trang 80Html h1 [] [ text "ElmHub" ]
Elm Runtime
Trang 81Model
Html h1 [] [ text "ElmHub" ]
Elm Runtime
Trang 84maxResults and Show More
potential feature:
Trang 85{ operation = "SHOW_MORE", data = 10
}
Trang 86{ operation = "SHOW_MORE"
, data = 10
}
update msg model =
if msg.operation == "SHOW_MORE" then
{ maxResults = model.maxResults + msg.data } else
model
Trang 87update msg model =
if msg.operation == "SHOW_MORE" then
{ maxResults = model.maxResults + msg.data }
Trang 88update 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
Trang 89Elm Runtime
view
Trang 90onClick
Trang 91{ operation = "SHOW_MORE", data = 10 }
Trang 92onClick { operation = "SHOW_MORE", data = 10 }
Trang 93
[ onClick { operation = "SHOW_MORE", data = 10 } ] [ text "Show More" ]
Trang 95DELETE_BY_ID
Trang 96Elm Runtime
Trang 97view
Trang 98Exercise: resolve the TODOs in part3/Main.elm
Elm: (\foo -> foo + 1)
JS: (function(foo) { return foo + 1 })
{ animals | cats = animals.cats + 1 }
List.filter (\num -> num >= 2) [ 1, 2, 3 ]
== [ 2, 3 ]
Trang 994 Annotations
Trang 100query is a string
query = "tutorial"
Trang 101query : String
query = "tutorial"
Trang 102stars : Int
stars = 123
Trang 103searchResult : { name : String, stars : Int }
searchResult = { name = "blah", stars = 415 }
Trang 104list : List String
list = [ "foo", "bar", "baz" ]
Trang 105list : List Float
list = [ 1.1, 2.2, 3.3 ]
Trang 106list : List Int
list = [ 1.1, 2.2, 3.3 ]
ERROR!
Trang 108type alias SearchResult =
{ id : Int
, name : String
, stars : Int
}
Trang 109type alias Model =
Trang 110type alias Msg =
{ operation : String , data : Int
}
Trang 111type alias Msg =
{ operation : String , data : Int
}
view : Model -> Html Msg
view model =
Trang 112type alias Msg =
{ operation : String , data : Int
}
view : Model -> Html Msg
view model =
Trang 114view : Model -> Html String
view model =
button [ onClick "RESET" ] [ text "Reset All" ]
Trang 115view : Model -> Html Float
view model =
button [ onClick 12.34 ] [ text "Reset All" ]
Trang 116view : Model -> Html Msg
view model =
button [ onClick { operation = "RESET", data = "all" } ] [ text "Reset All" ]
Trang 119pluralize : String -> String -> Int -> String
Trang 120elm-repl
Trang 121Exercise: resolve the TODOs in part4/Main.elm
pluralize : String -> String -> Int -> String
Trang 1225 Union Types
Trang 123case-expressions
Trang 124if msg.operation == "DELETE_BY_ID" then
else if msg.operation == "LOAD_RESULTS" then
else
Trang 126union types
Trang 127type Sorting =
Ascending | Descending | Randomized
Trang 128type Sorting
= Ascending | Descending | Randomized
Trang 129type Bool = True | False
Trang 132type Sorting
Trang 135String -> Sorting
Trang 137type alias Msg =
{ operation : String , data : Int
}
Trang 138}
Trang 139}
{ operation = "SET_QUERY"
, data = "tutorial"
}
Trang 140type Msg
= SetQuery String | DeleteById Int
type alias Msg =
{ operation : String , data : Int
}
Trang 142function (String -> Sorting)
Exercise: resolve the TODOs in part5/Main.elm
Trang 1436 Decoding JSON
Trang 144in JavaScript
Trang 145parseInt("42")
Trang 146parseInt("42") == 42
Trang 147parseInt("halibut")
Trang 148parseInt("42") == 42 parseInt("halibut") == NaN
Trang 149String.toInt
Trang 150String.toInt "42"
Trang 151String.toInt "42" == Ok 42
Trang 152type Result =
Trang 155Maybe
Trang 156type Result =
Trang 158type Maybe =
Nothing
List.head [ 5, 10, 15 ] == Just 5
Trang 159type Maybe =
Nothing
List.head [ 5, 10, 15 ] == Just 5 List.head [] == Nothing
Trang 160pipelines
Trang 161List.filter (\num -> num < 5) [ 2, 4, 6, 8 ]
Trang 162List.reverse (List.filter (\num -> num < 5) [ 2, 4, 6, 8 ])
Trang 166decoders
Trang 167decodeString float "123.45"
Trang 168== Ok 123.45
Trang 169decodeString float "123.45"
== Ok 123.45
decodeString float "blah"
Trang 170decodeString float "123.45"
== Ok 123.45
decodeString float "blah"
== Err "blah is not a float!"
Trang 171decodeString (list int) "[1, 2, 3]"
Trang 172== Ok [ 1, 2, 3 ]
Trang 173decodeString (list int) "[1, 2, 3]"
== Ok [ 1, 2, 3 ]
"[1, 2, 3]"
|> decodeString (list int)
== Ok [ 1, 2, 3 ]
Trang 174decoding objects into records
Trang 175makeGameState score playing =
{ score = score, playing = playing }
Trang 176makeGameState score playing =
{ score = score, playing = playing }decoder =
???
Trang 177makeGameState score playing =
{ score = score, playing = playing }
decoder =
???
decodeString decoder
"""{"score": 5.5, "playing": true}"""
== Ok { score = 5.5, playing = True }
Trang 178makeGameState 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 }
Trang 179makeGameState 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 }
Trang 180makeGameState 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 }
Trang 181type alias GameState =
{ score : Float, playing : Bool }
makeGameState : Float -> Bool -> GameState
{ score = score, playing = playing }
Trang 182type alias GameState =
{ score : Float, playing : Bool }
GameState : Float -> Bool -> GameState
makeGameState : Float -> Bool -> GameState
{ score = score, playing = playing }
Trang 183type alias GameState =
{ score : Float, playing : Bool }
GameState : Float -> Bool -> GameState
makeGameState : Float -> Bool -> GameStatemakeGameState score playing =
{ score = score, playing = playing }
Trang 184type 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 }
Trang 185type 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 }
Trang 186type alias GameState = { score : Float, playing : Bool }
decoder = decode GameState
|> required "score" float |> required "playing" boolExercise: resolve the TODOs in part6/Main.elm
Trang 1877 Client-Server Communication
Trang 188function guarantees
Trang 189same arguments?
same return value
Trang 190Math.random()
Trang 191Math.random()
Trang 192pickGreeting : List String -> String
Trang 193pickGreeting : List String -> String
Trang 194pickGreeting : List String -> Cmd Msg
pickGreeting : List String -> String
Trang 195Elm Runtime
Html
Trang 198Msg
Elm Runtimehow the Elm Runtime talks to update Html Msg
Trang 201update : Msg -> Model -> Model
Trang 202update : Msg -> Model -> ( Model, Cmd Msg )
Trang 204pickGreeting : List String -> Cmd Msg
( model, pickGreeting greetings )
Model
Trang 206another function guarantee
Trang 207no side effects
Trang 208no modifying external state
Trang 209fire-and-forget HTTP POST
Trang 210fire-and-forget HTTP POST
✓ same arguments, same result
Trang 211fire-and-forget HTTP POST
✗ does not modify external state
✓ same arguments, same result
Trang 212side effects
Trang 213managed effects
Trang 214Cmd
Trang 215Http.getString
Trang 216getString : String -> Cmd String
what if it looked like this?
Trang 217getString : String -> Cmd String
what if it looked like this?
it always produces a string?
Trang 218getString : String -> Task Http.Error String
Trang 219result if it fails
Trang 220getString : String -> Task Http.Error String
result if it succeeds
Trang 221Task Cmd
Trang 222Task Cmd
Trang 223Task Cmd
Trang 224(\err -> ShowError err)
(\json -> SetJson json)
(Http.getString "https://api.github.com?q=blah")
Task Cmd
Trang 225translate failure into a Msg
translate success into a Msg
Trang 226Http.getString "https://api.github.com?q=blah"
|> Task.perform ShowError SetJson
Task Cmd
translate failure into a Msg
translate success into a Msg
Trang 227translate failure into a Msg
translate success into a Msg
Trang 228success gives you a String
Http.getString url
Trang 229success gives you a String
Http.getString url
success gives you a SearchResult
Http.get searchResultDecoder url
Trang 230Exercise: resolve the TODOs in part7/Main.elm
cmd : Cmd Msg
cmd =
Http.getString "https://api.github.com?q=blah"
|> Task.perform ShowError SetJson
success gives you a SearchResult
Http.get searchResultDecoder url
Trang 231day 1 wrap
Trang 232Slack: elmlang.herokuapp.com
Helpful Resources
Weekly News: elmweekly.nl
Google Group: elm-discuss