1. Trang chủ
  2. » Giáo Dục - Đào Tạo

elm slides kho tài liệu bách khoa

234 49 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 234
Dung lượng 1,11 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

Trang 1

welcome to elm!

please follow these instructions to get set up:

Trang 2

@rtfeldman

Trang 3

1 Rendering a Page

Trang 4

compiles to

Trang 6

functions

Trang 9

arguments

Trang 10

no parentheses

Trang 12

required

Trang 13

expression

Trang 14

JS: quantity === 1 ? singular : plural

Trang 15

call pluralize passing 3 arguments

Trang 16

call text passing 1 argument

Trang 17

import the Html module

Trang 19

why bother?

Trang 20

comments about comments

Trang 21

this is a comment

Trang 22

{- this is a

block comment -}

Trang 23

Virtual 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 28

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" ] ]

Trang 29

2 Basic Data Structures

Trang 30

strings

Trang 31

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

Trang 32

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

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

Trang 33

toString 5 == "5"

Trang 34

let-expressions

Trang 35

pluralize singular plural quantity =

if quantity == 1 then

singular

else

plural

Trang 36

pluralize singular plural quantity =

Trang 37

pluralize singular plural quantity =

Trang 38

pluralize singular plural quantity =

Trang 39

pluralize singular plural quantity =

let quantityStr = toString quantity

prefix = quantityStr ++ " "

in

if quantity == 1 then

else prefix ++ plural

let-expression

Trang 40

pluralize singular plural quantity =

Trang 41

pluralize singular plural quantity =

Trang 42

Records, Tuples, and Lists

Trang 43

record =

{ 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 51

what does this rule get us?

Trang 52

Records Tuples Lists

Trang 53

Records Tuples Lists

fixed

length

fixed length

variable length

Trang 54

Records Tuples Lists

mixed

contents

mixed contents

uniform contents

fixed

length

fixed length

variable length

Trang 55

Exercise: resolve the TODOs in part2/Main.elm

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

== "99 balloons"

Trang 56

3 Adding Interaction

Trang 57

booleans

Trang 58

TrueFalse

Trang 59

x == y

Trang 60

False

x == y

not (x == y)

Trang 62

partial application

Trang 63

function pluralizeLeaves(quantity) {

return pluralize("leaf", "leaves", quantity);}

Trang 66

function pluralizeLeaf(plural, quantity) {

return pluralize("leaf", plural, quantity);}

pluralizeLeaf plural quantity =

pluralize "leaf" plural quantity

pluralizeLeaf =

pluralize "leaf"

Trang 67

List.filter

Trang 68

isKeepable num = num >= 2

List.filter isKeepable [ 1, 2, 3 ]

== [ 2, 3 ]

Trang 69

Elm: (\foo -> foo + 1)

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

Trang 70

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

== [ 2, 3 ]

isKeepable num = num >= 2

List.filter isKeepable [ 1, 2, 3 ]

== [ 2, 3 ]

Trang 71

List.map

Trang 72

double num = num * 2

Trang 73

double num =

num * 2

List.map double [ 1, 2, 3 ]

Trang 74

double num =

num * 2

List.map double [ 1, 2, 3 ]

== [ 2, 4, 6 ]

Trang 75

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

Trang 76

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

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

Trang 77

The Elm Architecture

model view update

Trang 78

view model =

div [ class "content" ] []

Trang 79

Html

Elm Runtime

Trang 80

Html h1 [] [ text "ElmHub" ]

Elm Runtime

Trang 81

Model

Html h1 [] [ text "ElmHub" ]

Elm Runtime

Trang 84

maxResults 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 87

update msg model =

if msg.operation == "SHOW_MORE" then

{ maxResults = model.maxResults + msg.data }

Trang 88

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

Trang 89

Elm Runtime

view

Trang 90

onClick

Trang 91

{ operation = "SHOW_MORE", data = 10 }

Trang 92

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

Trang 93

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

Trang 95

DELETE_BY_ID

Trang 96

Elm Runtime

Trang 97

view

Trang 98

Exercise: 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 99

4 Annotations

Trang 100

query is a string

query = "tutorial"

Trang 101

query : String

query = "tutorial"

Trang 102

stars : Int

stars = 123

Trang 103

searchResult : { name : String, stars : Int }

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

Trang 104

list : List String

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

Trang 105

list : List Float

list = [ 1.1, 2.2, 3.3 ]

Trang 106

list : List Int

list = [ 1.1, 2.2, 3.3 ]

ERROR!

Trang 108

type alias SearchResult =

{ id : Int

, name : String

, stars : Int

}

Trang 109

type alias Model =

Trang 110

type alias Msg =

{ operation : String , data : Int

}

Trang 111

type alias Msg =

{ operation : String , data : Int

}

view : Model -> Html Msg

view model =

Trang 112

type alias Msg =

{ operation : String , data : Int

}

view : Model -> Html Msg

view model =

Trang 114

view : Model -> Html String

view model =

button [ onClick "RESET" ] [ text "Reset All" ]

Trang 115

view : Model -> Html Float

view model =

button [ onClick 12.34 ] [ text "Reset All" ]

Trang 116

view : Model -> Html Msg

view model =

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

Trang 119

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

Trang 120

elm-repl

Trang 121

Exercise: resolve the TODOs in part4/Main.elm

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

Trang 122

5 Union Types

Trang 123

case-expressions

Trang 124

if msg.operation == "DELETE_BY_ID" then

else if msg.operation == "LOAD_RESULTS" then

else

Trang 126

union types

Trang 127

type Sorting =

Ascending | Descending | Randomized

Trang 128

type Sorting

= Ascending | Descending | Randomized

Trang 129

type Bool = True | False

Trang 132

type Sorting

Trang 135

String -> Sorting

Trang 137

type alias Msg =

{ operation : String , data : Int

}

Trang 138

}

Trang 139

}

{ operation = "SET_QUERY"

, data = "tutorial"

}

Trang 140

type Msg

= SetQuery String | DeleteById Int

type alias Msg =

{ operation : String , data : Int

}

Trang 142

function (String -> Sorting)

Exercise: resolve the TODOs in part5/Main.elm

Trang 143

6 Decoding JSON

Trang 144

in JavaScript

Trang 145

parseInt("42")

Trang 146

parseInt("42") == 42

Trang 147

parseInt("halibut")

Trang 148

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

Trang 149

String.toInt

Trang 150

String.toInt "42"

Trang 151

String.toInt "42" == Ok 42

Trang 152

type Result =

Trang 155

Maybe

Trang 156

type Result =

Trang 158

type Maybe =

Nothing

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

Trang 159

type Maybe =

Nothing

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

Trang 160

pipelines

Trang 161

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

Trang 162

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

Trang 166

decoders

Trang 167

decodeString float "123.45"

Trang 168

== Ok 123.45

Trang 169

decodeString float "123.45"

== Ok 123.45

decodeString float "blah"

Trang 170

decodeString float "123.45"

== Ok 123.45

decodeString float "blah"

== Err "blah is not a float!"

Trang 171

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

Trang 172

== Ok [ 1, 2, 3 ]

Trang 173

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

== Ok [ 1, 2, 3 ]

"[1, 2, 3]"

|> decodeString (list int)

== Ok [ 1, 2, 3 ]

Trang 174

decoding objects into records

Trang 175

makeGameState score playing =

{ score = score, playing = playing }

Trang 176

makeGameState score playing =

{ score = score, playing = playing }decoder =

???

Trang 177

makeGameState score playing =

{ score = score, playing = playing }

decoder =

???

decodeString decoder

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

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

Trang 178

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 }

Trang 179

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 }

Trang 180

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 }

Trang 181

type alias GameState =

{ score : Float, playing : Bool }

makeGameState : Float -> Bool -> GameState

{ score = score, playing = playing }

Trang 182

type alias GameState =

{ score : Float, playing : Bool }

GameState : Float -> Bool -> GameState

makeGameState : Float -> Bool -> GameState

{ score = score, playing = playing }

Trang 183

type alias GameState =

{ score : Float, playing : Bool }

GameState : Float -> Bool -> GameState

makeGameState : Float -> Bool -> GameStatemakeGameState score playing =

{ score = score, playing = playing }

Trang 184

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 }

Trang 185

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 }

Trang 186

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

decoder = decode GameState

|> required "score" float |> required "playing" boolExercise: resolve the TODOs in part6/Main.elm

Trang 187

7 Client-Server Communication

Trang 188

function guarantees

Trang 189

same arguments?

same return value

Trang 190

Math.random()

Trang 191

Math.random()

Trang 192

pickGreeting : List String -> String

Trang 193

pickGreeting : List String -> String

Trang 194

pickGreeting : List String -> Cmd Msg

pickGreeting : List String -> String

Trang 195

Elm Runtime

Html

Trang 198

Msg

Elm Runtimehow the Elm Runtime talks to update Html Msg

Trang 201

update : Msg -> Model -> Model

Trang 202

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

Trang 204

pickGreeting : List String -> Cmd Msg

( model, pickGreeting greetings )

Model

Trang 206

another function guarantee

Trang 207

no side effects

Trang 208

no modifying external state

Trang 209

fire-and-forget HTTP POST

Trang 210

fire-and-forget HTTP POST

✓ same arguments, same result

Trang 211

fire-and-forget HTTP POST

✗ does not modify external state

✓ same arguments, same result

Trang 212

side effects

Trang 213

managed effects

Trang 214

Cmd

Trang 215

Http.getString

Trang 216

getString : String -> Cmd String

what if it looked like this?

Trang 217

getString : String -> Cmd String

what if it looked like this?

it always produces a string?

Trang 218

getString : String -> Task Http.Error String

Trang 219

result if it fails

Trang 220

getString : String -> Task Http.Error String

result if it succeeds

Trang 221

Task Cmd

Trang 222

Task Cmd

Trang 223

Task Cmd

Trang 224

(\err -> ShowError err)

(\json -> SetJson json)

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

Task Cmd

Trang 225

translate failure into a Msg

translate success into a Msg

Trang 226

Http.getString "https://api.github.com?q=blah"

|> Task.perform ShowError SetJson

Task Cmd

translate failure into a Msg

translate success into a Msg

Trang 227

translate failure into a Msg

translate success into a Msg

Trang 228

success gives you a String

Http.getString url

Trang 229

success gives you a String

Http.getString url

success gives you a SearchResult

Http.get searchResultDecoder url

Trang 230

Exercise: 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 231

day 1 wrap

Trang 232

Slack: elmlang.herokuapp.com

Helpful Resources

Weekly News: elmweekly.nl

Google Group: elm-discuss

Ngày đăng: 16/11/2019, 21:02

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm