Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:20
Show Gist options
  • Select an option

  • Save dacr/a5cd6b1a415ae746fe442d8bcc2da9d1 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/a5cd6b1a415ae746fe442d8bcc2da9d1 to your computer and use it in GitHub Desktop.
ZIO learning - atomic update like operations / published by https://github.com/dacr/code-examples-manager #0925a4d5-b8d9-4995-8d64-5c241958d83b/137c00b1f185ff128b78ca9255c92806d1c753fa
// summary : ZIO learning - atomic update like operations
// keywords : scala, zio, learning, pure-functional, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : 0925a4d5-b8d9-4995-8d64-5c241958d83b
// created-on : 2021-04-06T09:46:48+02:00
// 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"
// ---------------------
/*
inspired from
- [John A De Goes - ZIO: Next-Generation Effects in Scala](https://www.youtube.com/watch?v=mkSHhsJXjdc)
- +34:00
*/
import zio.*
object Encapsulated extends ZIOAppDefault {
def run = {
// Ref[A] : is a "mutable" cell that contains a reference to an A that may be updated atomically
for {
ref <- Ref.make(0)
incrementers = List.fill(100)(ref.update(_ + 1))
_ <- ZIO.foreachPar(incrementers)(identity)
v <- ref.get
_ <- Console.printLine(v)
} yield v
}
}
Encapsulated.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment