Slide 1

Slide 1 text

Let's learn Elm! 2015/07/01 @Worksap ATE Yosuke Torii

Slide 2

Slide 2 text

This slide is about... - What is Elm? - Why is Elm great? - For web-app developers × Type systems × Useful tools/libraries × For game developers × For FP developers

Slide 3

Slide 3 text

What's Elm? - FRP(Functional Reactive Programming) - Haskell-like Syntax - No runtime errors - Compile to JavaScript main = span [class "welcome-message"] [text "Hello, World!"]

Slide 4

Slide 4 text

a = 1 b = 1 c = a + b print c // => 2 Reactive Programming

Slide 5

Slide 5 text

a = 1 b = 1 c = a + b print c // => 2 b = 2 print c // => ? Reactive Programming

Slide 6

Slide 6 text

Reactive Programming a b c + 1 1 2

Slide 7

Slide 7 text

Reactive Programming a b c + 1 2 3

Slide 8

Slide 8 text

Reactive Programming a b c + 2 2 4

Slide 9

Slide 9 text

a = model(1) b = model(1) updateC = function() { c = a.value + b.value } a.onChange(updateC) b.onChange(updateC) updateC() I know the observer pattern!

Slide 10

Slide 10 text

Functional Reactive Programming 2 1 Time a Signal : function of time

Slide 11

Slide 11 text

Functional Reactive Programming 2 1 2 1 Time Time a b

Slide 12

Slide 12 text

Functional Reactive Programming 2 1 2 1 Time Time 4 2 Time 3 1 a b c

Slide 13

Slide 13 text

Basis of FRP 2 1 4 2 Time Time a b map

Slide 14

Slide 14 text

Basis of FRP 3 1 Time Time a b filter 4 2 4 2

Slide 15

Slide 15 text

Basis of FRP 1 1 Time Time a b scan 1 1 3 1 4 2

Slide 16

Slide 16 text

Basis of FRP 1 1 Time Time a b merge 2 2 Time c 1 1 2 2

Slide 17

Slide 17 text

Basis of FRP 2 1 Time Time a b lift 2 1 Time c 3 1 4 2

Slide 18

Slide 18 text

FRP meets MV* architecture Action Model View
Hello, John Smith!
John Smith John Smith keydown keydown

Slide 19

Slide 19 text

FRP meets MV* architecture MV* FP Elm AngularJS Backbone.js React.js Ember.js Haskell Scala OCaml LISP Erlang

Slide 20

Slide 20 text

Trends of front-end MV* View Model Controller update() render()

Slide 21

Slide 21 text

Trends of front-end MV* View Model Model Model View View View View View

Slide 22

Slide 22 text

Trends of front-end MV* View Model Model Model View View View View View update()

Slide 23

Slide 23 text

React's Virtual DOM VDOM Model Model Model DOM DOM DOM DOM VDOM VDOM VDOM

Slide 24

Slide 24 text

React's Virtual DOM Model Model DOM DOM DOM DOM VDOM VDOM VDOM VDOM Model update() render()

Slide 25

Slide 25 text

React's Virtual DOM Model Model DOM DOM VDOM VDOM VDOM VDOM Model update() render() DOM DOM diff exists diff exists

Slide 26

Slide 26 text

Declarative programming comes back! render: function() { return (

TODO

{'Add #' + (this.state.items.length + 1)}
); }

Slide 27

Slide 27 text

Frameworks that uses Virtual DOM - Mercury - Mithril - Cape.JS - Om - Elm

Slide 28

Slide 28 text

Immutability makes Virtual DOM faster! Old Model DOM Old VDOM New Model DOM New VDOM diff diff

Slide 29

Slide 29 text

Flux, an architecture by facebook https://github.com/facebook/flux

Slide 30

Slide 30 text

Flux, an architecture by facebook https://github.com/facebook/flux one-way data flow

Slide 31

Slide 31 text

Flux libraries - Fluxxor - Fluxible(by Yahoo) - RefluxJS - McFly - Delorean

Slide 32

Slide 32 text

