Skip to content

Instantly share code, notes, and snippets.

@pdamoc
Last active June 17, 2016 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdamoc/c96714479d9f531fbc7468d5670ef576 to your computer and use it in GitHub Desktop.
Save pdamoc/c96714479d9f531fbc7468d5670ef576 to your computer and use it in GitHub Desktop.
SAM in Elm
module Main exposing (..)
import Html exposing (..)
import Html.App as App
import Html.Events exposing (onClick)
import Time
-- MODEL
type Model
= Ready
| Countdown Int
| Aborted
| Launched
init : Model
init =
Ready
-- UPDATE
type Msg
= StartCountdown
| Tick
| Abort
update : Msg -> Model -> Model
update msg model =
case ( msg, model ) of
( StartCountdown, Ready ) ->
Countdown 10
( Tick, Countdown c ) ->
if c == 1 then
Launched
else
Countdown (c - 1)
( Abort, Countdown c ) ->
Aborted
( _, _ ) ->
model
-- VIEW
view : Model -> Html Msg
view model =
case model of
Ready ->
div []
[ button [ onClick StartCountdown ] [ text "Start Countdown" ] ]
Countdown count ->
div []
[ p [] [ text ("Countdown: " ++ (toString count)) ]
, button [ onClick Abort ] [ text "Abort" ]
]
Aborted ->
div [] [ text "Launch Aborted!" ]
Launched ->
div [] [ text "Rocket Launched!" ]
-- WIRING
subscriptions : Model -> Sub Msg
subscriptions model =
case model of
Countdown _ ->
Time.every Time.second (\_ -> Tick)
_ ->
Sub.none
main : Program Never
main =
App.program
{ init = init ! []
, update = \msg model -> update msg model ! []
, view = view
, subscriptions = subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment