Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Elm 0.17 at Dublin Elm Meetup May 2016

228 views

Published on

Slides from my talk about Elm 0.17 at the Dublin Elm Meetup.

Source: https://github.com/micktwomey/elm-0.17-dublin-elm-may-2016

Meetup page: http://www.meetup.com/Elm-User-Group-Dublin/events/230200405/

Published in: Technology
  • Be the first to comment

Elm 0.17 at Dublin Elm Meetup May 2016

  1. 1. Elm 0.17 Michael Twomey @ Elm Dublin May 2016 https://github.com/micktwomey/ elm-0.17-dublin-elm-may-2016
  2. 2. What is Elm?
  3. 3. Really Short Introduction Elm is a functional language that compiles to JavaScript. Noted for helpful compiler errors. ML style language.
  4. 4. Basic Example (Pardon the formatting to fit)
  5. 5. import Html import Html.App main = Html.App.beginnerProgram { model = model , update = update , view = view } type alias Model = { message : String} model = { message = "Hello World!" } type Msg = Nothing update msg model = model view model = Html.h1 [] [ Html.text model.message ]
  6. 6. The Pattern 1. Model 2. Update 3. View (AKA Redux in ReactJS land)
  7. 7. What's New in 0.17?
  8. 8. No More Signals ! foldp and mailboxes are gone too Subscriptions are the new hotness
  9. 9. Subscription Example
  10. 10. import Html import Html.App import Time main = Html.App.program { init = init, update = update , view = view , subscriptions = subscriptions } type alias Model = { time : Time.Time} init = ( { time = 0 }, Cmd.none) type Msg = Tick Time.Time update msg model = case msg of Tick newTime -> ( { model | time = newTime } , Cmd.none) subscriptions : Model -> Sub Msg subscriptions model = Time.every Time.second Tick view model = Html.h1 [] [ Html.text ("The time is " ++ toString model.time) ]
  11. 11. Just the Subscription Bits
  12. 12. main = Html.App.program { ... , subscriptions = subscriptions } subscriptions : Model -> Sub Msg subscriptions model = Time.every Time.second Tick update : Msg -> Model -> Model update msg model = case msg of Tick newTime -> ... SomeActionFromUser -> ...
  13. 13. SVG Version (from Elm docs)
  14. 14. main = Html.App.program { init = init, update = update , view = view , subscriptions = subscriptions } type alias Model = Time.Time init = ( 0 , Cmd.none) update msg model = case msg of Tick newTime -> ( newTime , Cmd.none) type Msg = Tick Time.Time subscriptions model = Time.every Time.second Tick view model = let angle = turns (Time.inMinutes model) handX = toString (50 + 40 * cos angle) handY = toString (50 + 40 * sin angle) in svg [ viewBox "0 0 100 100", width "300px" ] [ circle [ cx "50", cy "50", r "45", fill "#0B79CE" ] [] , line [ x1 "50", y1 "50", x2 handX, y2 handY, stroke "#023963" ] [] ]
  15. 15. The only difference is the view
  16. 16. -- HTML view model = Html.h1 [] [ Html.text ("The time is " ++ toString model.time) ] -- SVG view model = let angle = turns (Time.inMinutes model) handX = toString (50 + 40 * cos angle) handY = toString (50 + 40 * sin angle) in svg [ viewBox "0 0 100 100", width "300px" ] [ circle [ cx "50", cy "50", r "45", fill "#0B79CE" ] [] , line [ x1 "50", y1 "50", x2 handX, y2 handY, stroke "#023963" ] [] ]
  17. 17. More 0.17 Changes
  18. 18. No more Signal.Address (simpler code) -- 0.16 view : Signal.Address Action -> Model -> Html view address model = div [] [ button [ onClick address Decrement ] [ text "-" ] , div [ countStyle ] [ text (toString model) ] , button [ onClick address Increment ] [ text "+" ] ] -- 0.17 view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [ countStyle ] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]
  19. 19. → Effects are now Cmd → Action is now Msg → StartApp is now Html.App → Many packages moved into core → Graphics.* now lives in evancz/elm-graphics
  20. 20. Msg Example (clicking buttons)
  21. 21. type Msg = SayHello | SayGoodbye update msg model = case msg of SayHello -> { model | message = "Hello there!" } SayGoodbye -> { model | message = "Goodbye!" } view model = Html.div [] [ Html.button [ Html.Events.onClick SayHello ] [ Html.text "Say Hello!" ] , Html.button [ Html.Events.onClick SayGoodbye ] [ Html.text "Say Goodbye!" ] , Html.h1 [] [ Html.text model.message ] ]
  22. 22. Effect Managers Added Moves the hard work to the library author
  23. 23. Example: Web Sockets
  24. 24. Simple API WebSocket.send "ws://echo.websocket.org" input WebSocket.listen "ws://echo.websocket.org" NewMessage (Effect manager handles connection management, errors, re-connecting for you.)
  25. 25. Websocket Example
  26. 26. import Html exposing (Html, div, text) import Html.App as Html import WebSocket main = Html.program { init = init, view = view , update = update, subscriptions = subscriptions } echoServer = "ws://echo.websocket.org" type alias Model = { input : String , messages : List String } init = ( Model "" [], WebSocket.send echoServer "Hello" ) type Msg = NewMessage String update msg {input, messages} = case msg of NewMessage str -> (Model input (str :: messages), Cmd.none) subscriptions model = WebSocket.listen echoServer NewMessage view model = div [] (List.map viewMessage (List.reverse model.messages)) viewMessage msg = Html.p [] [ text msg ]
  27. 27. Graphics Demoted to External library !
  28. 28. HTML app first, with possible embedded Graphics. Html.toElement is now Element.toHtml. No more Graphics.Input (use HTML forms).
  29. 29. import Color import Html import Html.App import Element import Collage exposing (..) main = Html.App.beginnerProgram { model = model, update = update, view = view } type alias Model = String model = "Hello" type Msg = Nothing update msg model = model view model = Html.div [] [ collage 320 200 [ group [ circle 50 |> filled Color.charcoal , circle 40 |> filled Color.yellow ] ] |> Element.toHtml ]
  30. 30. -- 0.16 view : Model -> Element view reading = collage 320 200 [ group [ circle 50 |> filled Color.charcoal , circle 40 |> filled Color.yellow ] ] -- 0.17 view : Model -> Html Msg view model = Html.div [] [ collage 320 200 [ group [ circle 50 |> filled Color.charcoal , circle 40 |> filled Color.yellow ] ] |> Element.toHtml ]
  31. 31. Graphics Clock Example
  32. 32. Html.div [] [ collage 640 480 [ group [ circle 150 |> filled Color.darkBlue , path [ ( 0, 0 ) , ( handX, handY ) ] |> traced { defaultLine | width = 5 , color = Color.darkCharcoal } ] ] |> toHtml , Html.h1 [] [ Html.text ("The time is " ++ time) ] ]
  33. 33. http://elm-lang.org/blog/farewell-to-frp http://guide.elm-lang.org http://www.lambdacat.com/migrating-from- elm-0-16-to-0-17-from-startapp/ http://noredink.github.io/posts/ signalsmigration.html https://github.com/micktwomey/elm-0.17-dublin- elm-may-2016

×