Last active
March 10, 2024 12:46
-
-
Save lrodero/bec7a74d6528b152a88d963c8800d44b to your computer and use it in GitHub Desktop.
Using cats-effect's MapRef, simple
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.3 | |
//> using dep org.typelevel::cats-effect:3.5.4 | |
import cats.effect.{IO, IOApp} | |
import cats.effect.std.MapRef | |
object SimpleMapRef extends IOApp.Simple: | |
private val simpleSample = | |
for | |
mr <- MapRef.ofSingleImmutableMap[IO, Int, String]() | |
_ <- mr.setKeyValue(1, "one") | |
_ <- mr(1).get.flatMap: | |
case None => IO.raiseError(new NoSuchElementException("Not found value for key 1, this is actually impossible")) | |
case Some(v) => IO.println(s"Found value $v for key 1") | |
yield () | |
override val run = simpleSample |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment