Skip to content

Instantly share code, notes, and snippets.

@rla
Created June 15, 2016 16:34
Show Gist options
  • Save rla/d81e2c00a5f319299fc86ceb5f60d2b7 to your computer and use it in GitHub Desktop.
Save rla/d81e2c00a5f319299fc86ceb5f60d2b7 to your computer and use it in GitHub Desktop.
var flatten = (a, r, cb) => {
if (typeof a.length === 'undefined') {
r.push(a);
return cb;
} else if (a.length === 0) {
return cb;
} else {
return flatten(a[0], r, () => flatten(a.slice(1), r, cb));
}
};
var a = [1,2,[3,4,[5]],6];
var r = [];
var cb = () => flatten(a, r, () => console.log(r));
while (cb = cb());
@slikts
Copy link

slikts commented Jun 18, 2016

This is still recursive, though, since flatten() calls itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment