Skip to content

Instantly share code, notes, and snippets.

@pdamoc
Created February 5, 2016 11:04
Show Gist options
  • Save pdamoc/8e88926fce5e27ba4226 to your computer and use it in GitHub Desktop.
Save pdamoc/8e88926fce5e27ba4226 to your computer and use it in GitHub Desktop.
RandomDemoPair
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "3.0.0 <= v < 4.0.0",
"evancz/elm-effects": "2.0.1 <= v < 3.0.0",
"evancz/elm-html": "4.0.2 <= v < 5.0.0",
"evancz/elm-http": "3.0.0 <= v < 4.0.0",
"evancz/start-app": "2.0.2 <= v < 3.0.0"
},
"elm-version": "0.16.0 <= v < 0.17.0"
}
module RandomDemo where
import Html exposing (..)
import Html.Events exposing (onClick)
import Random exposing (int, generate)
import StartApp
import Effects exposing (Effects)
type alias Model = { value : Int, seed : Random.Seed}
type Action = NewRandom
init : Int -> Random.Seed -> Model
init value initialSeed = Model value initialSeed
randInt : Random.Generator Int
randInt = int 10 100
update : Action -> Model -> Model
update action model =
case action of
NewRandom -> (uncurry Model) <| generate randInt model.seed
view : Signal.Address Action -> Model -> Html
view address model =
div []
[ text <| toString model.value
, br [] []
, button [onClick address NewRandom][ text "Push for new random"]
]
noFx : (Action -> Model -> Model) -> Action -> Model -> (Model, Effects Action)
noFx update action model = (update action model, Effects.none)
app : StartApp.App Model
app = StartApp.start
{ init = (init 42 (Random.initialSeed 42), Effects.none)
, update = noFx update
, view = view
, inputs = []}
main : Signal Html
main = app.html
import Html exposing (..)
import Effects exposing (Effects, Never)
import Task exposing (Task)
import StartApp
import RandomDemo as RD
import Random exposing (generate, initialSeed, pair, minInt, maxInt, int)
type Action = Top RD.Action | Bottom RD.Action
type alias Model =
{ top : RD.Model
, bottom : RD.Model
}
init : (Model, Effects Action)
init =
let
((int1, int2), _) = generate (pair (int minInt maxInt) (int minInt maxInt)) (initialSeed 42)
(value, seed) = generate RD.randInt (initialSeed int1)
(value', seed') = generate RD.randInt (initialSeed int2)
top = RD.init value seed
bottom = RD.init value' seed'
in
( Model top bottom, Effects.none )
update : Action -> Model -> (Model, Effects Action)
update action model =
case action of
Top act ->
let
top' = RD.update act model.top
in
(Model top' model.bottom, Effects.none)
Bottom act ->
let
bottom' = RD.update act model.bottom
in
(Model model.top bottom', Effects.none)
view : Signal.Address Action -> Model -> Html
view address model =
div []
[ RD.view (Signal.forwardTo address Top) model.top
, br [] []
, RD.view (Signal.forwardTo address Bottom) model.bottom]
app : StartApp.App Model
app = StartApp.start
{ init = init
, update = update
, view = view
, inputs = []}
main : Signal Html
main = app.html
port tasks : Signal (Task Never ())
port tasks = app.tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment