Last active
March 10, 2024 08:45
-
-
Save sshark/995ae3ebdfa7cdac2c8128a01d1ac3a9 to your computer and use it in GitHub Desktop.
Expand the example of using Random[F] in https://typelevel.org/cats-effect/docs/std/random#using-random
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package th.lim.dojo3.concurrent | |
import cats.effect.std.{Console, Random, SecureRandom} | |
import cats.effect.{IO, IOApp} | |
import cats.syntax.all.{toFlatMapOps, toFunctorOps} | |
import cats.{FlatMap, Functor} | |
/** An example of using Random[F] in additional to | |
* https://typelevel.org/cats-effect/docs/std/random#using-random | |
*/ | |
object RandomEff extends IOApp.Simple { | |
override def run: IO[Unit] = showMagicNumber("rnd",Random.scalaUtilRandom[IO]) | |
*> showMagicNumber("sec-rnd", SecureRandom.javaSecuritySecureRandom) | |
def showMagicNumber[F[_]: Console: FlatMap](id: String, rnd: F[Random[F]]): F[Unit] = | |
for { | |
given Random[F] <- rnd | |
i <- dieRoll[F] | |
_ <- Console[F].println(s"$id: $i") | |
} yield () | |
def dieRoll[F[_]: Functor: Random]: F[Int] = | |
Random[F].betweenInt(0, 6).map(_ + 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment