Created
June 19, 2024 14:50
-
-
Save TonioGela/73930319be3155501ee099d334a23ee7 to your computer and use it in GitHub Desktop.
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
//> 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