Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:39
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 dacr/63c093f10c122e60b4255a569342b26d to your computer and use it in GitHub Desktop.
Save dacr/63c093f10c122e60b4255a569342b26d to your computer and use it in GitHub Desktop.
ZIO learning - using several managed resource / published by https://github.com/dacr/code-examples-manager #3068a64d-ce93-4127-8053-637d6db9682a/d9caa6f2db4264ddff05c8d26c41403432187951
// summary : ZIO learning - using several managed resource
// keywords : scala, zio, learning, pure-functional, resources, acquire, auto-release, scope, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 3068a64d-ce93-4127-8053-637d6db9682a
// created-on : 2021-10-30T17:45:50Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.2.2"
//> using dep "dev.zio::zio:2.0.13"
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0"
// ---------------------
import zio.*, zio.worksheet.*
case class SomeResource(dest: String) {
def read(): String = s"SomeDataFor$dest"
def close(): Unit = println(s"Closing $dest")
}
val resource1 = ZIO.acquireRelease(ZIO.attempt(SomeResource("A")))(res => ZIO.succeed(res.close()))
val resource2 = ZIO.acquireRelease(ZIO.attempt(SomeResource("B")))(res => ZIO.succeed(res.close()))
val logic = ZIO.scoped(
(resource1 zip resource2).flatMap { (r1, r2) =>
Console.printLine(r1.read() + "-" + r2.read())
}
)
// -------------------------------------------------------------
logic.unsafeRun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment