-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
problem: scan(reduceFn, undefined) behaves identically to scan(reduceFn)
proposal: undefined is a valid seed value
(1) it is not consistent with Array.prototype.reduce
['a', 'b', 'c'].reduce(function(acc, s){ return acc + ' ' + s; })
"a b c"
['a', 'b', 'c'].reduce(function(acc, s){ return acc + ' ' + s; }, undefined)
"undefined a b c"
(2) with certain kinds of reducing functions, this behavior can lead to unexpected behavior
var reducer = new Rx.Subject();
reducer.scan(function(acc, fn) { return fn(acc); }, initialState);
reducer.next(function nextState(state){ /* return new state */ })if initialState is undefined, then subject will emit nextState function instead of nextState(undefined) leading to undesired behavior. Using a different value for initialState could also result in undesired behavior for example if combined with distinctUntilChanged
the scan api doc has no mention that undefined is not a valid seed value
Returns an Observable that applies a specified accumulator function to each item emitted by the source Observable. If a seed value is specified, then that value will be used as the initial value for the accumulator. If no seed value is specified, the first item of the source is used as the seed.