Sink.reduce

Apply a reduction function on the incoming elements and pass the result to the next invocation.

Sink operators

Signature

def reduce[T](f: (T, T) => T): Sink[T, Future[T]]

Description

Apply a reduction function on the incoming elements and pass the result to the next invocation. The first invocation receives the two first elements of the flow.

Materializes into a Future that will be completed by the last result of the reduction function.

cancels never

backpressures when the previous reduction function invocation has not yet completed

Example

Scala
sourceval source = Source(1 to 10)
val result = source.runWith(Sink.reduce[Int]((a, b) => a + b))
result.map(println)(system.dispatcher)
// 55
Java
sourceSource<Integer, NotUsed> ints = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
CompletionStage<Integer> sum = ints.runWith(Sink.reduce((a, b) -> a + b), materializer);
sum.thenAccept(System.out::println);
// 55
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.