$30 off During Our Annual Pro Sale. View Details »

Elm Development with front end

Avatar for hp hp
March 30, 2017

Elm Development with front end

Initial meetup lambda PT (Braga), development with ELM

Avatar for hp

hp

March 30, 2017
Tweet

Other Decks in Programming

Transcript

  1. ELM |> Front end |> Development Digisensei Helder Pinto GH:

    @helderjnpinto / LI: linkedin.com/in/helderjnpinto || GH: @Digisensei
  2. null undefined - is not a function impuro e mutável

    write lots of tests for a tiny web app
  3. elm

  4. Elm Elm is better …. No null. No runtime errors

    in practice. No undefined is not a function. The Compiler and Debugger is great! http://debug.elm-lang.org/
  5. Elm • Functional Language • Haskell based • Strongly typed

    • Front end oriented • Compiles to Js • Very fast, Elm uses "Virtual-dom" combining Pure Functions and Immutable data.
  6. Elm Strongly typed ??? JavaScript : Elm: '1' + 1

    // o resultado será '11' '1' + true // o resultado será 1true
  7. Elm Stateless / Pure functions Return Value based on Input

    parameters and don’t cause side effects
  8. Elm Rules for Pure functions 2 – Must not use

    State! var count = 0; function myFunction (x) { var localCount = count + 1; ...
  9. Elm Rules for Pure functions 3 – Must return a

    value based on parameters! function myFunction (x) { return x + 1; }
  10. Elm Rules for Pure functions 4 – Must not cause

    side effects! function myFunction (x) { Document.getElementById(“item”).innerText = “Kill ALL ”; send_random_rockets (x);// Just return x+1; }
  11. Elm Immutable data Elm use immutable data: - Better for

    processing (ex. Diff with Virtual Dom ) - Code is easier to understand in general. - Thread-safe (ex. multiple threads cannot corrupt the state ) - Compiler can check the type of data even without notation. - No generic map, apply, fold, or filter type functions. Instead it uses names with the prefix of its module as List.map, Dict.filter.
  12. Elm Type of data There are as in most languages:

    Int , String, Float, Char, ... Most used: Lists, Dictionaries, Tuples, Records, Union Types Other structures : Arrays, Set
  13. Elm Functions greet : String -> String greet name =

    “Howdy” ++ “ ” ++ name ++ “ !” greet “Samurai” -- output Howdy Samurai ! Identifier Parameter(s) Return type
  14. Elm Some examples of code (Tuples) ninja = (“katana”, 23

    ) Tuple.first ninja -- output “katana” Tuple.second ninja -- output 23 Tuple
  15. Elm (Records & Union Types) type alias Ninja = {

    name : String, age : Int, experience : NinjaLevel } ninja = Ninja “katana” 23 Junior ninja.name -- output “Katana” Record type NinjaLevel = Junior | Senior | Guru | Ninja Union Type type Answer = Yes | No | Other String
  16. Elm Currying “Creating Building Blocks” ex: add a b =

    a + b add 2 3 = 5 add1 = add 2 result = add1 3 -- output 5 ( 3) -- output 5 (add 2)
  17. Elm Pipe operator |> SUPER COMBO <| Currying add a

    b = a + b isEven a = a % 2 == 0 increment1 = add 1 [1,2,3,4,5] |> List.map increment1 |> List.filter isEven |> toString -- output [2,4,6] toString <| List.filter isEven <| List.map increment1 <| [1,2,3,4,5]
  18. Elm Immutable ninja = Ninja “Katana” 23 Junior older_ninja =

    { ninja | age = 40, experience = Guru } ninja.age -- 23 older_ninja.age -- 40
  19. Elm Null ~= Maybe a type Maybe a = Just

    a Nothing [ 1, 2, 3, 4 ] |> List.head |> toString
  20. Elm Pattern Matching with “case of” type alias Ninja =

    { name : String, age : Int, experience : NinjaLevel weapon : Maybe Weapon } type Weapon = Yari | Sword | Cross-bow ninja1 = Ninja “Katana” 3 Junior Nothing ninja2 = Ninja “Azuka” 90 Guru Yari
  21. Elm Pattern Matching with “case of” case ninja.weapon of Just

    weapon -> case weapon of Yari -> “...” Sword -> “...” _ -> “Default: ” ++ (toString weapon) Nothing -> “No weapon ...” type Weapon = Yari | Sword | Cross-bow
  22. Elm Tools Oficiais: - elm-repl (read–eval–print loop) - elm-package (Module

    manager and versions “based on file ( elm-package.json )”) - elm-make ( Elm compiler) Comunidade: - elm-install ( Install modules directly from github and official ) - elm-format ( Code formatter ) - ellie-app (online editor, https://ellie-app.com/new ) - Html to Elm (atom plugin, online : https://mbylstra.github.io/html-to-elm/) - elm-live ( A flexible dev server for Elm. Live reload included. https://git.io/elm-live )
  23. Elm Elm Architecture • Model — the state of your

    application • Update — a way to update your state • View — a way to view your state as HTML • Commands — A Cmd lets you do stuff: generate a random number, send an HTTP request, etc. • • Subscriptions — A Sub lets you register that you are interested in something: tell me about location changes, listen for web socket messages, Mouse, Keyboard ,etc.
  24. Elm Elm Architecture (The Basic Pattern) import Html exposing (..)

    -- MODEL type alias Model = { ninja } http://package.elm-lang.org/packages/elm-lang/html/latest This uses Virtual Dom Maybe a Record, Integer, String, … etc
  25. Elm Elm Architecture (The Basic Pattern) -- UPDATE type Msg

    = Reset | ... update : Msg -> Model -> Model update msg model = case msg of Reset -> ... ... Pattern matching for all Msg Send to elm runtime as the changes it has to do
  26. Elm Elm Architecture (The Basic Pattern) -- VIEW view :

    Model -> Html Msg view model = div [ id “divNinja”, class “ninja-name”, onClick Reset ] [ text (model.ninja).name ] ... List of Html.Attributes List of child elements
  27. Elm Elm Architecture (The Basic Pattern) main = Html.Program {

    model = initialModel , view = view , update = update } Record with basic Elm web app
  28. Elm JavaScript Interop Elm can talk to JS, using PORTS.

    Works like a client and server “websockets”. Elm listen to changes in JavaScript subscription port and vice versa.
  29. Elm Embed in HTML elm-make src/Main.elm --output= main.js <div id="main"></div>

    <script src="main.js"></script> <script> var node = document.getElementById('main'); var app = Elm.Main.embed(node); // Note: if your Elm module is named "MyThing.Root" you // would call "Elm.MyThing.Root.embed(node)" instead. </script>