I have some code like
//all data have different types
val data1Future = loadData1(params)
val data2Future = loadData2(params)
val data3Future = loadData3(params)
def saveResult(rez): Future[_] = ???
data1Future.flatMap { data1 =>
data2Future.flatMap { data2 =>
data3Future.flatMap { data3 =>
//do some computation
//several rows and several vals
val rez = ???
saveResult(rez)
}
}
}
But it is a litle bit ugly :) Unfortunatelly, I can't use for comprehension since I need something like "flatYield"
for {
data1 <- data1Future
data1 <- data1Future
data1 <- data1Future
} flatYield {
//do some computation
//several rows and several vals
val rez = data1 + data2 + data3
saveResult(rez)
}
Do you know pattern that is such elegant as "for comprehension" but with flatMap instead of map at the end of the chain?
You can do this:
This translates to
which is isomorphic to your code.