File size: 1,123 Bytes
96af7c9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
class Nil {
[Symbol.iterator]() {
return this;
}
next(value) {
return { value, done: true };
}
}
Nil.nil = new Nil();
export function nilHelper() {
return Nil.nil;
}
export function* mapHelper(g, f) {
for (const v of g) {
yield f(v);
}
}
export function* flatMapHelper(g, f) {
for (const v of g) {
yield* f(v);
}
}
export function* filterHelper(g, f) {
for (const v of g) {
if (f(v)) {
yield v;
}
}
}
export function* takeNHelper(g, n) {
for (let i = 0; i < n; ++i) {
const cur = g.next();
if (cur.done) {
break;
}
yield cur.value;
}
}
export function* takeWhileHelper(g, f) {
let cur = g.next();
while (!cur.done && f(cur.value)) {
yield cur.value;
cur = g.next();
}
}
export function* joinHelper(g, others) {
for (let cur = g.next(); !cur.done; cur = g.next()) {
yield cur.value;
}
for (const s of others) {
for (let cur = s.next(); !cur.done; cur = s.next()) {
yield cur.value;
}
}
}
|