Skip to content

Instantly share code, notes, and snippets.

@stephanie-gredell
Created December 28, 2020 05:46
Show Gist options
  • Save stephanie-gredell/109dcd83fcd0ed4aee2776d668001361 to your computer and use it in GitHub Desktop.
Save stephanie-gredell/109dcd83fcd0ed4aee2776d668001361 to your computer and use it in GitHub Desktop.
Solution to Elm button exercise
module Main exposing (..)
-- Press buttons to increment and decrement a counter.
--
-- Read how it works:
-- https://guide.elm-lang.org/architecture/buttons.html
--
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
-- MAIN
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Model = Int
init : Model
init =
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 =
div []
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (String.fromInt model) ]
, button [ onClick Increment ] [ text "+" ]
, button [ onClick Reset ] [ text "Reset" ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment