New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: Add an API to handle Random as a Task #924

Open
sabiwara opened this Issue Dec 4, 2017 · 5 comments

Comments

Projects
None yet
5 participants
@sabiwara

sabiwara commented Dec 4, 2017

The Random package does not expose any API to handle random generation as a Task, which makes simple composition of side effects difficult (e.g. sleep for a random amount of time, call an URL with some random parameter...).

I saw this reddit thread mentioning something like a Random.toTask but no opened issue for it.

@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Dec 4, 2017

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Dec 4, 2017

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@sabiwara

This comment has been minimized.

Show comment
Hide comment
@sabiwara

sabiwara Dec 4, 2017

(I opened the issue as recommended here: https://github.com/elm-lang/core/issues/322, but this is just an API idea, definitively not a bug. Sorry in advance if this is not the good place to post it!)

sabiwara commented Dec 4, 2017

(I opened the issue as recommended here: https://github.com/elm-lang/core/issues/322, but this is just an API idea, definitively not a bug. Sorry in advance if this is not the good place to post it!)

@sabiwara sabiwara changed the title from Add an API to handle Random as a Task to Idea: Add an API to handle Random as a Task Dec 4, 2017

@mkwiatkowski

This comment has been minimized.

Show comment
Hide comment
@mkwiatkowski

mkwiatkowski Dec 4, 2017

@sabiwara There is already a way to generate numbers with a task: Random.step gen seed |> Task.succeed. You can combine those, but you will need to explicitly pass the seed from one step to another.

There was a discussion on Slack about this. I don't have a direct link, but here's what I have in my notes (quoting Ilias Van Peer):

The "issue" there is that you need to keep the seed in the model, which means that if you perform (or attempt) a task using random generators, then fire off a second one before the first one resolves without the final result and the updated seed; they would use the same seed. One option there is to use independentSeed (and Random.Pcg), so you can give yourself an API where you first retrieve a new independent seed, store the updated seed in your model, and kick off the task-chain with that independent seed

I hope this helps!

mkwiatkowski commented Dec 4, 2017

@sabiwara There is already a way to generate numbers with a task: Random.step gen seed |> Task.succeed. You can combine those, but you will need to explicitly pass the seed from one step to another.

There was a discussion on Slack about this. I don't have a direct link, but here's what I have in my notes (quoting Ilias Van Peer):

The "issue" there is that you need to keep the seed in the model, which means that if you perform (or attempt) a task using random generators, then fire off a second one before the first one resolves without the final result and the updated seed; they would use the same seed. One option there is to use independentSeed (and Random.Pcg), so you can give yourself an API where you first retrieve a new independent seed, store the updated seed in your model, and kick off the task-chain with that independent seed

I hope this helps!

@sabiwara

This comment has been minimized.

Show comment
Hide comment
@sabiwara

sabiwara Dec 4, 2017

Thanks for your prompt reply!
I didn't know about Random.Pcg, those examples will definitively help!

sabiwara commented Dec 4, 2017

Thanks for your prompt reply!
I didn't know about Random.Pcg, those examples will definitively help!

@evancz evancz added the request label Mar 7, 2018

@klazuka

This comment has been minimized.

Show comment
Hide comment
@klazuka

klazuka May 29, 2018

When considering this proposal, please also consider it in the light of UUID generation. I'm currently preparing a 20k LOC Elm app for 0.19 by removing the only "native" module in the codebase. That module exposed a single function, randomUuidTask : Task x String, which always succeeded with a new, random UUID. The app was modeled after elm-spa-example where each page's init returns a Task. I have 4 separate pages which need to be initialized with a random UUID and using a Task to generate the UUID composed nicely with other things that I need to do at page init time such as request data from the API server.

I'm trying to switch it over to use ports now but it's turning out to be a pretty invasive change and not nearly as clean as the old solution using the native task.

klazuka commented May 29, 2018

When considering this proposal, please also consider it in the light of UUID generation. I'm currently preparing a 20k LOC Elm app for 0.19 by removing the only "native" module in the codebase. That module exposed a single function, randomUuidTask : Task x String, which always succeeded with a new, random UUID. The app was modeled after elm-spa-example where each page's init returns a Task. I have 4 separate pages which need to be initialized with a random UUID and using a Task to generate the UUID composed nicely with other things that I need to do at page init time such as request data from the API server.

I'm trying to switch it over to use ports now but it's turning out to be a pretty invasive change and not nearly as clean as the old solution using the native task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment