At a discussion about Javascript generators someone cooked up an interesting function:
function *foo() {
var y = 1;
while(true) yield (y = (y * (yield)));
}
Now, this function can be used via:
var results = [];
var bar = foo();
bar.next();
results.push(bar.next(2));
bar.next();
results.push(bar.next(2));
bar.next();
results.push(bar.next(2));
bar.next();
results.push(bar.next(2));
As we can see, in order to get the next product we need to call next
twice. The question is: can this somehow be resolved with a single yield?
Just yield y and then multiply y with its result.