The reduce operator

The preceding section covered value accumulation, whereas the reduce operation revolves around value consolidation. The reduce method makes it possible to aggregate the complete data stream into a single value. This is depicted as follows:

Suppose that we want to generate a sum of Fibonacci numbers, as follows:

fibonacciGenerator.take(10).reduce((x,y) -> x+y).subscribe(t -> {
    System.out.println(t);
});

In the preceding code, we did the following things:

  • The take operator selected the first 10 elements for the stream.
  • The reduce operator took a Bifunction of the long type. The lambda expression returns the sum of the long ...

Get Hands-On Reactive Programming with Reactor now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.