Skip to content

Instantly share code, notes, and snippets.

Created August 11, 2016 12:23
Show Gist options
  • Save anonymous/869fb4b099a02f328036a62b8894b6ad to your computer and use it in GitHub Desktop.
Save anonymous/869fb4b099a02f328036a62b8894b6ad to your computer and use it in GitHub Desktop.
import Html exposing (..)
import Html.App as App
import Html.Events exposing (..)
import List
import Json.Decode as Json
join : List String -> String
join list = (List.foldr (++) "" list)
main : Program Never
main = App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions}
type alias Model =
{blue: Int, red: Int}
init : (Model, Cmd Msg)
init = (Model 0 0, Cmd.none)
type Msg
= Red | Blue | Noop
update : Msg -> Model -> (Model, Cmd Msg)
update message model =
case message of
Blue ->
(Model (model.blue + 1) model.red, Cmd.none)
Red ->
(Model model.blue (model.red + 1), Cmd.none)
Noop ->
(model, Cmd.none)
subscriptions: Model -> Sub Msg
subscriptions model = Sub.none
keyCodeToMsg : Int -> Msg
keyCodeToMsg keyCode =
case keyCode of
83 -> Blue
79 -> Red
_ -> Noop
onKeyCode : (Int -> msg) -> Attribute msg
onKeyCode message =
on "keydown" (Json.map message keyCode)
view : Model -> Html Msg
view model =
div [ onKeyCode keyCodeToMsg ]
[ h1 [] [ text "Arm Wrestling" ]
, p [] [text ("Blue: " ++ (toString model.blue) ++ " Red: " ++ (toString model.red))]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment