Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flattening List + Future for Scala

I'm trying to await on a funky structure like this: Future[List[Future[List[Object]]]]. Await only gets rid of the outer most Future, is there a clean way of trimming this down to a simple List[Object]?

like image 798
BZapper Avatar asked Oct 07 '26 22:10

BZapper


1 Answers

Using Future.sequence:

val original: Future[List[Future[List[Object]]]] = // ...

val futureList = 
  original.flatMap(l => Future.sequence(l)) // Future[List[List[Object]]]
    .map(_.flatten)                         // Future[List[Object]]
like image 68
Jean Logeart Avatar answered Oct 09 '26 12:10

Jean Logeart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!