Writing JS is hard!
Requires
Change code with confidence
The compiler will catch any error
Immutable data
Pure functions
All the data they need is passed
They no have side effects
Non intrusive
Inferred
Has no side effects
Its return value is based on input parameters
Does not use global state
Reusable
Composable
Testable
Cacheable
Parallelizable
Type declarations are verbose
Cryptic Error Messages
Still have runtime errors
Types are inferred
Errors are friendly and helpful
No runtime exceptions
Feels like using a dynamic language
A simple pattern for architecting webapps
Great for modularity, code reuse, and testing
The state of your application
type alias Model =
Int
initialModel =
0
A way to update your state
type Msg = Increment | Decrement
update: Msg -> Model -> Model
update msg model =
case msg of
Increment ->
model + 1
Decrement ->
model - 1
A way to view your state as HTML
view : Model -> Html Msg
view model =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (toString model) ]
, button [ onClick Increment ] [ text "+" ]
]
main =
Html.beginnerProgram
{ model = initialModel
, view = view
, update = update
}
Miguel Cobá