Skip to content

Instantly share code, notes, and snippets.

@Voles
Created November 10, 2017 16:29
Show Gist options
  • Save Voles/beddbfe5313150ee1470300fc8424677 to your computer and use it in GitHub Desktop.
Save Voles/beddbfe5313150ee1470300fc8424677 to your computer and use it in GitHub Desktop.
My First Elm App
-- Read more about this program in the official Elm guide:
-- https://guide.elm-lang.org/architecture/user_input/buttons.html
import Html exposing (beginnerProgram, div, button, text, input)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick, onInput)
import String exposing (toInt)
{-
import Result exposing (..)
-}
main =
beginnerProgram { model = { amount = 1 }, view = view, update = update }
type alias Model = { amount : Int }
type alias AnotherModel = { amount : Int }
type alias Cat = { feet : String }
view model =
div [class "container"]
[ button [ onClick Decrement ] [ text "-" ]
, div [] [ text (toString model.amount) ]
, button [ onClick Increment ] [ text "+" ]
, div []
[ input [ onInput MultiplyBy] [ text "1"] ]
]
type Msg = Increment | Decrement | MultiplyBy String
update : Msg -> Model -> Model
update msg model =
case msg of
Increment ->
{ model | amount = model.amount + 1 }
Decrement ->
{ model | amount = model.amount - 1 }
MultiplyBy x ->
{ model | amount = model.amount * Result.withDefault 1 (toInt x) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment