Created
July 13, 2015 03:51
-
-
Save NickBeeuwsaert/e64c4b087e399c1a09e4 to your computer and use it in GitHub Desktop.
Python like `zip`-ping
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
| var zip = function() { | |
| var result = []; | |
| // Only zip array-like items | |
| var args = Array.prototype.filter.call(arguments, function(el){ | |
| //Make sure the argument is array-like | |
| return typeof el.length !== "undefined"; | |
| }); | |
| //if we have no items to reduce then return an empty array | |
| if(args.length === 0) return result; | |
| var minLength = args.reduce(function(l, r){ | |
| return Math.min(l.length, r.length); | |
| }); | |
| // Avoid creating a function within a loop | |
| // Lest Douglas Crockford cast his wrath on me | |
| var extract = function(e) { | |
| return e[this]; | |
| }; | |
| for(var i = 0; i < minLength; i++) { | |
| result.push(args.map(extract, i)); | |
| } | |
| return result; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment