Accumulating emissions – reduce operator

Reduce is a perfect accumulation operator. It accumulates all the emissions by the producer and emits them once it receives the onComplete notification from the producer.

Here is an example:

    fun main(args: Array<String>) { 
      Observable.range(1,10) 
      .reduce { previousAccumulation, newEmission ->        previousAccumulation+newEmission  } 
      .subscribeBy { println("accumulation $it") } 
 
       Observable.range(1,5) 
       .reduce { previousAccumulation, newEmission ->         previousAccumulation*10+newEmission  } 
       .subscribeBy { println("accumulation $it") } 
      } 

The output is shown as follows:

The reduce operator works similar to the

Get Reactive Programming in Kotlin 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.