(breaking change) moving _.reduce's method signature to that of ECMA5. _.reduce(obj, iterator, memo). Updated tests and docs.

This commit is contained in:
Jeremy Ashkenas
2010-07-15 09:50:55 -04:00
parent e81a2ec516
commit 9827f87611
4 changed files with 28 additions and 22 deletions

View File

@@ -216,10 +216,10 @@ var lyrics = [
_(lyrics).chain() _(lyrics).chain()
.map(function(line) { return line.words.split(' '); }) .map(function(line) { return line.words.split(' '); })
.flatten() .flatten()
.reduce({}, function(counts, word) { .reduce(function(counts, word) {
counts[word] = (counts[word] || 0) + 1; counts[word] = (counts[word] || 0) + 1;
return counts; return counts;
}).value(); }, {}).value();
=&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre> =&gt; {lumberjack : 2, all : 4, night : 2 ... }</pre>
@@ -261,7 +261,7 @@ _.map([1, 2, 3], function(num){ return num * 3 });
=&gt; [3, 6, 9]</pre> =&gt; [3, 6, 9]</pre>
<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, iterator, memo, [context])</code>
<span class="alias">Aliases: <b>inject, foldl</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
@@ -270,12 +270,12 @@ _.map([1, 2, 3], function(num){ return num * 3 });
<b>iterator</b>. <b>iterator</b>.
</p> </p>
<pre> <pre>
var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num }); var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num }, 0);
=&gt; 6 =&gt; 6
</pre> </pre>
<p id="reduceRight"> <p id="reduceRight">
<b class="header">reduceRight</b><code>_.reduceRight(list, memo, iterator, [context])</code> <b class="header">reduceRight</b><code>_.reduceRight(list, iterator, memo, [context])</code>
<span class="alias">Alias: <b>foldr</b></span> <span class="alias">Alias: <b>foldr</b></span>
<br /> <br />
The right-associative version of <b>reduce</b>. Delegates to the The right-associative version of <b>reduce</b>. Delegates to the
@@ -285,7 +285,7 @@ var sum = _.reduce([1, 2, 3], 0, function(memo, num){ return memo + num });
</p> </p>
<pre> <pre>
var list = [[0, 1], [2, 3], [4, 5]]; var list = [[0, 1], [2, 3], [4, 5]];
var flat = _.reduceRight(list, [], function(a, b) { return a.concat(b); }); var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
=&gt; [4, 5, 2, 3, 0, 1] =&gt; [4, 5, 2, 3, 0, 1]
</pre> </pre>

View File

@@ -12,11 +12,11 @@ $(document).ready(function() {
var counts = _(lyrics).chain() var counts = _(lyrics).chain()
.map(function(line) { return line.split(''); }) .map(function(line) { return line.split(''); })
.flatten() .flatten()
.reduce({}, function(hash, l) { .reduce(function(hash, l) {
hash[l] = hash[l] || 0; hash[l] = hash[l] || 0;
hash[l]++; hash[l]++;
return hash; return hash;
}).value(); }, {}).value();
ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song'); ok(counts['a'] == 16 && counts['e'] == 10, 'counted all the letters in the song');
}); });

View File

@@ -53,22 +53,22 @@ $(document).ready(function() {
}); });
test('collections: reduce', function() { test('collections: reduce', function() {
var sum = _.reduce([1, 2, 3], 0, function(sum, num){ return sum + num; }); var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; }, 0);
equals(sum, 6, 'can sum up an array'); equals(sum, 6, 'can sum up an array');
var context = {multiplier : 3}; var context = {multiplier : 3};
sum = _.reduce([1, 2, 3], 0, function(sum, num){ return sum + num * this.multiplier; }, context); sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num * this.multiplier; }, 0, context);
equals(sum, 18, 'can reduce with a context object'); 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], function(sum, num){ return sum + num; }, 0);
equals(sum, 6, 'aliased as "inject"'); equals(sum, 6, 'aliased as "inject"');
sum = _([1, 2, 3]).reduce(0, function(sum, num){ return sum + num; }); sum = _([1, 2, 3]).reduce(function(sum, num){ return sum + num; }, 0);
equals(sum, 6, 'OO-style reduce'); equals(sum, 6, 'OO-style reduce');
}); });
test('collections: reduceRight', function() { test('collections: reduceRight', function() {
var list = _.foldr([1, 2, 3], '', function(memo, num){ return memo + num; }); var list = _.foldr([1, 2, 3], function(memo, num){ return memo + num; }, '');
equals(list, '321', 'can perform right folds'); equals(list, '321', 'can perform right folds');
}); });

View File

@@ -92,8 +92,11 @@
// Reduce builds up a single result from a list of values, aka inject, or foldl. // Reduce builds up a single result from a list of values, aka inject, or foldl.
// Delegates to JavaScript 1.8's native reduce if available. // Delegates to JavaScript 1.8's native reduce if available.
_.reduce = function(obj, memo, iterator, context) { _.reduce = function(obj, iterator, memo, context) {
if (nativeReduce && obj.reduce === nativeReduce) return obj.reduce(_.bind(iterator, context), memo); if (nativeReduce && obj.reduce === nativeReduce) {
if (context) iterator = _.bind(iterator, context);
return obj.reduce(iterator, 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);
}); });
@@ -102,10 +105,13 @@
// The right-associative version of reduce, also known as foldr. Uses // The right-associative version of reduce, also known as foldr. Uses
// Delegates to JavaScript 1.8's native reduceRight if available. // Delegates to JavaScript 1.8's native reduceRight if available.
_.reduceRight = function(obj, memo, iterator, context) { _.reduceRight = function(obj, iterator, memo, context) {
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) return obj.reduceRight(_.bind(iterator, context), memo); if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
if (context) iterator = _.bind(iterator, context);
return obj.reduceRight(iterator, memo);
}
var reversed = _.clone(_.toArray(obj)).reverse(); var reversed = _.clone(_.toArray(obj)).reverse();
return _.reduce(reversed, memo, iterator, context); return _.reduce(reversed, iterator, memo, context);
}; };
// Return the first value which passes a truth test. // Return the first value which passes a truth test.
@@ -277,11 +283,11 @@
// Return a completely flattened version of an array. // Return a completely flattened version of an array.
_.flatten = function(array) { _.flatten = function(array) {
return _.reduce(array, [], function(memo, value) { return _.reduce(array, function(memo, value) {
if (_.isArray(value)) return memo.concat(_.flatten(value)); if (_.isArray(value)) return memo.concat(_.flatten(value));
memo.push(value); memo.push(value);
return memo; return memo;
}); }, []);
}; };
// Return a version of the array that does not contain the specified value(s). // Return a version of the array that does not contain the specified value(s).
@@ -293,10 +299,10 @@
// Produce a duplicate-free version of the array. If the array has already // Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm. // been sorted, you have the option of using a faster algorithm.
_.uniq = function(array, isSorted) { _.uniq = function(array, isSorted) {
return _.reduce(array, [], function(memo, el, i) { return _.reduce(array, function(memo, el, i) {
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo.push(el); if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) memo.push(el);
return memo; return memo;
}); }, []);
}; };
// Produce an array that contains every item shared between all the // Produce an array that contains every item shared between all the