class NaturalsIterator extends Iterator {
constructor() {
super();
this.current = 0; // 最初の自然数の前の値
}
next() {
this.current++;
return {
value: this.current,
done: false
};
}
return () {
return {
value: undefined,
done: true
};
}
}
const naturals = new NaturalsIterator();
const ary = naturals
.drop(3)
.take(5)
.map(n => n * 2 + 1)
.toArray()
console.log(ary) // [ 9, 11, 13, 15, 17 ]
console.log(process.version); // 例: v22.14.0