Skip to content

Commit 781c914

Browse files
aded new examples
1 parent 593b1ed commit 781c914

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
idea/
1+
.idea
22

3-
target/
3+
target
44

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

0 commit comments

Comments
 (0)