Skip to content

Instantly share code, notes, and snippets.

@dotcs
Created September 16, 2016 19:38
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 dotcs/3b3626cfbe5b0744134f7af8edcb32c5 to your computer and use it in GitHub Desktop.
Save dotcs/3b3626cfbe5b0744134f7af8edcb32c5 to your computer and use it in GitHub Desktop.
Elm example with two dices
import Html exposing (..)
import Html.App as App
import Html.Events exposing (onClick)
import Random
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ dieFace1 : Int
, dieFace2 : Int
}
init : (Model, Cmd Msg)
init =
(Model 1 2, Cmd.none)
dieGenerator : Random.Generator Int
dieGenerator =
Random.int 1 6
diePairGenerator : Random.Generator (Int, Int)
diePairGenerator =
Random.pair dieGenerator dieGenerator
-- UPDATE
type Msg
= Roll
| NewFaces (Int, Int)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
(model, Random.generate NewFaces diePairGenerator)
NewFaces (newFace1, newFace2) ->
(Model newFace1 newFace2, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h1 [] [ text (toString model.dieFace1) ]
, h1 [] [ text (toString model.dieFace2) ]
, button [ onClick Roll ] [ text "Roll" ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment