script tags inline in the HTML page itself. • Okay, that’s ugly, extract it out into JS files. • Why not use MVC, MVP and whatnot? Let’s do it Rails-style! • Listen for changes using Object.observe maybe, huh? • Object-oriented -- Before and after ES6? // Coffee, Backbone • 2-way data-binding, dirty checking. Yeah, that’s really DIRTY! // Angular v1, Ember • Unidirectional data flow, virtual DOM, immutable data, smart and dumb components, single source of truth… // React + Flux • Uh… Runtime errors. Be polite and just be optionally type-safe. // TypeScript, Reason 5
✓ Strictly Typed ✓ Type Inference ✓ Robust, Expressive, Self-documenting ✓ No Side Effects ✓ Virtual DOM ✓ JS Interop using Ports ✓ Enforced Semantic Versioning ✓ and many more… 9
= (*) x y multiply2 x y = if x == 1 then y else add (multiply2 (x - 1) y) y double number = number * 2 doubleNumbers1 list = List.map (\number -> number * 2) list doubleNumbers2 list = List.map double list 18
n lessThan6 n = n < 6 double n = n * 2 filter1 list = List.map square (List.filter lessThan6 (List.map double list)) filter2 list = list |> List.map double |> List.filter lessThan6 |> List.map square 22
title } export function setTitle(newTitle) { title = newTitle } setTitle("Welcome 2018") post = { title = "Goodbye 2017" } getTittle post = post.title setTitle post title = { post | title = title } newPost = setTitle post "Welcome 2018" post == newPost -- False JS: Impure as it depends on a mutable variable Elm: Everything is immutable and functions are pure 26
= -- logic here /** * Add a tag to a post and return updated post * * @param {String} tag - The tag needs to be added to the post. * @param {Object} post - Current state/version of the post. * @returns {Object} The updated Post after adding specified tag to the post. */ function addTagToPost(tag, post) { // logic here return updatedPost; }
-- <function> : number -> number -> number -> number add1 = add 1 -- <function> : number -> number -> number add3 = add 1 2 -- <function> : number -> number add 1 2 3 -- 6 : number add1 2 3 -- 6 : number add3 3 -- 6 : number 36
, body: String, , excerpt: Maybe String } viewPostExcerpt post = case post.excerpt = Nothing -> div [] [ text (truncatePostBodyToExcerpt post) ] Just excerpt -> div [] [ text excerpt ] 39
to 5.1.0... This is a MAJOR change. ------ Added modules - MINOR ------ Tuple ------ Changes to module Basics - MAJOR ------ Added: never : Basics.Never -> a Removed: fst : (a, b) -> a snd : (a, b) -> b ------ Changes to module Bitwise - MAJOR ------ Added: shiftLeftBy : Int -> Int -> Int shiftRightBy : Int -> Int -> Int shiftRightZfBy : Int -> Int -> Int Removed: shiftLeft : Int -> Int -> Int shiftRight : Int -> Int -> Int shiftRightLogical : Int -> Int -> Int ------ Changes to module Json.Decode - MAJOR ——— […] It is not allowed to push breaking changes to Elm packages unless bumping its MAJOR version. No more surprises in PATCH releases! 45
external code linter. Built-in “elm-format” command is like “go fmt” which formats all Elm files as per the universal standard style guide. The “elm-reactor” command starts a local server to hot-compile, hot-reload and serve the Elm app to ease the development. The “elm-repl” command starts an interactive shell environment to quickly evaluate Elm code. Also, “elm-package”, “elm-make”. 48