Elm
A different approach to webapps
Principle 1: Single source of truth
The state of your whole application is stored in an record tree.
Principle 2: State is read-only
The only way to mutate the state is to emit an action describing what happened.
Counter.elm
module Counter (Model, init, Action, update, view) where
import Html exposing (..)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
-- MODEL
type alias Model = Int
init : Int -> Model
init count = count
-- UPDATE
type Action = Increment | Decrement
update : Action -> Model -> Model
update action model =
case action of
Increment -> model + 1
Decrement -> model - 1
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
div []
[ button [ onClick address Decrement ] [ text "-" ]
, div [ countStyle ] [ text (toString model) ]
, button [ onClick address Increment ] [ text "+" ]
]
countStyle : Attribute
countStyle =
style
[ ("font-size", "20px")
, ("font-family", "monospace")
, ("display", "inline-block")
, ("width", "50px")
, ("text-align", "center")
]
Example 2 - Pair of Counters
CounterPair.elm
module CounterPair where
import Counter
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- MODEL
type alias Model =
{ topCounter : Counter.Model
, bottomCounter : Counter.Model
}
init : Int -> Int -> Model
init top bottom =
{ topCounter = Counter.init top
, bottomCounter = Counter.init bottom
}
-- UPDATE
type Action
= Reset
| Top Counter.Action
| Bottom Counter.Action
update : Action -> Model -> Model
update action model =
case action of
Reset -> init 0 0
Top act ->
{ model |
topCounter <- Counter.update act model.topCounter
}
Bottom act ->
{ model |
bottomCounter <- Counter.update act model.bottomCounter
}
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
div []
[ Counter.view (Signal.forwardTo address Top) model.topCounter
, Counter.view (Signal.forwardTo address Bottom) model.bottomCounter
, button [ onClick address Reset ] [ text "RESET" ]
]
Compiling
Wrong data type
type alias Model = String
Detected errors in 1 module.
## ERRORS in ././Counter.elm ###################################################
-- TYPE MISMATCH ----------------------------------------------- ././Counter.elm
The type annotation for `update` does not match its definition.
17| update : Action -> Model -> Model
^^^^^^^^^^^^^^^^^^^^^^^^
As I infer the type of values flowing through your program, I see a conflict
between these two types:
String
number
Elm Hedeley - Headless Drupal
https://github.com/amitaibu/elm-hedley
Yeoman Generator
https://github.com/Gizra/generator-elmlang
- Gulp
- browserSync
- Auto-compile
- Sass
- Bundle and Deploy to GH-pages