Okay, once I thought of the correct search term, I found a solution (using
fst and snd from Basics):
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
(model, Random.generate NewFace (Random.pair (Random.int 1 6) (Random.
int 1 6)))
NewFace newFace ->
(Model (fst newFace) (snd newFace), Cmd.none)
On Friday, June 3, 2016 at 4:40:50 PM UTC+2, Robert Walter wrote:
>
> If this is not the right forum for these kinds of questions, please point
> me to the right place.
>
> Anyway, I try to work my way the The Elm Architecture.
> As an exercise, I tried to enhance the Random Dice example by adding a
> second die. My idea is to add a second Int to the Model and use the
> Random.pair generator to produce random pairs.
>
> This is what I can come up with:
>
> import Html exposing (..)
> import Html.App as Html
> import Html.Events exposing (..)
> import Random
>
> main =
> Html.program
> { init = init
> , view = view
> , update = update
> , subscriptions = subscriptions
> }
>
>
> -- MODEL
>
> type alias Model =
> { dieFace1 : Int
> , dieFace2 : Int
> }
>
>
> init : (Model, Cmd Msg)
> init =
> (Model 1 1, Cmd.none)
>
>
> -- UPDATE
>
> type Msg
> = Roll
> | NewFace (Int, Int)
>
>
> update : Msg -> Model -> (Model, Cmd Msg)
> update msg model =
> case msg of
> Roll ->
> (model, Random.generate NewFace (Random.pair (Random.int 1 6) (
> Random.int 1 6)))
>
>
> NewFace newFace ->
> (newModel, Cmd.none)
>
>
> newModel : (Int, Int) -> Model
> newModel (a, b) =
> Model a b
>
>
> -- SUBSCRIPTIONS
>
> subscriptions : Model -> Sub Msg
> subscriptions model =
> Sub.none
>
>
> -- VIEW
>
>
> view : Model -> Html Msg
> view model =
> div []
> [ h1 [] [ text (toString model.dieFace1) ]
> , h1 [] [ text (toString model.dieFace2) ]
> , button [ onClick Roll ] [ text "Roll" ]
> ]
>
>
> This fails compiling with the following message:
>
> Detected errors in 1 module. -- TYPE MISMATCH
> --------------------------------------------------------------- The type
> annotation for `update` does not match its definition. 42| update : Msg
> -> Model -> (Model, Cmd Msg)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> The type annotation is saying: Msg
> -> { dieFace1 : Int, dieFace2 : Int }
> -> ( { dieFace1 : Int, dieFace2 : Int }, Cmd Msg ) But I am inferring
> that the definition has this type: Msg -> (( Int, Int ) -> Model) -> ( (
> Int, Int ) -> Model, Cmd Msg )
>
>
> I guess what I'm missing is how to create a new Model out of the (Int,
> Int) tuple. Maybe this is, however, fundamentally wrong?
>
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.