Skip to content

Instantly share code, notes, and snippets.

@dktn
Created October 15, 2019 22:41
Show Gist options
  • Save dktn/019988601b1928605bbc92fcbb9a6954 to your computer and use it in GitHub Desktop.
Save dktn/019988601b1928605bbc92fcbb9a6954 to your computer and use it in GitHub Desktop.
Elm example program
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Browser exposing (sandbox)
type alias Model =
{ message : String
, count : Int
}
type Msg =
Message String
| Increment
| Decrement
initialModel : Model
initialModel =
Model "Hi, this is state" 0
update : Msg -> Model -> Model
update msg model =
case msg of
Message newMessage ->
{ model | message = newMessage }
Increment ->
{ model | count = model.count + 1 }
Decrement ->
{ model | count = model.count - 1 }
view : Model -> Html Msg
view model =
div []
[ input [ placeholder "Type some stuff", onInput Message ] []
, div [] [ text model.message ]
, button [ onClick Increment ] [ text "+" ]
, button [ onClick Decrement ] [ text "-" ]
, div [] [ text (String.fromInt model.count) ]
]
main : Program () Model Msg
main =
sandbox
{ init = initialModel
, view = view
, update = update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment