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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.