Created
June 15, 2016 16:34
-
-
Save rla/d81e2c00a5f319299fc86ceb5f60d2b7 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
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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is still recursive, though, since
flatten()
calls itself.