Generator memo.
Generator is iterator?
PHP:
function generator () {
yield 0;
yield 1;
yield 2;
}
foreach (generator() as $n) {
echo $n."\n";
}
# 0
# 1
# 2
Python:
def generator():
yield 0
yield 1
yield 2
for n in generator():
print(n)
# 0
# 1
# 2
JavaScript:
function* generator () {
yield 0
yield 1
yield 2
}
generator().forEach(function (n) {
console.log(n)
})
// TypeError: generator(...).forEach is not a function
//that...?
A JavaScript generator is a coroutine, not an iterator? But in general, it's easier to understand a generator if you start with an understanding from iterator. But you can't touch the generator with the JavaScript iterator method.
Recommended Posts