Parallel-tagged type of Task that needs to be executed in parallel when using an cats.Applicative instance
Parallel-tagged type of Task that needs to be executed in parallel when using an cats.Applicative instance
Given a momoized Task,
import com.thoughtworks.zerocost.task._ val futureA: Task[String] = Task.execute("a").toFuture.toTask
and two ParallelTask
s that throw exceptions,
import com.thoughtworks.zerocost.parallel.Parallel def futureB(a: String): ParallelTask[String] = Parallel(Task.execute { throw new Exception("b failed"); a + "b" }) def futureC(a: String): ParallelTask[String] = Parallel(Task.execute { throw new Exception("c failed"); a + "c" })
and a Task
that depends on two String values.
def futureD(b: String, c: String): Task[String] = Task.execute(b + c + "d")
When combining those futures together,
val futureResult = futureA.flatMap { a => val Parallel(tupled) = futureB(a) product futureC(a) tupled.flatMap { case (b, c) => futureD(b, c) } }
then multiple exceptions should be handled together.
futureResult.handleError { case MultipleException(throwables) if throwables.map(_.getMessage) == Set("b failed", "c failed") => "Multiple exceptions handled" }.map { _ should be("Multiple exceptions handled") }.toFuture
Extension methods for scala.concurrent.Future
An asynchronous task.
An asynchronous task.
Unlike scala.concurrent.Task, this Task is not memoized by default.
var count = 0 val notMemoized = Task.delay { count += 1 } count should be(0); ( for { _ <- notMemoized _ = count should be(1) _ <- notMemoized _ = count should be(2) _ <- notMemoized } yield (count should be(3)) ).toFuture,
A Task can be memoized manually by converting this Task to a scala.concurrent.Task and then converting back.
var count = 0 val notMemoized = Task.delay { count += 1 } val memoized = notMemoized.toFuture.toTask; ( for { _ <- memoized _ = count should be(1) _ <- memoized _ = count should be(1) _ <- memoized } yield (count should be(1)) ).toFuture
ZeroCostTaskOps for methods available on this Task.
ParallelTask for parallel version of this Task.
Extension methods for UnitContinuation
Extension methods for Task
The name space that contains task.Task and utilities for
Task
.Usage
Features of task.Task are provided as implicit views or type classes. To enable those features, import all members under task along with Scalaz syntax.
Author:
杨博 (Yang Bo)