Skip to content

Instantly share code, notes, and snippets.

@louy2
Created September 16, 2016 10:34
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 louy2/4da43709fa8d53ffb6fc92ee75c01ba3 to your computer and use it in GitHub Desktop.
Save louy2/4da43709fa8d53ffb6fc92ee75c01ba3 to your computer and use it in GitHub Desktop.
Elm guide random, now with more dices. https://guide.elm-lang.org/architecture/effects/random.html
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Random
main =
App.program
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type alias Model =
{ dieFaces : List Int
}
-- INIT
init : (Model, Cmd Msg)
init =
(Model [1], Cmd.none)
-- UPDATE
type Msg
= Roll
| NewFaces (List Int)
| NewDice
update : Msg -> Model -> (Model, Cmd Msg)
update msg {dieFaces} =
case msg of
Roll ->
( Model dieFaces
, Random.generate NewFaces (Random.list (List.length dieFaces) (Random.int 1 6))
)
NewFaces newFaces ->
(Model newFaces, Cmd.none)
NewDice ->
(Model ( 1 :: dieFaces ), Cmd.none)
-- VIEW
view : Model -> Html Msg
view model =
div []
[ ul []
(List.map showDice model.dieFaces)
, button [ onClick Roll ] [ text "Roll" ]
, button [ onClick NewDice ] [ text "Add" ]
]
showDice dice =
li [] [ text (toString dice) ]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment