Reactive-Extensions / RxJS

The Reactive Extensions for JavaScript
http://reactivex.io
Other
19.48k stars 2.1k forks source link

Documention Change #1451

Open GramParallelo opened 7 years ago

GramParallelo commented 7 years ago

'With compare function' output was incorrect.

jackmeagher commented 7 years ago

This documentation change is incorrect. If you run this code yourself, you will see that the correct output actually is 42, 42.

Note the compare function: function (a,b) { return a !== b; }

Under the hood you can see why this is the case: when iterating over the events, the last emitted "distinct" event (by default, the first event MUST be distinct) is remembered as the "currentKey". While iterating, the key you're examining at any given point is compared to the currentKey. If they're the same, it skips the element you're examining. If they're different, it stores the element you're examining as the new currentKey and moves forward and repeats.

For the stream 42, 42, 24, 24, with the admittedly annoying comparison function, what happens is as follows:

42-42-24-24
^-----------
currentKey = null
emit 42
set currentKey := 42

42-42-24-24
---^--------
currentKey = 42
compare(currentKey, 42) == false
emit 42
set currentKey := 42

42-42-24-24
------^-----
currentKey = 42
compare(currentKey, 24) == true
skip

42-42-24-24
---------^--
currentKey = 42
compare(currentKey, 24) == true
skip

So as you can see, only 42s are emitted.

gurghet commented 7 years ago

well this is just plain confusing: if the objective is make people understand, having a comparison function a!==b without pointing it out is just tricking people.