shehet gregory · 2016-11-27 · • immutable data structures -> immutable.js • stajc type...

106
So$ware Engineer at Front-End Digest on Shehet Gregory

Upload: others

Post on 07-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

• So$ware Engineer at

• Front-End Digest on

Shehet Gregory

Page 2: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Enjoy your code with ELM

Page 3: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Grammarly

Page 4: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Popular

Page 5: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Write For Everywhere

Page 6: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Write For Everywhere

Page 7: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Write For Everywhere

Page 8: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Write For Everywhere

Page 9: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Applause to JS community

Page 10: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

But…

Page 11: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

History 15 days

Page 12: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Undefined

Page 13: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

function foo(a, b) { if (a > b) { return [a, b] } } foo(1, 2).join(',')

Some JS funcJon

Page 14: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

function foo(a, b) { if (a > b) { return [a, b] } } foo(1, 2).join(',')

Some JS funcJon

Page 15: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Who it works?

Page 16: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity
Page 17: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Use Flow

Page 18: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Add to Git Hooks

Page 19: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Flow Hackfunction foo(a, b) { if (a > b) { return [a, b] } } foo(1, 2).join(',')

Page 20: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Flow Hackfunction foo(a, b):any { if (a > b) { return [a, b] } } foo(1, 2).join(',')

Page 21: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Reality

Page 22: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Evan Czaplicki @evancz ELM

Page 23: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Let’s rewrite to ELM

Page 24: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

foo : Int -> Int -> List Int foo a b = if a > b then a :: b :: [] main = text (String.join "," (foo 1 2))

Some funcJon on ELM

Page 25: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

foo : Int -> Int -> List Int foo a b = if a > b then a :: b :: [] main = text (String.join "," (foo 1 2))

Some funcJon on ELM

Page 26: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

foo : Int -> Int -> List Int foo a b = if a > b then a :: b :: [] main = text (String.join "," (foo 1 2))

Some funcJon on ELM

Page 27: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

foo : Int -> Int -> List Int foo a b = if a > b then a :: b :: [] main = text (String.join "," (foo 1 2))

Some funcJon on ELM

Page 28: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Elm CompilaJon

Page 29: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

So why ELM?

Page 30: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Not Haskell or PureScript

Page 31: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

• Pure funcJons -> NaJve

Benefits?

Page 32: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Pure FuncJonfunction firstFoo(x) { return x + x } function secondFoo(x) { calc() return x + x ... }

Page 33: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Pure FuncJonfunction firstFoo(x) { return x + x } function secondFoo(x) { calc() return x + x ... }

Page 34: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Pure FuncJonfunction firstFoo(x) { return x + x } function secondFoo(x) { calc() return x + x ... }

Page 35: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

SO?

Page 36: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

• Pure funcJons -> NaJve • Immutable data structures -> Immutable.js

Benefits?

Page 37: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Stateless

Page 38: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

• Pure funcJons -> NaJve • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript)

Benefits?

Page 39: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

StaJc type checkingf : Int -> Int f v = v + 3

Page 40: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

StaJc type checkingf : Int -> String f v = v + 3

Page 41: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

StaJc type checkingf : Int -> String f v = v + 3

Page 42: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

• Pure funcJons -> NaJve • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe

Benefits?

Page 43: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Maybe monad

Page 44: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

• Pure funcJons -> NaJve • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity -> Rx.js

Benefits?

Page 45: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

ReacJve

Page 46: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

• Pure funcJons -> NaJve • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity -> Rx.js • The Elm Architecture -> Redux

Benefits?

Page 47: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Dan Abramov @dan_abramov Redux

Page 48: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

ELM === Redux

Page 49: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

• Pure funcJons -> NaJve • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity -> Rx.js • The Elm Architecture -> Redux • DeclaraJve UI -> React.js

Benefits?

Page 50: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Virtual DOM

