Front-end_dev
simple iterator(generator) example 본문
const log = console.log;
const isIterable = a => !!(a && a[Symbol.iterator]);
function *flatten(iter) {
for(const a of iter) {
// if(!isIterable(a)) for(const b of a) yield b;
if(!isIterable(a)) yield* b;
else yield a;
}
}
function *map(f, iter) {
for(const a of iter) yield f(a);
}
const flatMap = (f, iter) => flatten(map(f,iter));
log([...flatten([[1,2], [3,4]])]);
// [1,2,3,4]
log([...flatMap(a => [a,a], [1,2,3])]);
// [1,1,2,2,3,3]
'ES6 > Syntax' 카테고리의 다른 글
dynamically assigning name to function in ES6 & C++ (0) | 2018.12.07 |
---|---|
2차원배열 생성기 및 인덱스범위 벗어날시 에러 강제 (0) | 2018.11.09 |
Promise 몸풀기 (0) | 2018.08.06 |
ES7 decorator (0) | 2017.12.24 |
디자인패턴... (0) | 2017.04.05 |