Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Elm Counter
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
main = Html.beginnerProgram { model = model, view = view, update = update }
--- MODEL
type alias Model = Int
model : Model
model =
0
-- UPDATE
type Msg = Increment | Decrement | Reset
update : Msg -> Model -> Model
update msg model =
case msg of
Increment -> model + 1
Decrement -> model - 1
Reset -> 0
-- VIEW
view : Model -> Html Msg
view model =
p []
[ button [ onClick Decrement ] [ text "<" ]
, span [] [ text (toString model) ]
, button [ onClick Increment ] [ text ">" ]
, button [ onClick Reset, disabled (model == 0) ] [ text "Reset" ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment