Without Future, that's how I combine all smaller Seq into one big Seq with a flatmap
category.getCategoryUrlKey(id: Int):Seq[Meta] // main method
val appDomains: Seq[Int]
val categories:Seq[Meta] = appDomains.flatMap(category.getCategoryUrlKey(_))
Now the method getCategoryUrlKey
could fail. I put a circuit breaker in front to avoid to call it for the next elements after an amount of maxFailures. Now the circuit breaker doesn't return a Seq
but a Future[Seq]
lazy val breaker = new akka.pattern.CircuitBreaker(...)
private def getMeta(appDomainId: Int): Future[Seq[Meta]] = {
breaker.withCircuitBreaker {
category.getCategoryUrlKey(appDomainId)
}
}
How to iterate through the List appDomains
and combine the result into one single Future[Seq] , possible into Seq ?
If Functional Programming is applicable, is there a way to directly transform without temporary variables ?
Future.sequence
converts Seq[Future[T]]
to Future[Seq[T]]
In your case T
is Seq
. After the sequence operation, you will end up with Seq[Seq[T]]. So Just flatten it after the sequence operation using flatten.
def squashFutures[T](list: Seq[Future[Seq[T]]]): Future[Seq[T]] =
Future.sequence(list).map(_.flatten)
Your code becomes
Future.sequence(appDomains.map(getMeta)).map(_.flatten)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With