Last active
May 25, 2024 10:20
-
-
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/cc0d02339dc44c0e6351ee8cb955fb41651515ce
This file contains hidden or 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
// 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.4.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