-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleReduceLeft.scala
48 lines (42 loc) · 1.31 KB
/
ExampleReduceLeft.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package FuturesAndPromises
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.{Failure, Success}
/**
* Scala also provides a Future.reduceLeft() function which has a similar behaviour.
* Unlike foldLeft(), however, reduceLeft() does not allow you to provide a default value
* */
object ExampleReduceLeft {
def checkIsBig(input: Int): Future[Option[Int]] = {
Future(
if (input > 0) {
Some(20)
} else {
None
}
)
}
def main(args: Array[String]): Unit = {
val futures = List(
checkIsBig(1),
checkIsBig(-1),
checkIsBig(8),
checkIsBig(9),
checkIsBig(11),
checkIsBig(7)
)
// Call Future.reduceLeft to fold over futures results from left to right
// we cannot provide a default value and hence the accumulator is of type Option[Int]
val futureFoldLeft = Future.reduceLeft(futures) {
case (accumulator, some) =>
accumulator.map(result => result + some.getOrElse(0))
}
futureFoldLeft onComplete {
case Success(results) =>
println(s"Aggregate result: $results")
case Failure(e) =>
println(s"Error processing future operations, error = ${e.getMessage}")
}
Thread.sleep(100)
}
}