task

Scala Steward badge CI Badge Maven Central Version

The future of scala.concurrent.Future

logo.png ___

🧰 Why this library?

Task[E, A] is a lightweight monad built on top of scala.concurrent.Future, but with a type parameter for the error channel. The advantage of this is that if a function return a Task[DomainException, Int] it can only fail with an error which is a subtype of DomainException (or a fatal exception).

Task can also be used interchangeably with the Future monad and has the same performance. This means that you do not need to learn or introduce a new effect system to your codebase and your code can use the same libraries that you are already using with Future. Finally, this library is designed to be as lightweight as possible, and has no dependencies.

Similar Libraries

If you are already used to working with typed errors and instead want to go the extra mile to change effect system I would recommend checking out ZIO or Monix BIO.

⚙️ Getting Started

Installation

Setup via build.sbt:

libraryDependencies += "io.github.ragazoor" %% "task" % "0.1.17"

Examples

📋 Migration

The goal of this library is to be as lightweight as possible, with this in mind I am reusing as much as possible from scala.concurrent.*. This makes migration easy as well, it is mostly about replacing scala.concurrent.Future with io.github.ragazoor.task.* and io.github.ragazoor.task.implicits.*.

You have to fix the code manually mainly in the following ways:

🚀 Benchmarks

Run benchmarks with

sbt "benchmark/jmh:run -i 10 -wi 10 -f 1 -t 1 io.github.ragazoor.task.TaskBenchmark"
[info] Benchmark                      Mode  Cnt   Score   Error  Units
[info] TaskBenchmark.futureFlatMap   thrpt   10  34.419 ± 1.406  ops/s
[info] TaskBenchmark.futureMap       thrpt   10  34.556 ± 0.850  ops/s
[info] TaskBenchmark.futureRecover   thrpt   10  33.102 ± 0.802  ops/s
[info] TaskBenchmark.futureSequence  thrpt   10   1.858 ± 0.019  ops/s
[info] TaskBenchmark.taskFlatMap     thrpt   10  34.451 ± 0.961  ops/s
[info] TaskBenchmark.taskMap         thrpt   10  36.490 ± 1.042  ops/s
[info] TaskBenchmark.taskMapError    thrpt   10  35.284 ± 1.302  ops/s
[info] TaskBenchmark.taskSequence    thrpt   10   1.558 ± 0.047  ops/s