(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

@@ -12,11 +12,11 @@ $(document).ready(function() {
var counts = _(lyrics).chain()
.map(function(line) { return line.split(''); })
.flatten()
.reduce({}, function(hash, l) {
.reduce(function(hash, l) {
hash[l] = hash[l] || 0;
hash[l]++;
return hash;
}).value();
}, {}).value();
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() {
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');
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');
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"');
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');
});
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');
});