Skip to content

Instantly share code, notes, and snippets.

@adamrabung
Created September 1, 2016 11:18
Show Gist options
  • Save adamrabung/9cbb55ff3401d3202d10f5e1e0076dbf to your computer and use it in GitHub Desktop.
Save adamrabung/9cbb55ff3401d3202d10f5e1e0076dbf to your computer and use it in GitHub Desktop.
next week - 2 die
import Html exposing (..)
import Html.App as App
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Random
import Array
import List exposing (..)
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ dieFace : Int
}
init : (Model, Cmd Msg)
init =
(Model 1, Cmd.none)
-- UPDATE
type Msg
= Roll
| NewFace Int
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
(model, Random.generate NewFace (Random.int 1 6))
NewFace newFace ->
(Model newFace, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
dieFaces = ["http://i.imgur.com/vn28iue.gif",
"https://i.imgflip.com/2/ljk.jpg",
"http://i3.kym-cdn.com/photos/images/facebook/000/234/765/b7e.jpg"]
get : List t -> Int -> Maybe t
get xs index = head (drop index xs)
view : Model -> Html Msg
view model =
case get dieFaces (model.dieFace % length dieFaces) of
Just imgSrc ->
div []
[ h1 [] [ img [src imgSrc] [] ]
, button [ onClick Roll ] [ text "Roll" ]
]
Nothing -> div [] []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment