Future.sequence(futures).map vs futures.foldLeft


#1

Hi,

following Scala with Cats, I am wondering if two pieces of code below are the same or there is some difference:

val futures: Iterator[Future[B]] = …
Future.sequence(futures) map { iterable =>
iterable.foldLeft(Monoid[B].empty)(_ |+| _)
}

val futures: Iterator[Future[B]] = …
futures.foldLeft(Monoid[Future[B]].empty)(_ |+| _)

Are futures in second case still executed in parallel?

Thank you.

regards,
peter


#2

Whether they are executed in parallel depends on whether the futures have already been initialised, and the execution context configuration. Unless it’s a lazy stream or something then they should already be executing by the time this code is reached.