Skip to content

Commit

Permalink
Editorial: Fix incorrect note in reduce and reduceRight. Closes #744.
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed Dec 29, 2016
1 parent 5ce3a49 commit 72dc5b3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -30578,7 +30578,7 @@ <h1>Array.prototype.push ( ..._items_ )</h1>
<emu-clause id="sec-array.prototype.reduce">
<h1>Array.prototype.reduce ( _callbackfn_ [ , _initialValue_ ] )</h1>
<emu-note>
<p>_callbackfn_ should be a function that takes four arguments. `reduce` calls the callback, as a function, once for each element present in the array, in ascending order.</p>
<p>_callbackfn_ should be a function that takes four arguments. `reduce` calls the callback, as a function, once for each element after the first element present in the array, in ascending order.</p>
<p>_callbackfn_ is called with four arguments: the _previousValue_ (value from the previous call to _callbackfn_), the _currentValue_ (value of the current element), the _currentIndex_, and the object being traversed. The first time that callback is called, the _previousValue_ and _currentValue_ can be one of two values. If an _initialValue_ was provided in the call to `reduce`, then _previousValue_ will be equal to _initialValue_ and _currentValue_ will be equal to the first value in the array. If no _initialValue_ was provided, then _previousValue_ will be equal to the first value in the array and _currentValue_ will be equal to the second. It is a *TypeError* if the array contains no elements and _initialValue_ is not provided.</p>
<p>`reduce` does not directly mutate the object on which it is called but the object may be mutated by the calls to _callbackfn_.</p>
<p>The range of elements processed by `reduce` is set before the first call to _callbackfn_. Elements that are appended to the array after the call to `reduce` begins will not be visited by _callbackfn_. If existing elements of the array are changed, their value as passed to _callbackfn_ will be the value at the time `reduce` visits them; elements that are deleted after the call to `reduce` begins and before being visited are not visited.</p>
Expand Down Expand Up @@ -30619,7 +30619,7 @@ <h1>Array.prototype.reduce ( _callbackfn_ [ , _initialValue_ ] )</h1>
<emu-clause id="sec-array.prototype.reduceright">
<h1>Array.prototype.reduceRight ( _callbackfn_ [ , _initialValue_ ] )</h1>
<emu-note>
<p>_callbackfn_ should be a function that takes four arguments. `reduceRight` calls the callback, as a function, once for each element present in the array, in descending order.</p>
<p>_callbackfn_ should be a function that takes four arguments. `reduceRight` calls the callback, as a function, once for each element after the first element present in the array, in descending order.</p>
<p>_callbackfn_ is called with four arguments: the _previousValue_ (value from the previous call to _callbackfn_), the _currentValue_ (value of the current element), the _currentIndex_, and the object being traversed. The first time the function is called, the _previousValue_ and _currentValue_ can be one of two values. If an _initialValue_ was provided in the call to `reduceRight`, then _previousValue_ will be equal to _initialValue_ and _currentValue_ will be equal to the last value in the array. If no _initialValue_ was provided, then _previousValue_ will be equal to the last value in the array and _currentValue_ will be equal to the second-to-last value. It is a *TypeError* if the array contains no elements and _initialValue_ is not provided.</p>
<p>`reduceRight` does not directly mutate the object on which it is called but the object may be mutated by the calls to _callbackfn_.</p>
<p>The range of elements processed by `reduceRight` is set before the first call to _callbackfn_. Elements that are appended to the array after the call to `reduceRight` begins will not be visited by _callbackfn_. If existing elements of the array are changed by _callbackfn_, their value as passed to _callbackfn_ will be the value at the time `reduceRight` visits them; elements that are deleted after the call to `reduceRight` begins and before being visited are not visited.</p>
Expand Down

5 comments on commit 72dc5b3

@getify
Copy link
Contributor

@getify getify commented on 72dc5b3 Dec 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be missing something, but I think this is still inaccurate/misleading. In the case where initialValue is passed, the callback is called once for every element in the array.

I think the corrected versions of these sentences are only correct if initialValue (when passed) is actually treated as the first element of the array. Is it?

@bterlson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right, the prose doesn't cover all the cases. Any wording suggestions?

@ljharb
Copy link
Member

@ljharb ljharb commented on 72dc5b3 Dec 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Once for each element in the array, skipping the first element if initialValue is omitted"?

@bterlson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me. Any thoughts @getify?

@getify
Copy link
Contributor

@getify getify commented on 72dc5b3 Dec 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:+1

Please sign in to comment.