Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Reactive with Elm

suvash
December 16, 2015

Getting Reactive with Elm

An introductory talk on Elm ( http://elm-lang.org/ ) presented at an event in Gothenburg ( http://www.meetup.com/got-lambda/events/226630957/ )

suvash

December 16, 2015
Tweet

Other Decks in Programming

Transcript

  1. BEFORE WE BEGIN Thanks RapidImages for hosting ! Got.λ Chat

    room (Login with Github) https://gitter.im/got-lambda/expression Please feel free to interrupt.
  2. When I started working on the project that would become

    Elm, HTML was about 20 years old and people still had to read three blog posts and five Stack Overflow questions to figure out how to vertically center things. My initial goal with Elm was to rethink GUIs from scratch. What would web programming look like if we could restart? - Evan Czaplicki
  3. IMMUTABLE DATA age = age + 1 ❌ new_age =

    age + 1 ✅ Data is always transformed to new data and/or appended*, BUT never updated-in-place * Look into Persistent/Immutable Data structures to understand how this can be efficient.
  4. PURE FUNCTIONS Same result for same inputs, forever ! No

    side effects CAT 54B8617E CA0E54C7 D3C8E673 2C6B687A EXAMPLE : HASHING FUNCTION* * Hashing functions do have collisions, but that's not the point here.
  5. SIDE EFFECTS AND IMPURE FUNCTIONS CAT SOMETHING THAT PROBABLY CHANGES

    EVERY SINGLE TIME X = HASH "CAT" X = X + {{ TODAY }} UPDATE DIV WITH X HTTP POST X WRITE X TO LOCALSTORAGE DISPLAY "HI" X
  6. JUST GO AND BAKE A WHOLE PIZZA, FROM BEGINNING TO

    THE END IT MIGHT COME OUT A BIT DIFFERENT EVERY SINGLE TIME, BUT IT'S PROBABLY ALRIGHT. MANAGED SIDE EFFECTS WHEN BAKING A PIZZA LIST ALL THE INGREDIENTS AND PREPARE A RECIPE THIS WILL NOT CHANGE FOR A PARTICULAR CAKE. LET THE "ROBOT CHEF" BAKE YOUR RECIPE IT AS IT WANTS. NOT REPRODUCIBLE, BECAUSE "EACH PIZZA IS DIFFERENT" RECIPE IS REPRODUCIBLE, 'IF' YOU TRUST THE ROBOT CHEF.
  7. JUST GO AND WRITE THE WHOLE PROGRAM, INCLUDING SIDE EFFECTS

    AND ALL. IT MIGHT COME OUT A BIT DIFFERENT EVERY SINGLE TIME, BUT IT'S PROBABLY ALRIGHT MANAGED SIDE EFFECTS LIST ALL THE SIDE-EFFECTING THINGS YOU WANT TO BE DONE. THIS WILL NOT CHANGE FOR A PARTICULAR "OPERATION". WHEN WRITING A BROWSER-BASED APPLICATION LET THE "ELM RUNTIME" RUN YOUR OPERATION AS IT WANTS. PURITY IS NOT REALLY AN OPTION. WRITE PURE FUNCTIONS, & ELM PERFORMS EFFECTS
  8. any questions so far ? NOW ON TO MORE "REACTIVE"

    THINGS. sometimes, it's easiest to learn a different way by letting your expertise and opinions rest a bit.
  9. WHAT ARE SIGNALS ? -80 -63 -36 -60 -25 TIME

    WIFI Strength Signal (in dBm) THINK OF SIGNAL AS AN "IDEA" THAT REPRESENTS SOME VALUE(S) THAT CAN CHANGE OVER TIME.
  10. MORE EXAMPLES OF SIGNALS NORDSTAN RÖDA STEN ERIKS BERG TIME

    Signal of your location AVENYN JÄRN TORGET (10,40) (780,120) (245,190) TIME Signal of your mouse location (60,300) (560,124)
  11. MORE EXAMPLES OF SIGNALS A Movie (Signal of frames) A

    Dynamic Web application (Signal of HTML/...)
  12. ... SIGNALS if there's one thing you understand after this

    talk, please make it .. while not important at all when beginning Elm (Thanks StartApp), they're really at the foundations of what Elm is all about.
  13. REACTING TO SIGNALS Define a function over the signal. Every

    time the value in the signal changes, the function gets 'executed'. This the what it means to "react" to the signal. Now you might be thinking "ah ! event listeners / observables / subscribers / ... ", but hold on to that thought for now.
  14. REACTING TO SIGNALS ...CONTINUED Map over a Signal to produce

    a Signal Filter over a Signal to produce a Signal Merge multiple Signals into one Signal Foldp (Fold from the past) Signal into a Signal
  15. REACTING TO SIGNALS ...MAP 97 65 Keyboard.presses a A characters

    = Signal.map Char.fromCode Keyboard.presses False True pressedUpper = Signal.map Char.isUpper characters
  16. REACTING TO SIGNALS ...FILTER 97 65 Keyboard.presses a A characters

    = Signal.map Char.fromCode Keyboard.presses Z* A pressedUppercharacters = Signal.filter Char.isUpper 'Z' characters * Empty/Null Signals are not possible with Elm(and what would it mean for a signal to be empty), hence filtered Signals always need a default value.
  17. REACTING TO SIGNALS ...MAP2 320 90 Up Window.height Signal.map2 showUpOrDown

    Window.height Mouse.Y 12 65 Mouse.Y showUpOrDown wH mY = 
 let upDown =
 if mY < wH // 2 then "Up" else "Down"
 in
 show upDown Down
  18. REACTING TO SIGNALS ...FOLDP ( ) ( ) 0 1

    Mouse.clicks state = Signal.foldp (\click count -> count + 1) 0 Mouse.clicks 2 Foldp (also known as fold from the past) is used to perform operations
 that depends on previous step / past.
 Used heavily to maintain application state. (a signal)
  19. WHAT WE WONT COVER TODAY Time-traveling Debugger Tasks Ports and

    JS Interop Mailboxes & Messages .... MAYBE WE CAN HAVE A SECOND MEETUP !
  20. OTHER PERKS ... (Type)Safe and smooth JS interop. Clean* syntax.

    Time-traveling Debugger and more.... * In a literal sense, devoid of punctuation marks :)
  21. ELM SYNTAX Elm syntax is directly inspired from ML (MetaLanguage).

    If you've looked at any ML, OCaml, Haskell code before
 it should look familiar. The best way to learn the syntax is the online reference at http://elm-lang.org/docs/syntax http://elm-lang.org/docs/from-javascript
  22. THE ELM ARCHITECTURE Application code cleanly separated into 3 main

    parts MODEL UPDATE VIEW APPLICATION -- MODEL type alias Model = { ... } -- UPDATE type Action = Reset | ... update : Action -> Model -> Model update action model = case action of Reset -> ... ... -- VIEW view : Model -> Html view = ...
  23. THE ELM ARCHITECTURE Elm Architecture Get an Action from World

    Update the Model Use view to describe how they should be displayed Messages Mailbox
 (has addresses + messages) Internal Data Structures
 Not strings, templates, etc. ELM figures out how to render efficiently (so you don't have to) Action External
 Input ELM Runtime handles this
  24. THE ELM ARCHITECTURE Why did we talk about Signals before

    ? StartApp is a useful beginners package.
 Avoids the need to directly work with signals for simple applications. FINALLY : Example Code Reading
  25. ELM'S (AND HENCE EVAN'S) HIDDEN AGENDA* To subtly spoil us

    with the idea of declarative programming for building GUIs. * Obviously, my personal opinion :) A declarative language allows you to say what is done, without having to specify exactly how the computer should do it.
  26. MORE THINGS TO EXPLORE Time-traveling Debugger ( http://debug.elm-lang.org/try ) Types

    With and without StartApp Mailboxes Tasks More Signals 2D Graphics/Game (HTML5) Development ....... PRODUCTION !!!