reduce
Start with first element and then apply the current and next value to the given function, when upstream complete the current value is emitted downstream.
Signature
Source.reduce
Source.reduce
Flow.reduce
Flow.reduce
Description
Start with first element and then apply the current and next value to the given function, when upstream complete the current value is emitted downstream. Similar to fold
.
Example
reduce
will take a function and apply it on the incoming elements in the Stream and only emits its result when upstream completes. Here, it will add the incoming elements.
- Scala
-
source
val source = Source(1 to 100).reduce((acc, element) => acc + element) val result: Future[Int] = source.runWith(Sink.head) result.map(println) // 5050
- Java
-
source
Source<Integer, NotUsed> source = Source.range(1, 100).reduce((acc, element) -> acc + element); CompletionStage<Integer> result = source.runWith(Sink.head(), system); result.thenAccept(System.out::println); // 5050
Reactive Streams semantics
emits when upstream completes
backpressures when downstream backpressures
completes when upstream completes
1.0.3