Skip to content
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

scan doesn't accept undefined as seed value #2047

Closed
derekrjones opened this issue Oct 17, 2016 · 1 comment · Fixed by #2050
Closed

scan doesn't accept undefined as seed value #2047

derekrjones opened this issue Oct 17, 2016 · 1 comment · Fixed by #2050
Assignees
Labels
bug Confirmed bug

Comments

@derekrjones
Copy link

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.

@benlesh benlesh added the bug Confirmed bug label Oct 18, 2016
@benlesh benlesh self-assigned this Oct 18, 2016
jayphelps added a commit to jayphelps/rxjs that referenced this issue Oct 18, 2016
…ed value

Array#scan supports `undefined` as a valid seed value, so we should too.

```js
of(1, 2, 3).scan((acc, x) => acc + ' ' + x);
// "undefined 1"
// "undefined 1 2"
// "undefined 1 2 3"
```

fixes ReactiveX#2047
jayphelps added a commit to jayphelps/rxjs that referenced this issue Oct 18, 2016
…ed value

Array#scan supports `undefined` as a valid seed value, so we should too.

```js
of(1, 2, 3).scan((acc, x) => acc + ' ' + x, undefined);
// "undefined 1"
// "undefined 1 2"
// "undefined 1 2 3"
```

fixes ReactiveX#2047
jayphelps added a commit to jayphelps/rxjs that referenced this issue Oct 18, 2016
…ed value

Array#scan supports `undefined` as a valid seed value, so we should too.

```js
of(1, 2, 3).scan((acc, x) => acc + ' ' + x, undefined);
// "undefined 1"
// "undefined 1 2"
// "undefined 1 2 3"
```

fixes ReactiveX#2047
jayphelps added a commit to jayphelps/rxjs that referenced this issue Oct 19, 2016
…ed value

Array#reduce supports `undefined` as a valid seed value, so it seems
natural that we would too for scan

```js
of(1, 2, 3).scan((acc, x) => acc + ' ' + x, undefined);
// "undefined 1"
// "undefined 1 2"
// "undefined 1 2 3"
```

fixes ReactiveX#2047
jayphelps added a commit to jayphelps/rxjs that referenced this issue Oct 19, 2016
…ed value

Array#reduce supports `undefined` as a valid seed value, so it seems
natural that we would too for scan

```js
of(1, 2, 3).scan((acc, x) => acc + ' ' + x, undefined);
// "undefined 1"
// "undefined 1 2"
// "undefined 1 2 3"
```

fixes ReactiveX#2047
jayphelps added a commit that referenced this issue Oct 24, 2016
…itself as a valid seed (#2050)

* fix(scan): scan operator now accepts `undefined` itself as a valid seed value

Array#reduce supports `undefined` as a valid seed value, so it seems
natural that we would too for scan

```js
of(1, 2, 3).scan((acc, x) => acc + ' ' + x, undefined);
// "undefined 1"
// "undefined 1 2"
// "undefined 1 2 3"
```

fixes #2047

* fix(reduce): reduce operator now accepts `undefined` itself as a valid seed value

Array#reduce supports `undefined` as a valid seed value, so it seems
natural that we would too.

```js
of(1, 2, 3).reduce((acc, x) => acc + ' ' + x, undefined);
// "undefined 1 2 3"
```
@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Confirmed bug
Projects
None yet
2 participants