WICG / observable

Observable API proposal
https://wicg.github.io/observable/
Other
563 stars 13 forks source link

Examples suggest the order of arguments of `reduce` is backwards #38

Closed tremby closed 11 months ago

tremby commented 1 year ago

Example 4 has:

const maxY = await element.on('mousemove')
                          .takeUntil(element.on('mouseup'))
                          .map(e => e.clientY)
                          .reduce((y, soFar) => Math.max(y, soFar), 0);

The second argument of reduce is consumed as soFar, and in context "so far" represents the maximum y value the pointer has visited so far during the operation.

But that's the opposite order from the standard Array.reduce function, where the first argument is the accumulator and the second is the value currently being visited.

(I do realize that in this particular example (and indeed the others provided) the order of the two arguments doesn't actually matter, but that's beside the point.)

Other examples have the same issue.

The reduce method is later characterized:

Promise<any> reduce(Function accumulator, optional any);

I don't know exactly what this representation format is, but it suggests to me that the first argument is indeed intended to be the accumulator.

benlesh commented 1 year ago

You're right. Seems like and oversight or typo. The arguments for reduce should be the same as any other reducer. (accumulated, value) => newAccumulated