지연 평가를 하는 함수의 종류이다.
const take = (l, iter) => {
let res = [];
for (const a of iter) {
res.push(a);
if (res.length === l) return res
}
return res
}
console.log(take(5, range(100)))
const L = {};
L.map = function *(f, iter) {
for (const a of iter) {
yield f(a)
}
}
let it = L.map(a => a + 10, [1, 2, 3])
const L = {};
L.filter = function *(f, iter) {
for (const a of iter) {
if (f(a)) yield a
}
}
let it = L.filter(a => a + 10, [1, 2, 3])
const L = {};
const isIterable = a => a && a[Symbol.iterator];
L.flatten = function *(iter) {
for (const a of iter) {
if(isIterable(a)) {
for (const b of a) yield b;
}
else yield a;
}
}