Sample Iterator and Generator 2
by bakhshi
JavaScript
/*
process.run([1,2,3,4]);*/
'use strict';
class MapTask {
constructor(fn) {
this.fn = fn;
this.prev = null;
};
map(fn) {
const argIsFunction = typeof fn === 'function';
if (argIsFunction) {
const task = new MapTask(fn);
task.prev = this;
return task.pipe;
} else {
//run: arg is input array
}
}
run(arr) {
}
}
const natural = function*() {
let i = 1;
while (true) yield i++;
}
let process = map(a => a + 1).map(a => a * 2);
console.log(process);
function map(fn) {
return new MapTask(fn);
}
const mylist = {
[Symbol.iterator]() {
return {
next() {
if (this.count == null)
this.count = 4;
if (this.count-- > 0)
return {
value: this.count,
done: false
};
return {
value: undefined,
done: true
};
}
}
}
};
console.log(...mylist)
const nat = natural();
// generators are wired in a way to automatically return themselves as iterators
console.log(nat[Symbol.iterator]() == nat);
console.log(nat.next)