Created
November 7, 2013 03:13
-
-
Save panda01/7348364 to your computer and use it in GitHub Desktop.
Functional unflatten in 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
function unflatten(list, count) { | |
var ret = []; | |
// For every group | |
_.range(Math.ceil(list.length/count)).forEach(function(mul) { | |
ret.push(list.slice(mul*count, mul*count+count)); | |
}); | |
return ret; | |
} | |
console.log(unflatten(_.range(1, 13), 3)); |
Hey, check out the fork on this... I finally was able to remove that forEach and replace it with a map. This allowed me to remove the array push (since it's a side effect) to outside of the algorithm. Dopeness.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome!! I bet if I silkscreen this snippet on my shirt, I'll instantly get ladies flocking around me. This is very inspirational work, dude!
Also: I forked it so I can change the name from Unflatten to unflatten.js because github is using ace for syntax highlighting :)