Slide 1

Slide 1 text

The Power of Elm in JavaScript

Slide 2

Slide 2 text

@franzejr

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Before starting...

Slide 9

Slide 9 text

This presentation is based on

Slide 10

Slide 10 text

open-source code It's not theory.

Slide 11

Slide 11 text

we really enjoy using these things

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

elm

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Why should I care?

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

...many of the common issues that

Slide 21

Slide 21 text

front-end developers have to deal with just don't exist in Elm

Slide 22

Slide 22 text

don't exist

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

How elm can achieve such thing?

Slide 26

Slide 26 text

Errors will happen at compile time

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Compiler as an assistant

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

http://elm-lang.org/blog/compilers-as-assistants

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Strong Static Types

Slide 33

Slide 33 text

No null or undefined

Slide 34

Slide 34 text

Purely functional

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Total immutability state

Slide 37

Slide 37 text

Reactive by design

Slide 38

Slide 38 text

Imperative a = b + c if we change b, a will NOT be updated.

Slide 39

Slide 39 text

Reactive a = b + c if we change b, a will BE updated.

Slide 40

Slide 40 text

Confidence in Programming

Slide 41

Slide 41 text

Me writing Haskell: 30% test coverage. Scala: 60% test coverage. Ruby: 97% test coverage. Guess the order of reliability of code...

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

1. Make changes, any changes. 2. The compiler tells what you missed. 3. Go to 1.

Slide 44

Slide 44 text

No runtime exceptions

Slide 45

Slide 45 text

But I can't use elm now

Slide 46

Slide 46 text

!

Slide 47

Slide 47 text

Bring these things to JavaScript

Slide 48

Slide 48 text

Static Types

Slide 49

Slide 49 text

Flow

Slide 50

Slide 50 text

// @flow import { Record, Map } from "immutable"; import FormType from "./FormType"; import QuestionSubmissionType from "./QuestionSubmissionType"; export default class Model extends Record({ form: new FormType(), submissions: new Map(), currentStep: 0, submitted: false, errors: new Map(), apiKey: "" }) { form: FormType; submissions: Map; currentStep: number; apiKey: string; }

Slide 51

Slide 51 text

// @flow import { Record, Map } from "immutable"; // --> import FormType from "./FormType"; import QuestionSubmissionType from "./QuestionSubmissionType"; export default class Model extends Record({ form: new FormType(), submissions: new Map(), currentStep: 0, submitted: false, errors: new Map(), apiKey: "" }) { form: FormType; submissions: Map; currentStep: number; apiKey: string; }

Slide 52

Slide 52 text

src/output_lib/types/Model.js:9 9: form: new FormType(), ^^^^^^^^ identifier `FormType`. Could not resolve name Found 1 error

Slide 53

Slide 53 text

function decodeForm(data: ApiForm): FormType { // Decode the questions on their own const questions = List(data.questions.map(decodeQuestion)); return 0; return new FormType({ id: data.id, applicationId: data.application.id, completionContent: data.completion_content, sections: List( data.sections.map(section => { return decodeSection(section, questions); }) ), persisted: true }); }

Slide 54

Slide 54 text

➜ formulae_react git:(master) flow status src/output_lib/decoders.js:201 201: return 0; ^ number. This type is incompatible with the expected return type of 198: function decodeForm(data: ApiForm): FormType { ^^^^^^^^ FormType src/output_lib/decoders.js:202 202: return new FormType({ ^ unreachable code Found 2 errors

Slide 55

Slide 55 text

function myFunction() { let a = b; }

Slide 56

Slide 56 text

➜ formulae_react git:(master) ✗ flow status src/output_lib/decoders.js:199 199: let a = b; ^ identifier `b`. Could not resolve name Found 1 error

Slide 57

Slide 57 text

function myFunction() { let a = "hello" * 5; }

Slide 58

Slide 58 text

➜ formulae_react git:(master) ✗ flow status src/output_lib/decoders.js:199 199: let a = "hello" * 5; ^^^^^^^ string. The operand of an arithmetic operation must be a number. Found 1 error

Slide 59

Slide 59 text

function timesTwo(x: number) { return x * 2; } function main() { timesTwo("hello"); }

Slide 60

Slide 60 text

➜ formulae_react git:(master) ✗ flow status src/output_lib/decoders.js:203 203: timesTwo("hello"); ^^^^^^^ string. This type is incompatible with the expected param type of 198: function timesTwo(x: number) { ^^^^^^ number Found 1 error

Slide 61

Slide 61 text

TypeScript

Slide 62

Slide 62 text

Decoders

Slide 63

Slide 63 text

Encode // Decode

Slide 64

Slide 64 text

Convert at the boundaries

Slide 65

Slide 65 text

The boundary between Two Worlds

Slide 66

Slide 66 text

back-end <> front-end

Slide 67

Slide 67 text

const Person = { get: (name: string): Promise => { return api.get(`${name}`).then(resp => decodePersonType(resp.data)); } };

Slide 68

Slide 68 text

import { Record } from 'immutable' export default class PersonType extends Record({ name: '', age: 0, github: '' }) { name: string age: number github: string }

Slide 69

Slide 69 text

type ApiPerson = { name: string, age: number, github: string } decodePersonType(person: ApiPerson) : PersonType{ return new PersonType({ name: person.name, age: person.age, github: person.github }) }

Slide 70

Slide 70 text

All our code now uses our own types

Slide 71

Slide 71 text

You make the conversion between your server data and application data very explicitly.

Slide 72

Slide 72 text

Your app will now recognize weird JSON structures.

Slide 73

Slide 73 text

function decodeForm(data: ApiForm): FormType { // Decode the questions on their own const questions = List(data.questions.map(decodeQuestion)); return new FormType({ id: data.id, applicationId: data.application.id, completionContent: data.completion_content, sections: List( data.sections.map(section => { return decodeSection(section, questions); }) ), persisted: true }); }

Slide 74

Slide 74 text

function encodeForm(form: FormType): ApiForm { return { id: form.persisted ? form.id : undefined, application_id: form.applicationId, completion_content: form.completionContent, sections: form.sections.toArray().map(encodeSection), uuid: form.persisted ? undefined : form.id }; }

Slide 75

Slide 75 text

// @flow import { Record, List } from "immutable"; import SectionType from "./SectionType"; export default class FormType extends Record({ id: "", applicationId: "", sections: List(), completionContent: "", persisted: false }) { id: string; applicationId: string; sections: List; completionContent: string; persisted: boolean; }

Slide 76

Slide 76 text

type ApiForm = { id: number, application: ApiApplication, completion_content: string, sections: Array, questions: Array };

Slide 77

Slide 77 text

It adds one more validation phase in getting your data from your API

Slide 78

Slide 78 text

It turns server JSON into typed immutable records

Slide 79

Slide 79 text

Slide 80

Slide 80 text

Redux elm based architecture People are using Elm and never thought about that

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

import Html exposing (..) -- MODEL type alias Model = { ... } -- UPDATE type Msg = Reset | ... update : Msg -> Model -> Model update msg model = case msg of Reset -> ... ... -- VIEW view : Model -> Html Msg view model = ...

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

http://redux.js.org/docs/introduction/PriorArt.html

Slide 85

Slide 85 text

Redux First Router

Slide 86

Slide 86 text

Route State + Date State = ❤

Slide 87

Slide 87 text

... const routesMap = { HOME: "/", // action <-> url path PRODUCT: "/product/:id" // :id is a dynamic segment }; ...

Slide 88

Slide 88 text

const productsReducer = (state = init(), action = {}) => { switch (action.type) { case "HOME": ... case "PRODUCT": ... ...

Slide 89

Slide 89 text

... const mapDispatchToProps = dispatch => ({ goToProducts: id => dispatch({ type: "PRODUCT", payload: { id: id } }) }); ...

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

Stay tuned!

Slide 92

Slide 92 text

Questions? :)

Slide 93

Slide 93 text

References → https://www.dailydrip.com/ → https://www.dailydrip.com/topics/elm → https://www.dailydrip.com/blog/understanding- the-elm-architecture

Slide 94

Slide 94 text

References → https://www.dailydrip.com/blog/general-aspects- between-elm-and-js → http://slides.com/danielbachler/fearless- refactoring-with-elm#/ → http://elm-lang.org/blog/compilers-as-assistants → https://www.dailydrip.com/blog/using-decoders- in-your-api-the-boundary-between-two- worlds.html