FRP libraries - Rx(C#) - RxJava(Java) - RxJS(JavaScript) - Bacon.js(JavaScript) - Sodium(Haskell) - Reative-banana(Haskell)

Slide 33

Slide 33 text

FRP libraries (RxJava) http://reactivex.io/RxJava/javadoc/rx/Observable.html

Slide 34

Slide 34 text

FRP libraries (RxJava) http://reactivex.io/RxJava/javadoc/rx/Observable.html

Slide 35

Slide 35 text

React + X Type-safety Architecture Flux Immutability Virtual DOM Reactivilty - Flow - Bacon.js - RxJS - Fluxible - Fluxxor + α - Immutable.js

Slide 36

Slide 36 text

React + X Type-safety Architecture Flux Immutability Virtual DOM Reactivilty + α ✓ ✓ ✓ ✓

Slide 37

Slide 37 text

About Elm - born in Apr 2012 - ver 0.15 - designed by Evan Czaplicki (Prezi) - http://elm-lang.org/ - https://groups.google.com/forum/#! forum/elm-discuss

Slide 38

Slide 38 text

About Elm 0.15 Apr 2015 Tasks, better HTTP library 0.14 Dec 2014 Package manager, parallel builds, JSON --- Jul 2014 elm-html http://evancz.github.io/elm-todomvc/ Graphics.Element Html http://elm-lang.org/examples/adventure

Slide 39

Slide 39 text

Hello, world! import Html exposing (..) main : Html main = text "Hello, World!" text : String → Html

Slide 40

Slide 40 text

Hello, world! import Html exposing (..) import Html.Attributes exposing (..) main : Html main = ul [class "grocery-list"] [ li [] [text "Pamplemousse"] , li [] [text "Ananas"] , li [] [text "Jus d'orange"] ] ul : List Attribute → List Html → Html li : List Attribute → List Html → Html class : String → Attribute

Slide 41

Slide 41 text

Signals import Html exposing (..) import Mouse main : Signal Html main = Signal.map (\p -> text (toString p)) Mouse.position Mouse.position : Signal (Int, Int) Signal.map : (a → b) → Signal a → Signal b toString : a → String

Slide 42

Slide 42 text

Signals (10, 20) Time main : Signal Html (15, 30) position : Signal (Int, Int) “(10, 20)” Time “(15, 30)” map

Slide 43

Slide 43 text

Signals import Html exposing (..) import Mouse main : Signal Html main = Signal.map (\count -> text (toString count)) countClick countClick : Signal Int countClick = Signal.foldp (\clk count -> count + 1) 0 Mouse.clicks Mouse.clicks : Signal () Signal.foldp : (a → b → b) → b → Signal a → Signal b

Slide 44

Slide 44 text

Signals () Time countClick : Signal Int clicks : Signal (Int, Int) Time foldp () () 1 2 3

Slide 45

Slide 45 text

You might think FRP looks like...

Slide 46

Slide 46 text

You might think FRP looks like... keydown timestamp errors fullname ajax formData firstName lastName enter letter tabIndex errorCount ok messages html click submit ok error reset inputs

Slide 47

Slide 47 text

Actually?

Slide 48

Slide 48 text

Actually? action model html

Slide 49

Slide 49 text

The Elm Architecture - model - update - view http://evancz.github.io/elm-todomvc/ http://elm-lang.org/examples/adventure

Slide 50

Slide 50 text

The Elm Architecture Model View Action Update Mailbox - model - update - view

Slide 51

Slide 51 text

The Elm Architecture -- MODEL type alias Model = { ... } -- UPDATE type Action = Reset | ... update : Action -> Model -> Model update action model = case action of Reset -> ... ... -- VIEW view : Model -> Html view model = ...

Slide 52

Slide 52 text

Code reading http://evancz.github.io/elm-todomvc/

Slide 53

Slide 53 text

Model -- MODEL type alias Model = { tasks : List Task , field : String , uid : Int , visibility : String } type alias Task = { description : String , completed : Bool , editing : Bool , id : Int }

Slide 54

Slide 54 text

Model newTask : String -> Int -> Task newTask desc id = { description = desc , completed = False , editing = False , id = id } emptyModel : Model emptyModel = { tasks = [] , visibility = "All" , field = "" , uid = 0 }

Slide 55

Slide 55 text

Update -- UPDATE type Action = NoOp | UpdateField String | EditingTask Int Bool | UpdateTask Int String | Add | Delete Int | DeleteComplete | Check Int Bool | CheckAll Bool | ChangeVisibility String

Slide 56

Slide 56 text

Update update : Action -> Model -> Model update action model = case action of NoOp -> model Add -> { model | uid <- model.uid + 1, field <- "", tasks <- if String.isEmpty model.field then model.tasks else model.tasks ++ [newTask model.field model.uid] } Delete id -> { model | tasks <- List.filter (\t -> t.id /= id) model.tasks } Check id isCompleted -> let updateTask t = if t.id == id then { t | completed <- isCompleted } else t in { model | tasks <- List.map updateTask model.tasks } ...

Slide 57

Slide 57 text

View view : Address Action -> Model -> Html view address model = div [ class "todomvc-wrapper" , style [ ("visibility", "hidden") ] ] [ section [ id "todoapp" ] [ lazy2 taskEntry address model.field , lazy3 taskList address model.visibility model.tasks , lazy3 controls address model.visibility model.tasks ] , infoFooter ]

Slide 58

Slide 58 text

View taskEntry : Address Action -> String -> Html taskEntry address task = header [ id "header" ] [ h1 [] [ text "todos" ] , input [ id "new-todo" , placeholder "What needs to be done?" , autofocus True , value task , name "newTodo" , on "input" targetValue (Signal.message address << UpdateField) , onEnter address Add ] [] ]

Slide 59

Slide 59 text

Main main : Signal Html main = Signal.map (view actions.address) model model : Signal Model model = Signal.foldp update initialModel actions.signal actions : Signal.Mailbox Action actions = Signal.mailbox NoOp

Slide 60

Slide 60 text

Good architecture makes language simple! Language Architecture ×

Slide 61

Slide 61 text

Good architecture makes language simple! I want some function, but the core library doesn't support it! Q:

Slide 62

Slide 62 text

Good architecture makes language simple! Is that really required? Maybe your system can be written more simply. A:

Slide 63

Slide 63 text

No unnecessary features! Keep it simple. Please support monad! Q: No. A: Really need type classes! Q: No. A: Higher-kinded types... Q: No. A:

Slide 64

Slide 64 text

Static graphs only Signal (Signal a)

Slide 65

Slide 65 text

Static graphs only https://www.youtube.com/watch?v=Agu6jipKfYw

Slide 66

Slide 66 text

Larger application

Slide 67

Slide 67 text

inputView buttonView Nesting components Model View Action Mailbox

Slide 68

Slide 68 text

inputView buttonView inputAction buttonAction Nesting components Model View Action Mailbox inputModel buttonModel

Slide 69

Slide 69 text

Nesting components type ButtonAction = Click (Int, Int) | Hover Float | NoOp type ContainerAction = Button ButtonAction | Checkbox CheckboxAction | Inputbox InputboxAction | NoOp Button.elm Main.elm

Slide 70

Slide 70 text

Nesting components type alias ContainerModel = { button : ButtonModel , input : InputModel } init : ContainerModel init = { button = Button.init , input = Input.init } Main.elm

Slide 71

Slide 71 text

Nesting components containerAddress : Address ContainerAction buttonAddress : Address ButtonAction buttonAddress = Signal.forwardTo containerAddress Button checkboxAddress : Address CheckboxAction checkboxAddress = Signal.forwardTo containerAddress Checkbox inputboxAddress : Address InputboxAction inputboxAddress = Signal.forwardTo containerAddress Inputbox Main.elm

Slide 72

Slide 72 text

Stateful components? 2 3 buttonA buttonB click +1

Slide 73

Slide 73 text

Stateful components? updateButton : ButtonAction -> ButtonState -> ButtonState updateButton action state = case action of Click -> { state | count <- state.count + 1 } update : Action -> Model -> Model update action model = case action of ButtonA a -> { model | buttonA <- updateButton a model.buttonA } ButtonB b -> { model | buttonB <- updateButton b model.buttonB } Main.elm Button.elm

Slide 74

Slide 74 text

Is Elm ready for production?

Slide 75

Slide 75 text

Is Elm ready for production? Yes! But needs more libraries for productivity. type Action = Wait | Contribute

Slide 76

Slide 76 text

Conclusion - Elm is a FRP language - Elm also has a good architecture - Elm is ready to use

Slide 77

Slide 77 text

Thank you!