added an _.zip()

This commit is contained in:
Jeremy Ashkenas
2009-10-26 09:15:14 -04:00
parent cf61edffa8
commit d229e822e7
4 changed files with 68 additions and 44 deletions

View File

@@ -28,35 +28,10 @@ window._ = {
return obj;
},
// Determine whether all of the elements match a truth test. Delegate to
// Javascript 1.6's every(), if it is present.
all : function(obj, iterator, context) {
iterator = iterator || function(v){ return v; };
if (obj.every) return obj.every(iterator, context);
var result = true;
_.each(obj, function(value, index) {
result = result && !!iterator.call(context, value, index);
if (!result) throw '__break__';
});
return result;
},
// Determine if at least one element in the object matches a truth test. Use
// Javascript 1.6's some(), if it exists.
any : function(obj, iterator, context) {
iterator = iterator || function(v) { return v; };
if (obj.some) return obj.some(iterator, context);
var result = false;
_.each(obj, function(value, index) {
if (result = !!iterator.call(context, value, index)) throw '__break__';
});
return result;
},
// Return the results of applying the iterator to each element. Use Javascript
// 1.6's version of map, if possible.
map : function(obj, iterator, context) {
if (obj.map) return obj.map(iterator, context);
if (obj && obj.map) return obj.map(iterator, context);
var results = [];
_.each(obj, function(value, index) {
results.push(iterator.call(context, value, index));
@@ -64,7 +39,8 @@ window._ = {
return results;
},
// Aka reduce. Inject builds up a single result from a list of values.
// Inject builds up a single result from a list of values. Also known as
// reduce, and foldl.
inject : function(obj, memo, iterator, context) {
_.each(obj, function(value, index) {
memo = iterator.call(context, memo, value, index);
@@ -104,6 +80,31 @@ window._ = {
return results;
},
// Determine whether all of the elements match a truth test. Delegate to
// Javascript 1.6's every(), if it is present.
all : function(obj, iterator, context) {
iterator = iterator || function(v){ return v; };
if (obj.every) return obj.every(iterator, context);
var result = true;
_.each(obj, function(value, index) {
result = result && !!iterator.call(context, value, index);
if (!result) throw '__break__';
});
return result;
},
// Determine if at least one element in the object matches a truth test. Use
// Javascript 1.6's some(), if it exists.
any : function(obj, iterator, context) {
iterator = iterator || function(v) { return v; };
if (obj.some) return obj.some(iterator, context);
var result = false;
_.each(obj, function(value, index) {
if (result = !!iterator.call(context, value, index)) throw '__break__';
});
return result;
},
// Determine if a given value is included in the array or object,
// based on '=='.
include : function(obj, target) {
@@ -239,6 +240,16 @@ window._ = {
});
},
// Zip together multiple lists into a single array -- elements that share
// an index go together.
zip : function() {
var args = _.toArray(arguments);
var length = _.max(_.pluck(args, 'length'));
var results = new Array(length);
for (var i=0; i<length; i++) results[i] = _.pluck(args, String(i));
return results;
},
// If the browser doesn't supply us with indexOf (I'm looking at you, MSIE),
// we need this function. Return the position of the first occurence of an
// item in an array, or -1 if the item is not included in the array.
@@ -252,7 +263,7 @@ window._ = {
/* -------------- The following methods apply to functions -----------------*/
// Create a function bound to a given object (assigning 'this', and arguments,
// optionally).
// optionally). Binding with arguments is also known as 'curry'.
bind : function(func, context) {
if (!context) return func;
var args = _.toArray(arguments).slice(2);