reduceRight
优质
小牛编辑
135浏览
2023-12-01
Aggregates a sequence, from the tail, into a single value according to some accumulator function.
Signature
Sequence.reduceRight = function(aggregator, memo) { /*...*/ }
Sequence.reduceRight = function reduceRight(aggregator, memo) { if (arguments.length < 2) { return this.initial(1).reduceRight(aggregator, this.last()); } // This bothers me... but frankly, calling reverse().reduce() is potentially // going to eagerly evaluate the sequence anyway; so it's really not an issue. var indexed = this.getIndex(), i = indexed.length() - 1; return indexed.reverse().reduce(function(m, e) { return aggregator(m, e, i--); }, memo); }
Name | Type(s) | Description |
---|---|---|
aggregator | Function | The function through which to pass every element in the sequence. For every element, the function will be passed the total aggregated result thus far and the element itself, and should return a new aggregated result. |
memo | * | The starting value to use for the aggregated result. |
returns | * | The result of the aggregation. |
Examples
function append(s1, s2) { return s1 + s2; } function isVowel(str) { return "aeiou".indexOf(str) !== -1; } Lazy("abcde").reduceRight(append) // => "edcba" Lazy("abcde").filter(isVowel).reduceRight(append) // => "ea"