Skip to content

Instantly share code, notes, and snippets.

@loginx
Created February 3, 2011 22:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save loginx/810311 to your computer and use it in GitHub Desktop.
Save loginx/810311 to your computer and use it in GitHub Desktop.
Underscore.js mixin to emulate ruby's Array#each_slice method.
/**
* Underscore.js mixin to emulate Ruby's Enumerable#each_slice method.
* http://www.ruby-doc.org/core/classes/Enumerable.html#M001514
*
*/
_.mixin({
each_slice: function(obj, slice_size, iterator, context) {
var collection = obj.map(function(item) { return item; });
if (typeof collection.slice !== 'undefined') {
for (var i = 0, s = Math.ceil(collection.length/slice_size); i < s; i++) {
iterator.call(context, _(collection).slice(i*slice_size, (i*slice_size)+slice_size), obj);
}
}
return;
}
});
/* Example:
>>> _([1,2,3,4,5,6,7,8,9,10]).each_slice(4, function(slice) { console.log(slice); })
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment