An asynchronous task like com.thoughtworks.future.continuation.Continuation
with additional benifits:
An asynchronous task like com.thoughtworks.future.continuation.Continuation
with additional benifits:
- Stack safe - Support covariant - Support both JVM and Scala.js
Given two ParallelContinuations,
each of them modifies a var
,
import com.thoughtworks.future.continuation._ import scalaz.Tags.Parallel import scalaz.syntax.all._ import com.thoughtworks.future.continuation.Continuation._ var count0 = 0 var count1 = 0 val pc0: ParallelContinuation[Unit] = Parallel(Continuation.delay { count0 += 1 }) val pc1: ParallelContinuation[Unit] = Parallel(Continuation.delay { count1 += 1 })
when map them together,
val result: ParallelContinuation[Unit] = (pc0 |@| pc1) { (u0: Unit, u1: Unit) => }
then the two vars have not been modified right now,
count0 should be(0) count1 should be(0)
when the result ParallelContinuation
get done,
then two vars should be modified only once for each.
Parallel.unwrap(result).map { _: Unit => count0 should be(1) count1 should be(1) }
Given two ParallelContinuations that contain immediate values,
import com.thoughtworks.future.continuation._ import scalaz.Tags.Parallel import scalaz.syntax.all._ import com.thoughtworks.future.continuation.Continuation._ val pc0: ParallelContinuation[Int] = Parallel(Continuation.now[Unit, Int](40)) val pc1: ParallelContinuation[Int] = Parallel(Continuation.now[Unit, Int](2))
when map them together,
val result: ParallelContinuation[Int] = (pc0 |@| pc1)(_ + _)
then the result should be a ParallelContinuation
as well,
and it is able to convert to a normal Continuation
Parallel.unwrap(result).map {
_ should be(42)
}
Author:
杨博 (Yang Bo)