-
Couldn't load subscription status.
- Fork 1.4k
add TRandom #4443
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
add TRandom #4443
Conversation
| else Math.nextAfter(maxExclusive, Float.NegativeInfinity) | ||
| } | ||
|
|
||
| private[zio] def nextFloatBetweenWith(minInclusive: Float, maxExclusive: Float)( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is currently duplicated with zio.random.Random, but sadly I don't think there is a good way to share this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't they just in a static object so we can reference them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah but they return ZIO effects.
| object TRandom extends Serializable { | ||
|
|
||
| trait Service { | ||
| def nextBoolean: STM[Nothing, Boolean] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a super-nitpick, I'd use USTM instead of STM[Nothing, A] wherever possible.
| buffer | ||
| } | ||
| _ <- | ||
| STM.foreach((collection.size to 2 by -1).toList)((n: Int) => nextIntBounded(n).flatMap(k => swap(n - 1, k))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can replace with foreach_.
| collection: Collection[A] | ||
| )(implicit bf: BuildFrom[Collection[A], A, Collection[A]]): STM[Nothing, Collection[A]] = | ||
| for { | ||
| bufferRef <- TRef.make(new scala.collection.mutable.ArrayBuffer[A]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refs shouldn't hold mutables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch! Blindly copied that from the random one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely a separate issue but we shouldn't be doing that in the original version either.
|
I wonder if it would make sense to have an implementation more like we have for |
|
For all intents in purposes we already do this. We just rely on I would definitely prefer not to have our own random number generator here |
|
Yeah, I just wonder about performance. Because we're generating a new |
|
Ah, I wasn't aware that we have tests for generating the same values as scala Random. Nice! In that case it does make sense to use that one. Currently it seems to be hardcoded for normal Refs. I would prefer not to have duplicated code, so what do you think about changing it to use |
|
Yes, agree with you about duplicated code. Have you seen benchmarks for Maybe we merge as is or duplicate it for now based on your preference and then pursue optimization / consolidation in a follow up PR? Unless we know that the difference really is small. |
|
I think we can go with the unoptimized version for now and update later. |
|
@mschuwalow Let me take a look. We had an issue with where those got defined on Scala 2.11 when we were originally implementing |
|
Ah looks like you got it. Should we move it into a |
|
@adamgfraser Good point. I think we can play the familiarity card and have zio. stm.random. |
|
done. The only problem I see is that it might be a bit annoying when users want to have functions of both |
|
@mijicd can you kick the failed one? |
Adds TRandom to the stm package.
This allows accessing random numbers inside of a transaction.
\cc @mijicd