Created
September 16, 2016 10:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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