-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mutable-reductions friendly version of reduce (something similar to transduce / RxJava's collect) #3146
Comments
Just noticed this practice is recommended here: for toMap and toSet. As you can see above, this operations are unsafe. I would suggest changing those to "No longer implemented". |
Working implementation: export default curry(
(initialValueSupplier, reducer, source) =>
{
return Observable.concat(
source.pipe(
scan((accum, curr, idx) => {
if (idx == 0)
{
return reducer(initialValueSupplier(), curr);
}
return reducer(accum, curr);
}, null),
skip(1),
takeLast(1)
),
Observable.defer(() => Observable.of(initialValueSupplier()))
).first();
}); The additional complexity is necessary in order to avoid calling initialValueSupplier twice without breaking the no-items case (while not delaying the subscription to the source observable). |
Hi @asaf-romano :) I noticed this discussion relates to RxJS major version 4. As the current major version is 6 I guess this issue can be closed. If yes please do so, if no let me know how I can help further |
I am trying to use a Set as the accumulator value of the Scan operator. A mutable accumulator would be an amazing option! |
@tomdaniel0x01 You can solve the problem using Closing this as |
When it comes to non-primitive reductions (e.g. reducing an observable into an object or an array),
Observable.reduce
only works well with immutable reductions. Without a library like ImmutableJS, this is fine only as long as the amount of incoming items is relatively small. For high-volume observables, mutable reductions are a better option. While RxJS technically supports mutable reductions (nothing blocks object reuse in reduce), doing so can lead to terrible bugs when an Observable is subscribed multiple times. Consider the following:This is a common issue with mutable reductions that has come up in many libraries, and is very often addressed by providing another reduction operator that - in one way or the other - takes an initial value supplier rather than the initial value itself. This also applies to other Rx implementations, including RxJava, which implements such an operator as "collect" (there it simply takes an initial value supplier) and RxJS 4, which implemented the more powerful/generic/complex transducers protocol.
The text was updated successfully, but these errors were encountered: