by Claudia Doppioslash
24 February 2015 at GeekUp Liverpool
Elm is a Functional Reactive Language for interactive applications.
Elm is built around its FRP implementation
RP has been adapted to OOP but it was born as FRP in Haskell.
You don't need to learn Haskell to try FRP as it was intended.
Based on Bret Victor's talk
which also inspired LightTable.
(At 12:00 one of the bits in question)
If you have tried Erlang, you know the joy of not needing to recompile the whole project just to test out a tiny change.
You most likely have known the pain of having to put null guards.
No more. Union Types will spare you from that.
Single assignment makes concurrency easier.
(See Erlang)
With Signals. A signal is a value that changes over time, which updates all the values that depend on it.
FRP is well suited for GUIs and games.
If you have had to wrangle with MVC and MVVM and more of those acronyms in the past you know that UI programming is hardly solved.
Just to open elm-lang.org/try in a browser. It includes an editor and a preview, it supports hot code reloading too.
Download the installer from
elm-lang.org/Install.elm, it's available for Mac and Windows.
Try your luck with Cabal.
Elm is implemented in Haskell.
import Graphics.Element (..) import Text (..) main : Element main = plainText "Hello, World!"
Import modules ->
<- Import all symbols in the module
Type signature ->
<- Main function
- Hello World -
These are the 3 necessary elements of an Elm program:
If you've ever tried Haskell you'll find the syntax familiar.
The type signature goes before the function.
But Elm has type inference so most of the time you'll be able to skip writing it down.
import Graphics.Element (..)
Packages are found on:
Will import all functions in Graphics.Elements, you'll be able to use them without prefix.
import Graphics.Element as el
You'll be able to use them with prefix el.namefunction
- Hello World -
square : Int -> Int
Is the signature of a function that takes an Integer and returns an Integer.
add : Int -> Int -> Int
Is the signature of a function that takes two Integers and returns an Integer.
- Hello World -
The main function is the entry point of program execution, so you need one, or your code won't run.
- Hello World -
import Graphics.Element (..)
import Signal (Signal, map)
import Mouse
import Text (asText)
main : Signal Element
main =
map asText Mouse.position
[from examples]
Signals are values that change over time. They are Inputs that can be transformed, merged and filtered.
In practice it's similar to how a spreadsheet works.
Simple program which shows the mouse position.
- Fundamental Concepts -
A union type is a way to put together many different types. Somewhat a more powerful enum.
type Action = Increment | Decrement
type Action = Increment Int | Decrement Int
Plain enumeration
Enumeration + Data
- Fundamental Concepts -
type Maybe Int = Just Int | Nothing
The Maybe type allows us to avoid null checks. If there is a value it will be Just value if there isn't it will be Nothing.
- Fundamental Concepts -
It's a much more powerful switch case statement. Made to be use in combination with Union Types, it will accept any type the Union Type has attached.
type alias Model = Int
update : Action -> Model -> Model
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1
type alias Model = Int
update : Action -> Model -> Model
update action model =
case action of
Increment value -> model + value
Decrement value -> model - value
Plain enumeration
Enumeration + Data
- Fundamental Concepts -
String.toInt : String -> Maybe Int
toMonth : String -> Maybe Int
toMonth rawString =
case String.toInt rawString of
Nothing -> Nothing
Just n ->
if n > 0 && n <= 12 then Just n else Nothing
- Fundamental Concepts -
- Fundamental Concepts -
A function that is applied on a signal in time:
foldp : (a -> b -> b) -> b -> Signal a -> Signal b
Its arguments are:
The two args of the function are the current value of the input signal, and the past value or default value if there is no past value.
Model - a full definition of the application's state
Update - a way to step the application state forward
View - a way to visualize our application state with HTML
Signals - the signals and inputs necessary to manage events
[example from elm-architecture]
- Architecture -
-- MODEL
type alias Model = { ... }
-- UPDATE
type Action = Reset | ...
update : Action -> Model -> Model
update action model =
case action of
Reset -> ...
...
-- VIEW
view : Model -> Html
view =
...
<- type alias
<- Union type action
<- update function
<- View update function
- Architecture -
A data structure which contains our state. We skipped over Records so we'll stick to a plain Int.
type alias Model = Int
- Architecture -
type Action = Increment | Decrement
update : Action -> Model -> Model
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1
- Architecture -
view : Model -> Html
view model =
div []
[ button [ onClick (Signal.send actionChannel Decrement) ] [ text "-" ]
, div [ countStyle ] [ text (toString model) ]
, button [ onClick (Signal.send actionChannel Increment) ] [ text "+" ]
]
countStyle : Attribute
countStyle =
style
[ ("font-size", "20px")
, ("font-family", "monospace")
, ("display", "inline-block")
, ("width", "50px")
, ("text-align", "center")
]
The function which applied to the model creates the new view state.
main : Signal Html
main =
Signal.map view model
model : Signal Model
model =
Signal.foldp update 0 (Signal.subscribe actionChannel)
actionChannel : Signal.Channel Action
actionChannel =
Signal.channel Increment
- Architecture -
The main function is driven by the Signals. It is updated every time we click on the buttons we create in the view.
Use elm-package to install community packages:
elm-package install user/project
elm-package is sandboxed by default. All packages are installed on a per-project basis, so all your projects dependencies are independent.
[from elm web site]
You can also put the dependencies of a project in the elm-package.json file, for convenient dependency handling.
Workflow:
Command:
elm-make Main.elm --output=main.html
Workflow:
Command:
elm-reactor
In the root of your elm files.
You should now:
Fin.