Slide 1

Slide 1 text

pure functional programming in JS

Slide 2

Slide 2 text

hello! [email protected] frontend engineer @AudienseCo website handle email

Slide 3

Slide 3 text

Olmo Or how I stealed Elm’s Architecture

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Why did I do that? ● I wanted an unidirectional architecture for ui ● On top of that I wanted treat side-effects as data ● I wanted to use as much pure functions as possible ● I wanted composable components ● I wanted it to be simple

Slide 6

Slide 6 text

Pure functions init : InitalValue -> (Model, SideEffect) update : Action -> Model -> (Model, SideEffect) view : EventsChannel -> Model -> Html

Slide 7

Slide 7 text

Side effects as data function init( topic = “funny cats” ) { const model = { topic, url: “assets/waiting.gif” } const effect = getRandomGifWithTopic( topic ) return [ model, effect ] }

Slide 8

Slide 8 text

Composable function parentInit( ) { return childInit() } function parentUpdate( ) { return childUpdate() } function parentView( ) { return childView() }

Slide 9

Slide 9 text

Simple init update view actions signal

Slide 10

Slide 10 text

Back to the basics

Slide 11

Slide 11 text

Functional Programming What is it?

Slide 12

Slide 12 text

2 Big Concepts Let’s define them first

Slide 13

Slide 13 text

1. Equality Let’s say two things are equal or equivalent if they are constructed the same way, so they can be used interchangeably in our program.

Slide 14

Slide 14 text

x = y var addA = Adder() var addB = Adder() is addA = addB ? addA(1) -> 1 addA(2) -> 2 addB(3) -> 3

Slide 15

Slide 15 text

x = y var addA = Adder() var addB = Adder() is addA = addB ? addA(1) -> 1 addA(2) -> 2 addB(3) -> 3 addA(3) -> 3

Slide 16

Slide 16 text

x = y function Adder( sum = 0 ) { return function( quantity ) { return sum + quantity } }

Slide 17

Slide 17 text

2. Referential Transparency We can generalize equality and say that every expression that can be replaced by another expression that denotes the same value is referentially transparent. With referential transparency there’s no distinction between the name of a thing and the value it denotes, so we benefit of optimizations like: memoization, parallelization, common subexpression elimination, etc.

Slide 18

Slide 18 text

Now let’s send those nice props back to the trash can

Slide 19

Slide 19 text

x ≠ y function Adder( sum = 0 ) { return function( quantity ) { sum = sum + quantity return sum } } KABOOM!

Slide 20

Slide 20 text

x ≠ y var addA = Adder() var addB = Adder() is addA = addB ? addA(2) -> 2 addA(2) -> 4 addB(2) -> 2 ACCUMULATED EFFECT IDENTITY STATE

Slide 21

Slide 21 text

What just happened? ● By introducing assignment our objects got identity introducing a new dimension of bugs: side-effect bugs ● By introducing assignment is just philosophically harder to define equality ● By introducing assignment we brought our time perspective into our programs ● By introducing assignment we just make our lives more difficult

Slide 22

Slide 22 text

Functional Programming Is a technique for program construction that forbids the use of assignment.

Slide 23

Slide 23 text

Imperative Programming Is a technique for program construction that uses assignment.

Slide 24

Slide 24 text

Functional Programming is more simple than imperative programming, but imperative programming is easier than functional programming.

Slide 25

Slide 25 text

“ Simple is an objective notion... -- Rich Hickey

Slide 26

Slide 26 text

Simple Less interleaving of things/concepts. Easy Nearer to your current skills.

Slide 27

Slide 27 text

Simplicity is HARD

Slide 28

Slide 28 text

Even worse in JavaScript because it requires discipline which is also hard, however some functional languages use invariants (the type system) which is way less hard.

Slide 29

Slide 29 text

Why do we need assignment? ● It gives us encapsulation and better modularity ● We can move in all three dimensions of space, yet we are prisoners of the present, that is, we need assignment to work with time because mathematical functions are timeless, static ● We could think that our temporal existence is what imposes state on any system

Slide 30

Slide 30 text

x ≠ y var addA = Adder() var addB = Adder() is addA = addB ? addA(2) -> 2 addA(2) -> 4 addB(2) -> 2 ACCUMULATED EFFECT IDENTITY STATE

Slide 31

Slide 31 text

But we can still emulate state with mathematical functions for the most part and kind of hide the imperative chunks

Slide 32

Slide 32 text

Modeling state with streams state = stream( time ) state = head(stream) link to gist

Slide 33

Slide 33 text

Function Cola ahead

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Things to highlight defer/demand Thanks to those we can model infinite streams that are computed as needed, the downside is that we lose a bit of modularity and some functions depending on what they do might have a version that accepts deferred arguments normal order evaluation One way to avoid that problem would be to automatically deferring all function arguments giving us a language with normal order evaluation, yet it would complicate the cases where our program depends on the order of events Scalability The same interface can be used to solve different problems (sync vs async).

Slide 54

Slide 54 text

Modeling state with signals Signals (or Observables) are just streams where the producer (not the consumer) is the one in charge. link to gist

Slide 55

Slide 55 text

Function Cola ahead

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

we can’t escape time

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Benefits of signals Time Higher-order view makes time a static quantity which let us benefit from mathematical properties. Modularity It restores back the same encapsulation we had when using assignment but without using it and with better semantics. Combinatorial interface The interface we obtain is highly expressive and very powerful constructs can be made out of simple combinators, it also can be used to solve different domain problems (sync vs async) maintaining almost the same interface.

Slide 66

Slide 66 text

Olmo we use signals (through RxJS library) other people are doing the same (although not necessarily with RxJS) elm cycle angular2 ?

Slide 67

Slide 67 text

thanks! Any questions?