Stage: 0, Strawman
Author: Lee Byron
Short-circuiting from a reduce
operation allows it to be more generically
useful. Much of the clojure core library is built on this premise.
// Reduce a sum or "99+"
values.reduce(function (a, v) {
if (a + v < 100) {
return a + v;
} else {
return { [Symbol.reduced]: "99+" };
}
});
This proposal suggests adding a new well known symbol @@reduced and altering the defintion of Array.prototype.reduce to respect this symbol.
One row is added to Table 1.
Specification Name | [[Description]] | Value and Purpose |
---|---|---|
@@reduced | "Symbol.reduced" | A property detected in the return value of a reduce function to indicate early completion. |
This property is new
The initial value of Symbol.reduced is the well known symbol @@reduced (Table 1).
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }
This existing method has some additions: 10.d.v - 10.d.vii
- Let O be ToObject(this value).
- ReturnIfAbrupt(O).
- Let len be ToLength(Get(O, "length")).
- ReturnIfAbrupt(len).
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- If len is 0 and initialValue is not present, throw a TypeError exception.
- Let k be 0.
- If initialValue is present, then
- a. Set accumulator to initialValue.
- Else initialValue is not present,
- a. Let kPresent be false.
- b. Repeat, while kPresent is false and k < len
- i. Let Pk be ToString(k).
- ii. Let kPresent be HasProperty(O, Pk).
- iii. ReturnIfAbrupt(kPresent).
- iv. If kPresent is true, then
- Let accumulator be Get(O, Pk).
- ReturnIfAbrupt(accumulator).
- v. Increase k by 1.
- c. If kPresent is false, throw a TypeError exception.
- Repeat, while k < len
- a. Let Pk be ToString(k).
- b. Let kPresent be HasProperty(O, Pk).
- c. ReturnIfAbrupt(kPresent).
- d. If kPresent is true, then
- i. Let kValue be Get(O, Pk).
- ii. ReturnIfAbrupt(kValue).
- iii. Let accumulator be Call(callbackfn, undefined, «accumulator, kValue, k, O»).
- iv. ReturnIfAbrupt(accumulator).
- v. Let A be ToObject(accumulator).
- vi. Let reducedPresent be HasProperty(A, @@reduced).
- vii. If reducedPresent is true, then
- Let reduced be Get(A, @@reduced).
- ReturnIfAbrupt(reduced).
- Return reduced.
- e. Increase k by 1.
- Return accumulator.