How do I extend Array with functions that returns a generator object and be able to chain them?
I have tried extending the Array to see if I can chain generator functions. The first call works, but the next chain would not, because it returns a generator object and I have no idea how to extend that.
Array.prototype.select = function* (fn) {
let it = this[Symbol.iterator]();
for (let item of it) yield fn(item);
}
Array.prototype.where = function* (fn) {
let it = this[Symbol.iterator]();
for (let item of it) {
if (fn(item)) yield item;
}
}
I want to be able to chain generators like this to an array
let result = arr.select(v => v * 2)
.where(v => v % 3 === 0);
for (let item of result) console.log(item);