elm / random

Generate random values in Elm
https://package.elm-lang.org/packages/elm/random/latest/
BSD 3-Clause "New" or "Revised" License
46 stars 23 forks source link

Feature Request: Random.generateTask #13

Open Orasund opened 5 years ago

Orasund commented 5 years ago

I noticed that Elm/Random has no way to generate a random value as a Task. I’m not sure if this is intended.

Currently, I am trying to build a chat with Jsonstore at the backend. If I now want to create a new chatroom, I need to first generate a number and then create the room.

createRoom : Task Error (Maybe Room)
createRoom =
    Random.generateTask Random.int 0 Random.maxInt
    |> Task.andThen insertNewRoom

sadly, Random has now task version of gernerate and therefore i need to instead split up the task into two messages.

update : Msg -> Model -> (Model,Cmd Msg)
update msg model =
    case msg of
        PressedNewRoomButton ->
            ( model
            , Random.generate Random.int 0 Random.maxInt
            )

        GeneratedRoomId int ->
            ( model
            , insertNewRoom int
                |> Task.attempt Sync
            )