Skip to content

Instantly share code, notes, and snippets.

@panda01
Created November 7, 2013 03:13
Show Gist options
  • Save panda01/7348364 to your computer and use it in GitHub Desktop.
Save panda01/7348364 to your computer and use it in GitHub Desktop.
Functional unflatten in js
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));
@animatedlew
Copy link

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 :)

@animatedlew
Copy link

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