Created
October 11, 2016 08:13
-
-
Save manuscrypt/eb8ebce074092e5c34bf575a66dae374 to your computer and use it in GitHub Desktop.
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
module Main exposing (..) | |
import Html exposing (div, button, ul, Html, li, text) | |
import Html.Attributes exposing (value, style) | |
import Html.Events exposing (onClick) | |
import Html.App as App | |
import Dict exposing (Dict) | |
import Random exposing (Seed) | |
type alias Child = | |
{ id : Int } | |
type alias Model = | |
{ children : Dict Int Child | |
, seed : Seed | |
} | |
type Msg | |
= CreateChild | |
init : Model | |
init = | |
{ children = Dict.empty | |
, seed = Random.initialSeed 42 | |
} | |
idGenerator : Random.Generator Int | |
idGenerator = | |
Random.int 0 Random.maxInt | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
CreateChild -> | |
let | |
( rndInt, newSeed ) = | |
Random.step idGenerator model.seed | |
newChild = | |
Child rndInt | |
in | |
{ model | seed = newSeed, children = Dict.insert rndInt newChild model.children } | |
view : Model -> Html Msg | |
view model = | |
div [ style [ (,) "margin" "50px" ] ] | |
[ button [ onClick CreateChild ] [ text "Create Child" ] | |
, ul [] (List.map viewChild <| Dict.values model.children) | |
] | |
viewChild : Child -> Html Msg | |
viewChild child = | |
li [] [ text <| "Child with Id " ++ (toString child.id) ] | |
main : Program Never | |
main = | |
App.beginnerProgram | |
{ model = init | |
, update = update | |
, view = view | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment