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