Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mschuwalow
Copy link
Member

@mschuwalow mschuwalow commented Nov 24, 2020

Adds TRandom to the stm package.
This allows accessing random numbers inside of a transaction.

\cc @mijicd

else Math.nextAfter(maxExclusive, Float.NegativeInfinity)
}

private[zio] def nextFloatBetweenWith(minInclusive: Float, maxExclusive: Float)(
Copy link
Member Author

@mschuwalow mschuwalow Nov 24, 2020

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.

Copy link
Contributor

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?

Copy link
Contributor

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]
Copy link
Member

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)))
Copy link
Member

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])
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Contributor

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.

@adamgfraser
Copy link
Contributor

I wonder if it would make sense to have an implementation more like we have for TestRandom where we just carry the seed around and use it ourselves to generate random numbers instead of creating these scala.util.Random instances all the time and then calling those.

@mschuwalow
Copy link
Member Author

mschuwalow commented Nov 24, 2020

For all intents in purposes we already do this. We just rely on scala.util.Random to use the seed to generate a random value and a next seed.

I would definitely prefer not to have our own random number generator here

@adamgfraser
Copy link
Contributor

Yeah, I just wonder about performance. Because we're generating a new scala.util.Random every time we generate a random value and we have to generate an additional value each time because we need to both generate the value we are trying to generate and a new seed even though scala.util.Random has already generated a new seed internally. We already have all the code for this in TestRandom and it generates the same values as scala.util.Random for any seed. But maybe it doesn't matter in the context of a transaction. We could always pursue it later as an optimization.

@mschuwalow
Copy link
Member Author

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 TRef internally and just commit when used for TestRandom? Of course this is going to negatively affect test performance slightly.

@adamgfraser
Copy link
Contributor

Yes, agree with you about duplicated code. Have you seen benchmarks for TRef versus Ref? Random number generation is on the hot path for property based testing so I would not want to do something that would cause a significant regression there.

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.

@mschuwalow
Copy link
Member Author

I think we can go with the unoptimized version for now and update later.
BTW, do you have an idea for the test failure on 2.11? It seems to be unhappy with the type alias in the package object.

@adamgfraser
Copy link
Contributor

@mschuwalow Let me take a look. We had an issue with where those got defined on Scala 2.11 when we were originally implementing Has and ZLayer. I think it may be the same problem.

@adamgfraser
Copy link
Contributor

Ah looks like you got it. Should we move it into a zio.stm.random package object? I think that would make the naming more consistent. Like zio.random.nextInt and zio.stm.random.nextInt.

@mijicd
Copy link
Member

mijicd commented Nov 24, 2020

@adamgfraser Good point. I think we can play the familiarity card and have zio. stm.random.

@mschuwalow
Copy link
Member Author

done. The only problem I see is that it might be a bit annoying when users want to have functions of both zio.random and zio.stm.random in scope in a module. Currently we will have name clashes.

@mschuwalow
Copy link
Member Author

@mijicd can you kick the failed one?

@mijicd mijicd merged commit 4c7dafb into zio:master Nov 24, 2020
@mschuwalow mschuwalow deleted the trandom branch November 29, 2020 14:35
atooni pushed a commit to blended-zio/zio that referenced this pull request Dec 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants