Created
October 26, 2015 10:24
-
-
Save abuseofnotation/683f21539587ba0e02e9 to your computer and use it in GitHub Desktop.
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
| /* | |
| A function that rotates a 2-dimensional array by 90 degrees. | |
| > _([ | |
| [1, 2, 3], | |
| [1, 2, 3], | |
| [1, 2, 3] | |
| ]).rotate().value() | |
| [ | |
| [ 1, 1, 1 ], | |
| [ 2, 2, 2 ], | |
| [ 3, 3, 3 ] | |
| ] | |
| Written so generic-structure objects can be arranged in HTML tables: | |
| > _([ | |
| ["john", "m", 21], | |
| ["Jane", "f", 34] | |
| ]).rotate().value() | |
| [ | |
| [ 'john', 'Jane' ], | |
| [ 'm', 'f' ], | |
| [ 21, 34 ] | |
| ] | |
| */ | |
| _.mixin({rotate: function rotate (arr) { | |
| // get the maximum length of the sub-arrays: | |
| var length = arr.reduce(function (max, inner_array) { return inner_array.length > max ? inner_array.length : max }, 0) | |
| // Init a new array of that size and fill it. | |
| return _.range(length).map(function (i) { | |
| return arr.map(function (arr) {return arr[i]}) | |
| }) | |
| }}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment