Slide 1

Slide 1 text

@elpapapollo / jfairbank Building Web Apps with Elm Jeremy Fairbank Follow instructions here to get setup! github.com/jfairbank/elm-workshop

Slide 2

Slide 2 text

Goals

Slide 3

Slide 3 text

Goals • Benefits of Elm

Slide 4

Slide 4 text

Goals • Benefits of Elm • Elm Syntax

Slide 5

Slide 5 text

Goals • Benefits of Elm • Elm Syntax • Functional Programming

Slide 6

Slide 6 text

Goals • Benefits of Elm • Elm Syntax • Functional Programming • Build web apps with Elm

Slide 7

Slide 7 text

Format • Some slides. • Coding. • Ask questions! • Code along if you want. - Exercises

Slide 8

Slide 8 text

Follow Along github.com/jfairbank/elm-workshop

Slide 9

Slide 9 text

elm

Slide 10

Slide 10 text

elm • Created in 2012 Evan Czaplicki

Slide 11

Slide 11 text

elm • Created in 2012 Evan Czaplicki • Web and UI focused

Slide 12

Slide 12 text

elm • Created in 2012 Evan Czaplicki • Web and UI focused • Compiles to JavaScript

Slide 13

Slide 13 text

elm • Created in 2012 Evan Czaplicki • Web and UI focused • Compiles to JavaScript • Influences: Haskell, SML, OCaml, F#

Slide 14

Slide 14 text

elm • Created in 2012 Evan Czaplicki • Web and UI focused • Compiles to JavaScript • Influences: Haskell, SML, OCaml, F# • Functional

Slide 15

Slide 15 text

elm • Created in 2012 Evan Czaplicki • Web and UI focused • Compiles to JavaScript • Influences: Haskell, SML, OCaml, F# • Functional • Statically Typed

Slide 16

Slide 16 text

What’s all the fuss about?

Slide 17

Slide 17 text

undefined is not a function

Slide 18

Slide 18 text

No runtime exceptions in practice.

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Fast

Slide 21

Slide 21 text

Fast

Slide 22

Slide 22 text

Fast

Slide 23

Slide 23 text

Functional

Slide 24

Slide 24 text

Expressive and Declarative Concise and intent-revealing

Slide 25

Slide 25 text

Expressive and Declarative Fewer lines of code!

Slide 26

Slide 26 text

function doubleNumbers(numbers) { const doubled = []; const l = numbers.length; for (let i = 0; i < l; i++) { doubled.push(numbers[i] * 2); } return doubled; } doubleNumbers([1, 2, 3]); // [2, 4, 6] ×

Slide 27

Slide 27 text

doubleNumbers list = List.map ((*) 2) list doubleNumbers myList -- [2, 4, 6] ✓

Slide 28

Slide 28 text

Pure Functions Predictable and easier to test

Slide 29

Slide 29 text

add x y = x + y add 2 2 == 4 add 2 2 == 4 add 2 2 == 4

Slide 30

Slide 30 text

Immutable Data No mutation bugs or inconsistent state

Slide 31

Slide 31 text

× conference = { name = "JazzCon" } conference.name = "ConnectJS"

Slide 32

Slide 32 text

conference = { name = "JazzCon" } nextConference = { conference | name = "ConnectJS" } ✓

Slide 33

Slide 33 text

Built-in Framework The Elm Architecture

Slide 34

Slide 34 text

Built-in Framework Unidirectional with organized state changes

Slide 35

Slide 35 text

Update View Model Messages

Slide 36

Slide 36 text

Sold? Let’s learn Elm!

Slide 37

Slide 37 text

Plan 9:00 - 9:15 Intro 9:15 - 9:45 Code: Syntax and Functional Programming 9:45 - 9:55 The Elm Architecture 9:55 - 10:30 Code: Building a Todo List 10:30 - 11:00 Code: Exercise and Break 11:00 - 11:10 HTTP: Commands and Decoders 11:10 - 11:45 Code: Fetching a GitHub User 11:45 - 12:00 Code: Exercise 12:00 - 12:15 Code: Components 12:15 - 12:30 Wrap Up

Slide 38

Slide 38 text

Coding Time! Elm Syntax

Slide 39

Slide 39 text

The Elm Architecture Model-View-Update

Slide 40

Slide 40 text

Controller View Model Model Model Model Model View View View View MVC

Slide 41

Slide 41 text

View Model Model Model Model Model View Model View Model View Model View Model Model Two-way Data Binding

Slide 42

Slide 42 text

Update View Model Messages Unidirectional

Slide 43

Slide 43 text

Update View Model Messages

Slide 44

Slide 44 text

Model Single source of truth

Slide 45

Slide 45 text

Model • String • Int • Record • List • Etc. Any data type

Slide 46

Slide 46 text

Update View Model Messages

Slide 47

Slide 47 text

View Projection of model

Slide 48

Slide 48 text

UI as a Function Model Virtual DOM View

Slide 49

Slide 49 text

elm Virtual DOM Todo List Learn Elm Build awesome Elm apps Learn functional programming

Slide 50

Slide 50 text

Todo List Learn Elm Build awesome Elm apps Learn functional programming Model Delete first todo item

Slide 51

Slide 51 text

View Model Notice missing item in virtual DOM list, so just remove corresponding
  • . elm Virtual DOM Todo List Build awesome Elm apps Learn functional programming
  • Slide 52

    Slide 52 text

    Update View Model Messages

    Slide 53

    Slide 53 text

    Messages Standardized application events

    Slide 54

    Slide 54 text

    elm app model

    Slide 55

    Slide 55 text

    elm app model Commands HTTP Dates Random #’s Subscriptions WebSockets Browser Window Mouse Position Events Text Input Mouse Click

    Slide 56

    Slide 56 text

    elm app model Commands HTTP Dates Random #’s Subscriptions WebSockets Browser Window Mouse Position Events Text Input Mouse Click

    Slide 57

    Slide 57 text

    Update View Model Messages

    Slide 58

    Slide 58 text

    Update Responds to messages and creates new state

    Slide 59

    Slide 59 text

    Update Model New Model Command

    Slide 60

    Slide 60 text

    model Update View elm

    Slide 61

    Slide 61 text

    model Update View elm

    Slide 62

    Slide 62 text

    model Update View elm

    Slide 63

    Slide 63 text

    model Update View elm !

    Slide 64

    Slide 64 text

    model Update View elm !

    Slide 65

    Slide 65 text

    model Update View elm

    Slide 66

    Slide 66 text

    model Update View elm

    Slide 67

    Slide 67 text

    model Update View elm

    Slide 68

    Slide 68 text

    model Update View elm

    Slide 69

    Slide 69 text

    Coding Time! Building a Todo List

    Slide 70

    Slide 70 text

    Exercise(s) • Allow adding new todo items. • Underneath todo list add a button to add a new todo item to the todo list. • The new todo item should have a blank description and should start off in an Editing state. • Hint: you’ll probably want the :: or ++ operators. • Allow deleting todo items. • Add delete button next to edit button. • Hint: editing/saving/completing required List.map. Deletion will require List.filter. • Allow marking a complete todo item as incomplete.

    Slide 71

    Slide 71 text

    Working with HTTP Commands and Decoders

    Slide 72

    Slide 72 text

    Update Model New Model Command

    Slide 73

    Slide 73 text

    Update Model New Model Command ?

    Slide 74

    Slide 74 text

    Side Effects I/O Mutable State

    Slide 75

    Slide 75 text

    Commands Describe side effects instead of doing them

    Slide 76

    Slide 76 text

    HTTP Commands Update Command elm

    Slide 77

    Slide 77 text

    What about the response? Update elm JSON

    Slide 78

    Slide 78 text

    What about the response? Update elm JSON Dynamic Payloads

    Slide 79

    Slide 79 text

    What about the response? Update elm JSON Dynamic Payloads Static Types

    Slide 80

    Slide 80 text

    Decoders Transform arbitrary JSON to static types

    Slide 81

    Slide 81 text

    user : User user = { id = 1 , name = "Jeremy" } { "id": 1, "name": "Jeremy" } Decoder type alias User = { id : Int, name : String }

    Slide 82

    Slide 82 text

    { "id": 1 } type alias User = { id : Int, name : String } Decoder ?

    Slide 83

    Slide 83 text

    { "id": 1 } type alias User = { id : Int, name : String } Decoder ?

    Slide 84

    Slide 84 text

    { "id": 1 } Decoder BadPayload Err

    Slide 85

    Slide 85 text

    { "id": 1 } Decoder BadPayload Err Result type

    Slide 86

    Slide 86 text

    Result type Result error value = Ok value | Err error

    Slide 87

    Slide 87 text

    Result type Result error value = Ok value | Err error

    Slide 88

    Slide 88 text

    Result type Result error value = Ok value | Err error

    Slide 89

    Slide 89 text

    user = { id = 1 , name = "Jeremy" } { "id": 1, "name": "Jeremy" } Decoder Ok

    Slide 90

    Slide 90 text

    { "id": 1, "name": "Jeremy" } “id” int “name” string

    Slide 91

    Slide 91 text

    { "id": 1, "name": "Jeremy" } “id” int “name” string Field decoders

    Slide 92

    Slide 92 text

    { "id": 1, "name": "Jeremy" } “id” int “name” string 1 Ok "Jeremy" Ok

    Slide 93

    Slide 93 text

    1 Ok "Jeremy" Ok User Decoder user = { id = 1 , name = "Jeremy" } Ok

    Slide 94

    Slide 94 text

    1 Ok User Decoder BadPayload Err BadPayload Err

    Slide 95

    Slide 95 text

    Coding Time! Fetching a GitHub User

    Slide 96

    Slide 96 text

    Exercise(s) • Add a loading screen. • Display something like “Fetching user …” • Hint: what state should you update and when? • Display another field or two for the user. • Hint: Remember to update the User type and the userDecoder. • Decode GitHub error messages. • Use login nouser for testing. • Hint: you need to decode response.body in the Http.BadStatus branch. • Hint: you’ll probably want the decodeString and field functions from:
 package.elm-lang.org/packages/elm-lang/core/5.1.1/Json-Decode

    Slide 97

    Slide 97 text

    Coding Time! Components

    Slide 98

    Slide 98 text

    Components vs. Flat Structure

    Slide 99

    Slide 99 text

    Components vs. Flat Structure • Components • Pros • Perfect for independent, reusable applications • Single-page applications • Cons • Nested complexity • More boilerplate • Cumbersome parent-child communication

    Slide 100

    Slide 100 text

    Components vs. Flat Structure • Components • Pros • Perfect for independent, reusable applications • Single-page applications • Cons • Nested complexity • More boilerplate • Cumbersome parent-child communication • Flat Structure • Flatter state • Use function and module helpers • Pros • No parent-child communication ceremony • Easier to follow flow of application • Cons • Larger models • More Msg values

    Slide 101

    Slide 101 text

    Boilerplate But greater guarantees and safety

    Slide 102

    Slide 102 text

    Solving Boilerplate • github.com/halfzebra/create-elm-app • github.com/elm-community/elm-webpack- starter • github.com/gkubisa/elm-app-boilerplate • github.com/splodingsocks/elm-starter

    Slide 103

    Slide 103 text

    create-elm-app

    Slide 104

    Slide 104 text

    Tutorials • www.elm-tutorial.org • guide.elm-lang.org • frontendmasters.com/ courses/elm/

    Slide 105

    Slide 105 text

    Elm in Action manning.com/books/elm-in-action

    Slide 106

    Slide 106 text

    Final Questions?

    Slide 107

    Slide 107 text

    @elpapapollo / jfairbank Thanks! Jeremy Fairbank Code: github.com/jfairbank/elm-workshop Slides: bit.ly/jazzcon17-elm-workshop