Skip to content

Instantly share code, notes, and snippets.

@manuscrypt
Created October 11, 2016 08:13
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 manuscrypt/eb8ebce074092e5c34bf575a66dae374 to your computer and use it in GitHub Desktop.
Save manuscrypt/eb8ebce074092e5c34bf575a66dae374 to your computer and use it in GitHub Desktop.
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