Slide 1

Slide 1 text

Elm Theory & Practice @markwunsch

Slide 2

Slide 2 text

tinyletter.com/wunsch

Slide 3

Slide 3 text

A delightful language for reliable webapps.
 Generate JavaScript with great performance and no runtime exceptions. http://elm-lang.org

Slide 4

Slide 4 text

Study of an Elm Tree John Constable, 1821

Slide 5

Slide 5 text

• Where does Elm come from? • What makes it reliable? • How am I using it? • How can you use it? • Why would you want to do such a thing?

Slide 6

Slide 6 text

Theory

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Where does Elm come from?

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Elm has two major features: 
 (1) purely functional graphical layout and 
 (2) support for Concurrent FRP.

Slide 11

Slide 11 text

Concurrent FRP solves some of FRP’s long- standing efficiency problems: 
 global delays and needless recomputation.

Slide 12

Slide 12 text

Needless recomputation occurs when a function is recomputed even though its input has not changed.

Slide 13

Slide 13 text

Initial Public Release
 v0.3.0 May 29, 2013

Slide 14

Slide 14 text

Functional Reactive Programming

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Functional Reactive Programming (FRP) is a declarative programming paradigm for working with mutable values. FRP recasts mutable values as time-varying values, better capturing the temporal aspect of mutability.

Slide 17

Slide 17 text

Signal α = Time → α

Slide 18

Slide 18 text

What makes it reliable?

Slide 19

Slide 19 text

The core language of Elm aims to provide flexibility and expressiveness, yet only commit to abstractions that can be implemented efficiently in a concurrent system…

Slide 20

Slide 20 text

In Elm, no event occurs at exactly the same moment as another. This ensures that there is a strict ordering of events, and simplifies the mental model for events: events happen one at a time. The exact co- occurrence of events is also extremely uncommon in GUIs which deal primarily with user input. On the time-scale of computers, users are slow enough that user input events really are strictly ordered.

Slide 21

Slide 21 text

http://redux.js.org

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

So is Elm about FRP anymore? No. Those days are over now. Elm is just a functional language that takes concurrency very seriously. And from a user's perspective, 
Elm is just a friendly functional language!

Slide 24

Slide 24 text

Elm is just a friendly functional language!

Slide 25

Slide 25 text

Practice

Slide 26

Slide 26 text

The Elm Architecture

Slide 27

Slide 27 text

module Main exposing (..) import Html exposing (..) main : Program Never Model Msg main = Html.program { init = init , view = view , update = update , subscriptions = subscriptions } type alias Model = {} init : ( Model, Cmd Msg ) init = ( {}, Cmd.none ) type Msg = NoOp update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = ( model, Cmd.none ) subscriptions : Model -> Sub Msg subscriptions model = Sub.none view : Model -> Html Msg view model = Html.text "Hello, world!" B O ILER P LA TE

Slide 28

Slide 28 text

https://gist.github.com/mwunsch/ 99e70f774a9f065bac778aed505d4f9b

Slide 29

Slide 29 text

module Main exposing (..) -- Declare a Main module and expose
 -- everything to the public.
 -- This is a comment, btw. import Html exposing (..) -- Import everything in the Html module.

Slide 30

Slide 30 text

main : Program Never Model Msg main = Html.program { init = init , view = view , update = update , subscriptions = subscriptions }

Slide 31

Slide 31 text

type alias Model = {} init : ( Model, Cmd Msg ) init = ( {}, Cmd.none )

Slide 32

Slide 32 text

type Msg = NoOp update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = ( model, Cmd.none )

Slide 33

Slide 33 text

subscriptions : Model -> Sub Msg subscriptions model = Sub.none

Slide 34

Slide 34 text

view : Model -> Html Msg view model = Html.text "Hello, world!"

Slide 35

Slide 35 text

type alias Model = { greeting : String } init : ( Model, Cmd Msg ) init = ( { greeting = “Hello, Code Driven!” } , Cmd.none ) … view : Model -> Html Msg view model = Html.text model.greeting

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

type alias Model = { greeting : String } init : ( Model, Cmd Msg ) init = ( { greetng = “Hello, Code Driven!” } , Cmd.none )

Slide 38

Slide 38 text

-- TYPE MISMATCH ------------------------------------------------------ Main.elm The definition of `init` does not match its type annotation. 27| init : ( Model, Cmd Msg ) 28| init = 29|> ( { greetng = "Hello, Code Driven!" } 30|> , Cmd.none 31|> ) The type annotation for `init` says it is a: ( Model, Cmd Msg ) But the definition (shown above) is a: ( { greetng : String }, Cmd msg ) Hint: The record fields do not match up. Maybe you made one of these typos? greeting <-> greetng Detected errors in 1 module.

Slide 39

Slide 39 text

How am I using it?

Slide 40

Slide 40 text

Random.generate : ( a → msg ) → Random.Generator a → Cmd msg

Slide 41

Slide 41 text

import Html.Attributes exposing (style) import Html.Events exposing (onClick) import Random

Slide 42

Slide 42 text

type alias Model = { greeting : String , diceRoll : Int } init : ( Model, Cmd Msg ) init = ( { greeting = "Hello, Code Driven!” , diceRoll = 0 } , Cmd.none )

Slide 43

Slide 43 text

type Msg = ButtonClick | Roll Int

Slide 44

Slide 44 text

view : Model -> Html Msg view model = div [ style [ ( "margin", "20px" ) ] ] [ h1 [] [ text model.greeting ] , h2 [] [ text ("You rolled a " ++ (toString model.diceRoll)) ] , button [ style [ ( "font-size", "2em" ) ] , onClick ButtonClick ] [ text "\x1f3b2" ] ]

Slide 45

Slide 45 text

type Msg = ButtonClick | Roll Int update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of ButtonClick -> ( model, Random.generate Roll (Random.int 1 20) ) Roll result -> ( { model | diceRoll = result , greeting = if result >= 20 then "Hello, Crit Driven!" else "Hello, Code Driven!" } , Cmd.none )

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

github.com/mwunsch/hive-city/

Slide 50

Slide 50 text

How can you use it?

Slide 51

Slide 51 text

import Elm from 'react-elm-components' import { Todo } from '../dist/elm/todomvc.js' function render() { return } http://elm-lang.org/blog/how-to-use-elm-at-work

Slide 52

Slide 52 text

Why would you want to do such a thing?

Slide 53

Slide 53 text

Let’s be mainstream! User focused design in Elm
 http://www.elmbark.com/2016/03/16/mainstream-elm-user-focused-design

Slide 54

Slide 54 text

“Easy to reason about. It’s not really clear what that’s going to give me. Was it hard to reason about? I’m a JavaScript programmer, I spend a lot of time reasoning about what my code does because weird stuff can happen.”

Slide 55

Slide 55 text

State & Time https://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey

Slide 56

Slide 56 text

@markwunsch tinyletter.com/wunsch

Slide 57

Slide 57 text

The School of Athens Raphael, 1509–1511

Slide 58

Slide 58 text

^D