1
\$\begingroup\$

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 of SelfGenerator.

  • genFunc is the parameter passed into SelfGenerator.

Rules:

  • sg must have a start method that returns a generator that behaves the same as the generator that would be returned by genFunc.

  • 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 into genFunc.

  • After sg.start(...params) has been executed, if genFunc(...params)[prop] is callable, then sg[prop] is also callable and will return the same value.

  • sg.start(...params).next(value); let a = sg.next(); and sg.start(...params); sg.next(value); let a = sg.next(); must end up with the same value for a.

  • 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}
\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! You have tagged this code-golf and javascript but you have not mentioned either of those things in the body of the question. Is it meant to be code-golf? Is this meant to be Javascript only? Your question is also lacking on expectations for answers. The sentence at the top is a good start and the test cases are helpful but this really needs a thorough explanation of what makes an answer valid or invalid. \$\endgroup\$ – Post Rock Garf Hunter Aug 8 '19 at 20:42
  • 1
    \$\begingroup\$ @SriotchilismO'Zaic Well I have no idea how this challenge would apply to other languages. I did it in JavaScript because accomplishing this in JavaScript specifically was a fun challenge and required decent knowledge of how js object behave. \$\endgroup\$ – Mason Aug 8 '19 at 20:49
  • 1
    \$\begingroup\$ @SriotchilismO'Zaic I would say that it's perfectly justified to place such a restriction here. Many languages don't even have a concept of functions, much less generators and objects, and trying to phrase this in a language-agnostic way would be next to impossible. \$\endgroup\$ – Esolanging Fruit Aug 8 '19 at 23:50
  • 1
    \$\begingroup\$ What does sg must also behave as a superset of the generator returned by sg.start mean? Is this statement covered by the testcase? \$\endgroup\$ – tsh Aug 9 '19 at 1:45
  • 1
    \$\begingroup\$ It's not clear to me how multiple invocations should behave: a generator function should allow the creation of independent generators, but what if the wrapped generator has side-effects? \$\endgroup\$ – Peter Taylor Aug 10 '19 at 8:25

Browse other questions tagged or ask your own question.