view = div [ Props ] [ Children ]

Page 51: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Example ELM vs Redux

Page 52: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Counter

Page 53: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Start App// Redux

let initialState = 0 const store = createStore( reducer, initialState ) const App () => ( <Provider store={ store }> <View /> </Provider> )

-- ELM

type alias Model = Int model : Model model = 0 main = Html.beginnerProgram { model = model , view = view , update = update }

Page 54: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Start App// Redux

let initialState = 0 const store = createStore( reducer, initialState ) const App () => ( <Provider store={ store }> <View /> </Provider> )

-- ELM

type alias Model = Int model : Model model = 0 main = Html.beginnerProgram { model = model , view = view , update = update }

Page 55: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Start App// Redux

let initialState = 0 const store = createStore( reducer, initialState ) const App = () => ( <Provider store={ store }> <View /> </Provider> )

-- ELM

type alias Model = Int model : Model model = 0 main = Html.beginnerProgram { model = model , view = view , update = update }

Page 56: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Start App// Redux

let initialState = 0 const store = createStore( reducer, initialState ) const App () => ( <Provider store={ store }> <View /> </Provider> )

-- ELM

type alias Model = Int model : Model model = 0 main = Html.beginnerProgram { model = model , view = view , update = update }

Page 57: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Start App// Redux

let initialState = 0 const store = createStore( reducer, initialState ) const App () => ( <Provider store={ store }> <View /> </Provider> )

-- ELM

type alias Model = Int model : Model model = 0 main = Html.beginnerProgram { model = model , view = view , update = update }

Page 58: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

View

-- ELM

view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]

// Redux

const View = ({ dispatch, state }) => ( <div> <button onClick={() => dispatch({type: 'DECREMENT'})} value='-' /> <div>{ state }</div> <button onClick={() => dispatch({type: 'INCREMENT'})} value='+' /> </div> )

Page 59: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

View

-- ELM

view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]

// Redux

const View = ({ dispatch, state }) => ( <div> <button onClick={() => dispatch({type: 'DECREMENT'})} value='-' /> <div>{ state }</div> <button onClick={() => dispatch({type: 'INCREMENT'})} value='+' /> </div> )

Page 60: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

View

-- ELM

view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]

// Redux

const View = ({ dispatch, state }) => ( <div> <button onClick={() => dispatch({type: 'DECREMENT'})} value='-' /> <div>{ state }</div> <button onClick={() => dispatch({type: 'INCREMENT'})} value='+' /> </div> )

Page 61: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

View

-- ELM

view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]

// Redux

const View = ({ dispatch, state }) => ( <div> <button onClick={() => dispatch({type: 'DECREMENT'})} value='-' /> <div>{ state }</div> <button onClick={() => dispatch({type: 'INCREMENT'})} value='+' /> </div> )

Page 62: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

View

-- ELM

view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]

// Redux

const View = ({ dispatch, state }) => ( <div> <button onClick={() => dispatch({type: 'DECREMENT'})} value='-' /> <div>{ state }</div> <button onClick={() => dispatch({type: 'INCREMENT'})} value='+' /> </div> )

Page 63: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Update-- ELM

type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

// Redux

const reducer = (state = initialState, action) => { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state } }

Page 64: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Update-- ELM

type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

// Redux

const reducer = (state = initialState, action) => { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state } }

Page 65: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Update-- ELM

type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

// Redux

const reducer = (state = initialState, action) => { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state } }

Page 66: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Update-- ELM

type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

// Redux

const reducer = (state = initialState, action) => { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state } }

Page 67: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Update-- ELM

type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

// Redux

const reducer = (state = initialState, action) => { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state } }

Page 68: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Update-- ELM

type Msg = Increment | Decrement update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

// Redux

const reducer = (state = initialState, action) => { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state } }

Page 69: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Debugging?

Page 70: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity
Page 71: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

So why ELM?

Page 72: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

NoRedInk

Page 73: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

50,000 lines of ELM code 1.5 year in producJon

Page 74: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

0 RunJme Errors

Page 75: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

How I can use it?

Page 76: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Ports & SubscripJons

Page 77: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

<body> <input type="submit" value="+" onClick="sendEvent('Increment')"> <div id="counter"></div> <input type="submit" value="-" onClick="sendEvent('Decrement')"> <script> ... </script> </body>

HTML

Page 78: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Result

Page 79: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Init App

main = Html.program { init = init , view = view , update = update , subscriptions = subscriptions }

main = Html.beginnerProgram { model = model , view = view , update = update }

Page 80: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Init App

main = Html.program { init = init , view = view , update = update , subscriptions = subscriptions }

main = Html.beginnerProgram { model = model , view = view , update = update }

Page 81: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Model

type alias Model = Int init : (Model, Cmd Msg) init = ( 0, Cmd.none )

type alias Model = Int model : Model model = 0

Page 82: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Model

type alias Model = Int init : (Model, Cmd Msg) init = ( 0, Cmd.none )

type alias Model = Int model : Model model = 0

Page 83: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

AcJons

type Msg = Increment | Decrement

type Msg = Update String

Page 84: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Ports-- PORTS port modelChanges : String -> Cmd msg port modelValue : (String -> msg) -> Sub msg -- SUBSCRIPTIONS subscriptions : Model -> Sub Msg subscriptions model = modelValue Update

Page 85: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

SubscripJons-- PORTS port modelChanges : String -> Cmd msg port modelValue : (String -> msg) -> Sub msg -- SUBSCRIPTIONS subscriptions : Model -> Sub Msg subscriptions model = modelValue Update

Page 86: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Updateupdate : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of Update action -> case action of "Increment" -> (model + 1, modelChanges (toString (model + 1))) "Decrement" -> (model - 1, modelChanges (toString (model - 1))) _ -> (model, Cmd.none)

Page 87: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Updateupdate : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of Update action -> case action of "Increment" -> (model + 1, modelChanges (toString (model + 1))) "Decrement" -> (model - 1, modelChanges (toString (model - 1))) _ -> (model, Cmd.none)

Page 88: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Updateupdate : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of Update action -> case action of "Increment" -> (model + 1, modelChanges (toString (model + 1))) "Decrement" -> (model - 1, modelChanges (toString (model - 1))) _ -> (model, Cmd.none)

Page 89: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

HTML<body> <input type="submit" value="+" onClick="sendEvent('Increment')"> <div id="counter"></div> <input type="submit" value="-" onClick="sendEvent('Decrement')"> <script> var node = document.getElementById('counter'); var app = Elm.Counter.embed(node); app.ports.modelChanges.subscribe(function(model) { console.log(model); }) function sendEvent(action) { app.ports.modelValue.send(action); } </script> </body>

Page 90: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

HTML<body> <input type="submit" value="+" onClick="sendEvent('Increment')"> <div id="counter"></div> <input type="submit" value="-" onClick="sendEvent('Decrement')"> <script> var node = document.getElementById('counter'); var app = Elm.Counter.embed(node); app.ports.modelChanges.subscribe(function(model) { console.log(model); }) function sendEvent(action) { app.ports.modelValue.send(action); } </script> </body>

Page 91: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Init Component<body> <input type="submit" value="+" onClick="sendEvent('Increment')"> <div id="counter"></div> <input type="submit" value="-" onClick="sendEvent('Decrement')"> <script> var node = document.getElementById('counter'); var app = Elm.Counter.embed(node); app.ports.modelChanges.subscribe(function(model) { console.log(model); }) function sendEvent(action) { app.ports.modelValue.send(action); } </script> </body>

Page 92: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Subscribe/Send<body> <input type="submit" value="+" onClick="sendEvent('Increment')"> <div id="counter"></div> <input type="submit" value="-" onClick="sendEvent('Decrement')"> <script> var node = document.getElementById('counter'); var app = Elm.Counter.embed(node); app.ports.modelChanges.subscribe(function(model) { console.log(model); }) function sendEvent(action) { app.ports.modelValue.send(action); } </script> </body>

Page 93: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

What about React?

Page 94: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

React Elm Component

Page 95: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

import Elm from 'react-elm-components' import { Counter } from './Counter'

Import

Page 96: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

class CounterWrapper extends Component ({ constructor(props) { super(props) this.ports = null } setupPorts = ports => { this.ports = ports this.ports.modelChanges.subscribe(model => console.log(model)) } sendEvent(action) { this.ports.modelValue.send(action) } render() { return ( <div> <input type='submit' value='+' onClick={() => this.sendEvent('Increment')} /> <Elm src={ Counter } ports={ this.setupPorts } /> <input type='submit' value='-' onClick={() => this.sendEvent('Decrement')} /> </div> ) } })

Page 97: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

class CounterWrapper extends Component ({ constructor(props) { super(props) this.ports = null } setupPorts = ports => { this.ports = ports this.ports.modelChanges.subscribe(model => console.log(model)) } sendEvent(action) { this.ports.modelValue.send(action) } render() { return ( <div> <input type='submit' value='+' onClick={() => this.sendEvent('Increment')} /> <Elm src={ Counter } ports={ this.setupPorts } /> <input type='submit' value='-' onClick={() => this.sendEvent('Decrement')} /> </div> ) } })

Page 98: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

class CounterWrapper extends Component ({ constructor(props) { super(props) this.ports = null } setupPorts = ports => { this.ports = ports this.ports.modelChanges.subscribe(model => console.log(model)) } sendEvent(action) { this.ports.modelValue.send(action) } render() { return ( <div> <input type='submit' value='+' onClick={() => this.sendEvent('Increment')} /> <Elm src={ Counter } ports={ this.setupPorts } /> <input type='submit' value='-' onClick={() => this.sendEvent('Decrement')} /> </div> ) } })

Page 99: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

class CounterWrapper extends Component ({ constructor(props) { super(props) this.ports = null } setupPorts = ports => { this.ports = ports this.ports.modelChanges.subscribe(model => console.log(model)) } sendEvent(action) { this.ports.modelValue.send(action) } render() { return ( <div> <input type='submit' value='+' onClick={() => this.sendEvent('Increment')} /> <Elm src={ Counter } ports={ this.setupPorts } /> <input type='submit' value='-' onClick={() => this.sendEvent('Decrement')} /> </div> ) } })

Page 100: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

class CounterWrapper extends Component ({ constructor(props) { super(props) this.ports = null } setupPorts = ports => { this.ports = ports this.ports.modelChanges.subscribe(model => console.log(model)) } sendEvent(action) { this.ports.modelValue.send(action) } render() { return ( <div> <input type='submit' value='+' onClick={() => this.sendEvent('Increment')} /> <Elm src={ Counter } ports={ this.setupPorts } /> <input type='submit' value='-' onClick={() => this.sendEvent('Decrement')} /> </div> ) } })

Page 101: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

So why ELM?

Page 102: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Redux Elm Middlewarehttps://github.com/stoeffel/redux-elm-middleware

Page 103: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Problem

Page 104: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

PureScripthttp://www.purescript.org/

Page 105: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

What Next?hep://elm-lang.org/ heps://www.reddit.com/r/elm hep://elmlang.herokuapp.com/ - Slack heps://medium.com/@diamondgfx - ELM tutorial heps://github.com/isRuslan/awesome-elm

Page 106: Shehet Gregory · 2016-11-27 · • Immutable data structures -> Immutable.js • StaJc type checking -> Flow (Not JavaScript) • No concept of Null -> folktale/data.maybe • ReacJvity

Thank you! QuesJons are welcome