|
Scala example source code file (MVarUsage.scala)
The MVarUsage.scala Scala example source codepackage scalaz package example package concurrent import scalaz._ import effect._ import IO._ import concurrent._ import MVar._ import std.anyVal._ import syntax.equal._ object MVarUsage extends App { def forkIO(f: => IO[Unit])(implicit s: Strategy): IO[Unit] = IO { s(f.unsafePerformIO); () } def out() { def calc(mvar: MVar[Int]): IO[Unit] = mvar.put(42) val io = for { mvar <- newEmptyMVar[Int] _ <- forkIO(calc(mvar)) a <- mvar.take } yield a assert(io.unsafePerformIO === 42) } def inout() { def calc(in: MVar[Int], out: MVar[Int]): IO[Unit] = for { a <- in.take b <- in.take _ <- out.put(a * b) } yield () val io = for { in <- newMVar(6) out <- newEmptyMVar[Int] _ <- forkIO(calc(in, out)) _ <- in.put(7) a <- out.take } yield a assert(io.unsafePerformIO === 42) } def pingpong() { def pong(c: MVar[String], p: MVar[String]) = for { _ <- c.take flatMap (s => putStrLn("c: " + s)) _ <- p.put("pong") _ <- c.take flatMap (s => putStrLn("c: " + s)) _ <- p.put("pong") } yield () def io = for { c <- newMVar("ping") p <- newEmptyMVar[String] _ <- forkIO(pong(c, p)) _ <- p.take flatMap (s => putStrLn("p: " + s)) _ <- c.put("ping") _ <- p.take flatMap (s => putStrLn("p: " + s)) } yield () io.unsafePerformIO } //out() //inout() pingpong() } Other Scala examples (source code examples)Here is a short list of links related to this Scala MVarUsage.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.