|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The Elm Architecture |
|
|
|
|
|
|
|
|
|
Jan Dudek |
|
|
|
React.js Meetup |
|
|
|
2015-11-24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
What is Elm? |
|
|
|
|
|
* purely functional language |
|
|
|
* FP for humans |
|
|
|
* compiles to JavaScript |
|
|
|
* static typing |
|
|
|
* functional reactive programming |
|
|
|
* The Elm Architecture |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Purely functional language |
|
|
|
* all data is immutable |
|
|
|
* no side effects inside pure functions |
|
|
|
* communication with outside world: |
|
|
|
Signals |
|
|
|
Tasks |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FP for humans |
|
|
|
|
|
* Not like Haskell… |
|
|
|
“A monad is just a monoid in the category of endofunctors” |
|
|
|
“A catamorphism deconstructs a data structure with an F-algebra for its underlying functor” |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Compiles to JavaScript |
|
|
|
|
|
…but with its own runtime |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Static typing |
|
|
|
* if your code compiles, it’s very likely to work correctly on the 1st run |
|
|
|
* (almost) no runtime exceptions |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Static typing: examples |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Static typing: benefits |
|
|
|
|
|
|
|
* types like Maybe force us to *always* handle failure |
|
|
|
* no way to interact with outside world without using special types |
|
|
|
* never see: |
|
|
|
Cannot read property 'foo' of undefined |
|
|
|
'foo' is not a function |
|
|
|
etc. |
|
|
|
* don’t worry about making typos and other silly mistakes anymore |
|
|
|
* refactor with confidence – compiler will find the errors |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Functional reactive programming |
|
|
|
|
|
|
|
* signals |
|
|
|
for example: mouse position, last pressed key, results of HTTP requests |
|
|
|
|
|
* the application is also a signal: signal of Html values |
|
|
|
|
|
|
|
* all external input can be modelled as signals |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The Elm Architecture |
|
|
|
|
|
* model – state of the app |
|
|
|
* view – pure mapping from model to html |
|
|
|
* update – change model when an action happens |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The Elm Architecture: composability |
|
|
|
|
|
* compose components by composing their model, view & update |
|
|
|
init |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Example: time-travelling debugger |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The Elm Architecture: effects |
|
|
|
|
|
* all side effects have to be modelled explicitly |
|
|
|
|
|
* instead of |
|
|
|
update : Action -> Model -> Model |
|
|
|
we have |
|
|
|
update : Action -> Model -> (Model, Effects) |
|
|
|
|
|
* in other words, all side effects have to be performed *explicitly* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Why should I learn Elm? |
|
|
|
|
|
* it’s fun |
|
|
|
* it’s mind-bending |
|
|
|
* it *forces* you to use best practices |
|
|
|
* the Elm Architecture is the main inspiration for Redux (a Flux implementation) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thank you! |
|
|