Skip to content

Instantly share code, notes, and snippets.

@rizo
Created May 16, 2014 13:33
Show Gist options
  • Select an option

  • Save rizo/c03ea881ac7d40b68605 to your computer and use it in GitHub Desktop.

Select an option

Save rizo/c03ea881ac7d40b68605 to your computer and use it in GitHub Desktop.
pairs.js
// Requires underscore for zip function.
function slice(array, from, to, step) {
if (from===null) from=0;
if (to===null) to=array.length;
if (!step) return array.slice(from, to);
var result = Array.prototype.slice.call(array, from, to);
if (step < 0) result.reverse();
step = Math.abs(step);
if (step > 1) {
var final = [];
for (var i = result.length - 1; i >= 0; i--) {
(i % step === 0) && final.push(result[i]);
};
final.reverse();
result = final;
}
return result;
}
var numbers = [1,2,3,4,5,6];
function pairs(a) {
return = _.zip(slice(numbers, 0, numbers.length, 2),
slice(numbers, 1, numbers.length, 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment