Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a second die to this elm effects example?

Tags:

elm

I am new to Elm and have been looking at the following example (note this is under the newer 0.17 architecture, where Action is now Command): http://elm-lang.org/examples/random

There is a follow up challenge to add a second die to the example, so that a single click of the button rolls a new value for each die. My idea is to change the model to hold two separate values, one for each die, ala

type alias Model =
       { dieFace1 : Int
       , dieFace2 : Int
       }

This works fine until I get to the update block. I am not sure how to update the Random number generator to create two values. The function is a bit confusing to me.

type Msg
  = Roll
  | NewFace Int Int


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Roll ->
      **(model, Random.generate NewFace (Random.int 1 6))** <-- not sure what to do here

    NewFace newFace1 newFace2 ->
      (Model newFace1 newFace2, Cmd.none)

The documentation for the Random.generate function is a bit light -

generate : (a -> msg) -> Generator a -> Cmd msg

Create a command that will generate random values.

Is this even the correct approach to handle two dice, or is there a better way? I am an elm noob, please be nice :)

like image 691
TonyM Avatar asked May 14 '16 13:05

TonyM


1 Answers

Random.int is a primitive generator that gives you a single random int. You need a generator that gives you exactly two random integers.

Random number generators can be built up from more primitive generators to create more complex generators. Fortunately, Elm has just such a function, Random.pair which lets you specify which two generators you want for each part of the tuple.

Let's pull the die generator into its own function to avoid repeating ourselves:

dieGenerator : Random.Generator Int
dieGenerator =
  Random.int 1 6

Now we can build another generator that gives us the random value of a pair of die:

diePairGenerator : Random.Generator (Int, Int)
diePairGenerator =
  Random.pair dieGenerator dieGenerator

Since we're dealing with a tuple of ints, let's update your Msg definition of NewFace Int Int to NewFaces (Int, Int). That will allow your Roll handler to be nice and clean:

Roll ->
  (model, Random.generate NewFaces diePairGenerator)

If you want to try moving beyond this, think about what it would take to allow for any number of die to be rolled. Take this idea of building complex generators from more primitive generators and use the documentation for the Random module us a guide.

like image 51
Chad Gilbert Avatar answered Sep 23 '22 12:09

Chad Gilbert