mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 20:07:49 +00:00
adding a reduceRight (it's in JS 1.8), and aliasing foldl and foldr
This commit is contained in:
27
index.html
27
index.html
@@ -123,10 +123,14 @@
|
|||||||
<b>Collections</b>
|
<b>Collections</b>
|
||||||
<br />
|
<br />
|
||||||
<span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
|
<span class="methods"><a href="#each">each</a>, <a href="#map">map</a>,
|
||||||
<a href="#reduce">reduce</a>, <a href="#detect">detect</a>, <a href="#select">select</a>, <a href="#reject">reject</a>, <a href="#all">all</a>,
|
<a href="#reduce">reduce</a>, <a href="#reduceRight">reduceRight</a>,
|
||||||
<a href="#any">any</a>, <a href="#include">include</a>, <a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>, <a href="#max">max</a>,
|
<a href="#detect">detect</a>, <a href="#select">select</a>,
|
||||||
<a href="#min">min</a>, <a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>, <a href="#toArray">toArray</a>,
|
<a href="#reject">reject</a>, <a href="#all">all</a>,
|
||||||
<a href="#size">size</a></span>
|
<a href="#any">any</a>, <a href="#include">include</a>,
|
||||||
|
<a href="#invoke">invoke</a>, <a href="#pluck">pluck</a>,
|
||||||
|
<a href="#max">max</a>, <a href="#min">min</a>,
|
||||||
|
<a href="#sortBy">sortBy</a>, <a href="#sortedIndex">sortedIndex</a>,
|
||||||
|
<a href="#toArray">toArray</a>, <a href="#size">size</a></span>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -195,7 +199,7 @@ _.map([1, 2, 3], function(num){ return num * 3 });
|
|||||||
|
|
||||||
<p id="reduce">
|
<p id="reduce">
|
||||||
<b class="header">reduce</b><code>_.reduce(list, memo, iterator, [context])</code>
|
<b class="header">reduce</b><code>_.reduce(list, memo, iterator, [context])</code>
|
||||||
<span class="alias">Alias: <b>inject</b></span>
|
<span class="alias">Aliases: <b>inject, foldl</b></span>
|
||||||
<br />
|
<br />
|
||||||
Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
|
Also known as <b>inject</b> and <b>foldl</b>, <b>reduce</b> boils down a
|
||||||
<b>list</b> of values into a single value. <b>Memo</b> is the initial state
|
<b>list</b> of values into a single value. <b>Memo</b> is the initial state
|
||||||
@@ -205,6 +209,19 @@ _.map([1, 2, 3], function(num){ return num * 3 });
|
|||||||
<pre>
|
<pre>
|
||||||
var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num });
|
var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num });
|
||||||
=> 6
|
=> 6
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p id="reduceRight">
|
||||||
|
<b class="header">reduceRight</b><code>_.reduceRight(list, memo, iterator, [context])</code>
|
||||||
|
<span class="alias">Alias: <b>foldr</b></span>
|
||||||
|
<br />
|
||||||
|
The right-associative version of <b>reduce</b>. Delegates to the
|
||||||
|
JavaScript 1.8 version of <b>reduceRight</b>, if it exists.
|
||||||
|
</p>
|
||||||
|
<pre>
|
||||||
|
var list = [[0, 1], [2, 3], [4, 5]];
|
||||||
|
var flat = _.reduceRight(list, [], function(a, b) { return a.concat(b); });
|
||||||
|
=> [4, 5, 2, 3, 0, 1]
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p id="detect">
|
<p id="detect">
|
||||||
|
|||||||
@@ -47,10 +47,19 @@ $(document).ready(function() {
|
|||||||
var sum = _.reduce([1, 2, 3], 0, function(sum, num){ return sum + num; });
|
var sum = _.reduce([1, 2, 3], 0, function(sum, num){ return sum + num; });
|
||||||
equals(sum, 6, 'can sum up an array');
|
equals(sum, 6, 'can sum up an array');
|
||||||
|
|
||||||
|
var context = {multiplier : 3};
|
||||||
|
sum = _.reduce([1, 2, 3], 0, function(sum, num){ return sum + num * this.multiplier; }, context);
|
||||||
|
equals(sum, 18, 'can reduce with a context object');
|
||||||
|
|
||||||
sum = _.inject([1, 2, 3], 0, function(sum, num){ return sum + num; });
|
sum = _.inject([1, 2, 3], 0, function(sum, num){ return sum + num; });
|
||||||
equals(sum, 6, 'aliased as "inject"');
|
equals(sum, 6, 'aliased as "inject"');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('collections: reduceRight', function() {
|
||||||
|
var list = _.foldr([1, 2, 3], '', function(memo, num){ return memo + num; });
|
||||||
|
equals(list, '321', 'can perform right folds');
|
||||||
|
});
|
||||||
|
|
||||||
test('collections: detect', function() {
|
test('collections: detect', function() {
|
||||||
var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
|
var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
|
||||||
equals(result, 2, 'found the first "2" and broke the loop');
|
equals(result, 2, 'found the first "2" and broke the loop');
|
||||||
|
|||||||
@@ -61,14 +61,26 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Reduce builds up a single result from a list of values. Also known as
|
// Reduce builds up a single result from a list of values. Also known as
|
||||||
// inject, or foldl.
|
// inject, or foldl. Uses JavaScript 1.8's version of reduce, if possible.
|
||||||
_.reduce = function(obj, memo, iterator, context) {
|
_.reduce = function(obj, memo, iterator, context) {
|
||||||
|
if (obj && obj.reduce) return obj.reduce(_.bind(iterator, context), memo);
|
||||||
_.each(obj, function(value, index, list) {
|
_.each(obj, function(value, index, list) {
|
||||||
memo = iterator.call(context, memo, value, index, list);
|
memo = iterator.call(context, memo, value, index, list);
|
||||||
});
|
});
|
||||||
return memo;
|
return memo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// The right-associative version of reduce, also known as foldr. Uses
|
||||||
|
// JavaScript 1.8's version of reduceRight, if available.
|
||||||
|
_.reduceRight = function(obj, memo, iterator, context) {
|
||||||
|
if (obj && obj.reduceRight) return obj.reduceRight(_.bind(iterator, context), memo);
|
||||||
|
var reversed = _.clone(_.toArray(obj)).reverse();
|
||||||
|
_.each(reversed, function(value, index) {
|
||||||
|
memo = iterator.call(context, memo, value, index, obj);
|
||||||
|
});
|
||||||
|
return memo;
|
||||||
|
};
|
||||||
|
|
||||||
// Return the first value which passes a truth test.
|
// Return the first value which passes a truth test.
|
||||||
_.detect = function(obj, iterator, context) {
|
_.detect = function(obj, iterator, context) {
|
||||||
var result;
|
var result;
|
||||||
@@ -368,6 +380,7 @@
|
|||||||
|
|
||||||
// Create a (shallow-cloned) duplicate of an object.
|
// Create a (shallow-cloned) duplicate of an object.
|
||||||
_.clone = function(obj) {
|
_.clone = function(obj) {
|
||||||
|
if (_.isArray(obj)) return obj.slice(0);
|
||||||
return _.extend({}, obj);
|
return _.extend({}, obj);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -455,7 +468,8 @@
|
|||||||
/*------------------------------- Aliases ----------------------------------*/
|
/*------------------------------- Aliases ----------------------------------*/
|
||||||
|
|
||||||
_.forEach = _.each;
|
_.forEach = _.each;
|
||||||
_.inject = _.reduce;
|
_.foldl = _.inject = _.reduce;
|
||||||
|
_.foldr = _.reduceRight;
|
||||||
_.filter = _.select;
|
_.filter = _.select;
|
||||||
_.every = _.all;
|
_.every = _.all;
|
||||||
_.some = _.any;
|
_.some = _.any;
|
||||||
|
|||||||
Reference in New Issue
Block a user