Last active
September 15, 2015 11:56
-
-
Save alexspurling/51048d3935afe7427c69 to your computer and use it in GitHub Desktop.
Elm example for producing random values in a model
This file contains hidden or 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 (div, button, text) | |
import Html.Events exposing (onClick) | |
import StartApp.Simple as StartApp | |
import Random exposing (Seed) | |
type alias Model = { seed : Seed, values : List Int } | |
initialModel : Model | |
initialModel = | |
{ seed = (Random.initialSeed 0), | |
values = [1, 2, 3] | |
} | |
view address model = | |
div [] | |
[ button [ onClick address AnotherOne ] [ text "Give me more randoms" ] | |
, div [] [ text (toString model.values) ] | |
] | |
randomList : Seed -> (List Int, Seed) | |
randomList seed = | |
let | |
(numberOfItems, seed') = Random.generate (Random.int 0 30) seed | |
in | |
Random.generate (Random.list numberOfItems (Random.int 0 100)) seed' | |
type Action = AnotherOne | |
update : Action -> Model -> Model | |
update action model = | |
case action of | |
AnotherOne -> | |
let | |
(randoms, newSeed) = randomList model.seed | |
in | |
{ seed = newSeed, values = randoms } | |
main = | |
StartApp.start { model = initialModel, view = view, update = update } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment