adding a reduceRight (it's in JS 1.8), and aliasing foldl and foldr

This commit is contained in:
Jeremy Ashkenas
2009-10-30 09:40:19 -04:00
parent eca085a9d9
commit cb38c6ae63
3 changed files with 47 additions and 7 deletions

View File

@@ -47,10 +47,19 @@ $(document).ready(function() {
var sum = _.reduce([1, 2, 3], 0, function(sum, num){ return sum + num; });
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; });
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() {
var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });
equals(result, 2, 'found the first "2" and broke the loop');