Last active
May 6, 2023 15:39
-
-
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
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
// 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