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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.