How to elegantly combine multiple Tasks containing options in ZIO

1.4k views Asked by At

I'm looking for the most elegant implementation of

import scalaz.zio.Task

def combineTasks[A, B, C, D](task1: Task[Option[A]],
                             task2: Task[Option[B]],
                             task3: Task[Option[C]])
                            (f: (A, B, C) => D)
: Task[Option[D]]

using

  1. no additional dependencies
  2. scalaz-zio-interop-cats and cats
  3. scalaz-zio-interop-scalaz7x and scalaz7x

The solutions should generalize well to n arguments.

2

There are 2 answers

0
Matthias Langer On BEST ANSWER

After getting some help and doing research, I found the following implementations, that seem the most elegant to me so far:

1. Using no additional dependencies:

def combineTasks[A, B, C, D](task1: Task[Option[A]],
                             task2: Task[Option[B]],
                             task3: Task[Option[C]])
                            (f: (A, B, C) => D)
: Task[Option[D]] = {
  for {
    t1 <- task1
    t2 <- task2
    t3 <- task3
  } yield {
    (t1, t2, t3) match {
      case (Some(t1), Some(t2), Some(t3)) => Some(f(t1, t2, t3))
      case _ => None
    }
  }
}

2. Using scalaz-zio-interop-cats and cats:

def combineTasks[A, B, C, D](task1: Task[Option[A]],
                             task2: Task[Option[B]],
                             task3: Task[Option[C]])
                            (f: (A, B, C) => D)
: Task[Option[D]] = {
  import cats.implicits.catsStdInstancesForOption
  import cats.Apply
  import scalaz.zio.interop.catz._

  Apply[Task].compose[Option].map3(task1, task2, task3)(f)
}

See mapN over composed Apply for a related discussion.

3. Using scalaz-zio-interop-scalaz7x and scalaz7x:

def combineTasks[A, B, C, D](task1: Task[Option[A]],
                             task2: Task[Option[B]],
                             task3: Task[Option[C]])
                            (f: (A, B, C) => D): Task[Option[D]] = {
  import scalaz.Apply
  import scalaz.std.option._
  import scalaz.zio.interop.scalaz72._

  Apply[Task].compose[Option].apply3(task1, task2, task3)(f)
}
4
Krzysztof Atłasik On

With cats, you can combine multiple options by using mapN. So this would work:

import cats._
import cats.implicits._

val o1 = 1.some
val o2 = 2.some
val o3 = 3.some

(o1,o2,o3).mapN((a,b,c) => a |+| b |+| c) // Some(6)

Olny thing left to do is just unwrapping Tasks and since they're monads, you can do:

for {
  t1 <- task1 
  t2 <- task2 
  t3 <- task3 
} yield (t1,t2,t3)
  .mapN((a,b,c) => /* combine a,b,c */)