Create a function that takes in a generator function as its only parameter, and returns an object that can be used as both as a generator and a generator function for said generator. The idea is to be able to create an object that behaves like a generator (that is, it follows the iteration protocols) but also has the ability to restart itself.
For the rules:
SelfGenerator
is the function you're creating.sg
is the return value ofSelfGenerator
.genFunc
is the parameter passed intoSelfGenerator
.
Rules:
sg
must have astart
method that returns a generator that behaves the same as the generator that would be returned bygenFunc
.sg.start
must accept parameters that will be used to create the generator it returns such that it is the same as the generator returned by passing those parameters intogenFunc
.After
sg.start(...params)
has been executed, ifgenFunc(...params)[prop]
is callable, thensg[prop]
is also callable and will return the same value.sg.start(...params).next(value); let a = sg.next();
andsg.start(...params); sg.next(value); let a = sg.next();
must end up with the same value fora
.smallest source code wins
Example:
let sg = SelfGenerator(function*(n) {
yield n;
yield n + 1;
yield n + 2;
});
sg.start(1); // Generator
sg.next(); // {value: 1, done: false}
sg.next(); // {value: 2, done: false}
sg.start(10).next(); // {value: 10, done: false}
sg.next(); // {value: 11, done: false}
sg must also behave as a superset of the generator returned by sg.start
mean? Is this statement covered by the testcase? \$\endgroup\$