score:0

I don't see anything wrong with your solution either, but scalaz defines types called Semigroup and Monoid which could help you here. It also includes Semigroup definitions for many common types like List and Int. SemiGroup defines an append operation. Monoid extends Semigroup but additionally provides a zero or an initial value. This would be useful if orchestrators could be empty, in which case you would want to foldLeft instead of reduce.

Note: I reversed your type parameters

import scalaz._
import Scalaz._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

def pollAll[O, T : Semigroup](orchestrators :Seq[O], poll :(O)=>Future[T]) :Future[T] = 
 (Future sequence orchestrators.map(poll(_)))
   .map(res => res.reduce(implicitly[Semigroup[T]].append(_, _)))

or sugared a bit

def pollAll2[O, T : Semigroup](orchestrators :Seq[O], poll :(O)=>Future[T]) :Future[T] = 
 (Future traverse orchestrators)(poll(_)).map(_.reduce(_ |+| _))

output

scala> pollAll(List(1, 2, 3, 4), (i: Int) => Future.successful(i)).foreach(println)

scala> 10

scala> pollAll2(List(1, 2, 3, 4), (i: Int) => Future.successful(i)).foreach(println)

scala> 10

Related Query

More Query from same tag