const isIterable = a => !!(a && a[Symbol.iterator]); | |
function *flat(iter) { | |
for (const a of iter) { | |
if (isIterable(a)) yield *a; | |
else yield a; | |
} | |
} | |
function *deepFlat(iter) { | |
for (const a of iter) { | |
if (isIterable(a)) yield *deepFlat(a); | |
else yield a; | |
} | |
} | |
log([...flat([1, [2], [3, [4]], [[[5], 6]]])]); | |
// [1, 2, 3, [4], [[5], 6]]; | |
log([...deepFlat([1, [2], [3, [4]], [[[5], 6]]])]); | |
// [1, 2, 3, 4, 5, 6]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment