Skip to content

Instantly share code, notes, and snippets.

@TonioGela
Created June 19, 2024 14:50
Show Gist options
  • Save TonioGela/73930319be3155501ee099d334a23ee7 to your computer and use it in GitHub Desktop.
Save TonioGela/73930319be3155501ee099d334a23ee7 to your computer and use it in GitHub Desktop.
//> using scala 3.4.2
//> using toolkit typelevel::latest
import cats.effect.{IO, IOApp}
import cats.effect.std.Random
import fs2.concurrent.SignallingRef
import scala.concurrent.duration.*
object Main extends IOApp.Simple:
def thingToDo: IO[Boolean] =
for
random <- Random.scalaUtilRandom[IO]
n <- random.betweenInt(0, 100)
_ <- IO.println(s"Extracted $n")
yield n < 9
def run: IO[Unit] = SignallingRef[IO].of[Boolean](false).flatMap: signal =>
fs2.Stream
.repeatEval(thingToDo.flatMap(signal.set))
.meteredStartImmediately(1.second)
.interruptWhen(signal)
.interruptAfter(1.hour)
.compile.drain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment