@marcoshuttle / m.perone@mvlabs.it
PHP developer in my daily life
grew up as a mathematician
weakness for functional programming
I'm not an expert
Just loving ELM and wanting to share it
Front end developers?
Functional programmers?
Both?
Tried ELM?
Running ELM in production?
Statically typed reactive functional programming in the browser
Compiles to Html, Javascript and Css
elm make Main.elm
Type of a variable is known at compile time
Errors catched at compile time
NO RUNTIME EXCEPTIONS!!
Enforced semantic versioning
Hot-swapping
Making inputs and outputs explicit
If functions are stateless and variables are immutable, how can we interact with the real world?
Possible solutions:
Program paradigm oriented around data flows and the propagation of change
Time as first order citizen
Dynamic evolving values
Specify the dynamic behavior completely at the time of declaration
Similar to EventEmitters or Observables in Javascript
They exist for the entire lifetime of the program
They always have an initial value
main =
Signal.map show Mouse.position
main =
Signal.map display Keyboard.presses
display keyCode =
show ( Char.fromCode keyCode )
Only "real" signals
Not possible to create or destroy a signal, not to use signals of signals
Signals are connected setting up a static processing network
Signals from the world
We are not in control of changing the value, the value is changing everything else
map : ( a -> b ) -> Signal a -> Signal b
foldp : ( a -> b -> b ) -> b -> Signal a
-> Signal b
Application state all in one place
Save and undo become easy
merge : Signal a -> Signal a -> Signal b
Simple pattern for infinitely nestable components
Great for modularity, code reuse and testing
Hide implementation details
Expose just what you need
Ability to combine modules one with the other
Model
View
Update
Data structure representing the state of the component
Single source of truth
type alias Talk =
{ title : String
, description : String
, speaker : String
}
view : Model -> Html
No mutation of the DOM
view : Talk -> Html
view talk =
div [ class "talk" ]
[ div [] [ text ( talk.title ++
" by " ++ talk.speaker )]
, div [] [ text talk.description ]
]
type Action
= FirstAction
| SecondAction
| ThirdAction
update : Action -> Model -> Model
Clear representation of how the model can be transformed
type Action
= ClickTitle
| ClickDescription
update : Action -> Talk -> Talk
update action talk =
case action of
ClickDescription ->
{ talk | description =
"We got a new description!" }
from
to
Mechanism for sending messages in and out of ELM
Passing messages from Javascript to ELM
port addTalk : Signal (
Title, Description, Author )
myApp.ports.addTalk.send([
"ELM or how I learned to love ...",
"Front-end development is rapidly ...",
"Marco Perone"
]);
Passing messages from ELM to JavaScript
port requestTalks : Signal EventName
port requestTalks = signalOfEventName
myApp.ports.requestTalks.subscribe(
retrieveTalks);
function retrieveTalks (eventName) {
...
}
Abstraction to model side-effects handled by the runtime
Way to describe asynchronous operations that may fail
Tasks describe operations that could be performed in the future
To perform a task we hand it to a port
retrieveTalks : String
-> Task Http.Error Action
retrieveTalks uri =
get decoder uri
|> Task.map TalksRetrieved
type alias Mailbox a =
{ address : Address a
, signal : Signal a
}
mailbox : a -> Mailbox a
Asynchronous operations modelled with Effects Action
End result of execution is an Action that will be routed through the update function
Model
is replaced by ( Model, Effects Action )
update : Action -> Model
-> ( Model, Effects Action )
type Action
= ConfRetrieved ( Maybe Conferences )
| RetrieveTalks
update action joindin =
case action of
ConfRetrieved maybeConf ->
( addConferences joindin maybeConf
, task ( succeed RetrieveTalks )
)