publish-subscribe

How to emit cumulative sum only from a reactive stream?

孤街浪徒 提交于 2020-08-10 19:57:27
问题 I've a use case where the stream should only emit when the cumulative "sum" equals or exceeds a given value, n. Let's take the example of six integers with n = 5. +---+------+---------+ | i | Emit | Sum | +---+------+---------+ | 1 | - | 1 | | 2 | - | 3 | | 3 | 5 | 1 | | 4 | 5 | 0 | | 5 | 5 | 0 | | 2 | 2 | 0 (end) | +---+------+---------+ As you can see, nothing is emitted unless the sum equals or exceeds 5, except for the last element, which is emitted anyway. Once an item is emitted, the