Skip to content

Instantly share code, notes, and snippets.

@lrodero
Last active March 10, 2024 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrodero/bec7a74d6528b152a88d963c8800d44b to your computer and use it in GitHub Desktop.
Save lrodero/bec7a74d6528b152a88d963c8800d44b to your computer and use it in GitHub Desktop.
Using cats-effect's MapRef, simple
//> 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