Created
May 16, 2014 13:33
-
-
Save rizo/c03ea881ac7d40b68605 to your computer and use it in GitHub Desktop.
pairs.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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