Yielding conditionally in the middle of a generator function

Viewed 151

Here is my current example code:

function PrintStuff()
{
    var gen = CommentGenerator();
    var out = '';

    // Setup array with some I-type and L-type values
    var lines = ['I1', 'I1.1', 'I1.2', 'L1', 'L1.2', 'L2'];

    // Push some random RL-type values into the array
    for(let i = 1; i < 1 + Math.floor(Math.random() * 10); i++)
    {
        lines.push(`RL${i.toString()}`);
        lines.push(`RL${i.toString()}.1`);
        lines.push(`RL${i.toString()}.2`);
        lines.push(`RL${i.toString()}.3`);
    }

    // Push a couple of O-type values
    lines.push('O10');
    lines.push('O10.1');
    lines.push('O10.2');

    var r = 3;

    for(let i = 0; i < lines.length; i++)
    {
        var line = lines[i];

        if(line.indexOf('RL') > -1)
        {
            out += gen.next(r).value;
            out += `${lines[i]}\n`;
            r++;
        }
        else
        {
            out += gen.next().value;
            out += `${lines[i]}\n`;
        }
    }

    console.log(out)
}

function* CommentGenerator(v)
{
    yield '# Input 1\n';
    yield '';
    yield '';
    yield '# Layer 1\n';
    yield '';
    yield '# Layer 2\n';

    while(typeof v !== 'undefined')
    {
        yield `# RLayer ${v}\n`;
    }

    yield '# Output 1\n';

    while(true)
    {
        yield '';
    }
}

PrintStuff();

And this is the current (wrong) example output:

# Input 1
I1
I1.1
I1.2
# Layer 1
L1
L1.2
# Layer 2
L2
# Output 1
RL1
RL1.1
RL1.2
RL1.3
RL2
RL2.1
RL2.2
RL2.3
O10
O10.1
O10.2

The idea here is that we have an array set up with some static "I"-type and "L"-type values, then we have n-amount of some "RL"-type values and finally some more "O"-type values. The generator function is set up like so that when we call it, it correctly prints the # Input 1, # Layer 1 and # Layer 2 -headers before the values, but it breaks when it is supposed to start printing # Layer n -headers for as long as we keep calling the generator function with some value, after it has yielded # Input 1, # Layer 1 and # Layer 2 -headers.

The output is supposed to look like this:

# Input 1
I1
I1.1
I1.2
# Layer 1
L1
L1.2
# Layer 2
L2
# RLayer 1
RL1
RL1.1
RL1.2
RL1.3
# RLayer 2
RL2
RL2.1
RL2.2
RL2.3
# RLayer 3
RL3
RL3.1
RL3.2
RL3.3
# RLayer 4
RL4
RL4.1
RL4.2
RL4.3
# RLayer 5
RL5
RL5.1
RL5.2
RL5.3
# Output 1
O10
O10.1
O10.2

There is most likely something I don't understand about generators/yield. In the example code you can see my current logic / thought process, but it is not working as I expect it to.

What am I missing here?

1 Answers

This approach store the r value in l and omits a new headline, if equal.

Beside this, I suggest not to use a generator, but a simple array with the headlines.

function PrintStuff() {
  var gen = CommentGenerator();
  var out = '';

  // Setup array with some I-type and L-type values
  var lines = ['I1', 'I1.1', 'I1.2', 'L1', 'L1.2', 'L2'];

  // Push some random RL-type values into the array
  for (let i = 1; i < 1 + Math.floor(Math.random() * 10); i++) {
    lines.push(`RL${i.toString()}`);
    lines.push(`RL${i.toString()}.1`);
    lines.push(`RL${i.toString()}.2`);
    lines.push(`RL${i.toString()}.3`);
  }

  // Push a couple of O-type values
  lines.push('O10');
  lines.push('O10.1');
  lines.push('O10.2');

  var l;

  for (let i = 0; i < lines.length; i++) {
    let line = lines[i];

    if (line.startsWith('RL')) {
        let r = +line.match(/\d+/);
        if (r !== l) out += gen.next(r).value;
        out += `${lines[i]}\n`;
        l = r;
    } else {
      out += gen.next().value;
      out += `${lines[i]}\n`;
    }
  }

  console.log(out)
}

function* CommentGenerator() {
    yield '# Input 1\n';
    yield '';
    yield '';
    yield '# Layer 1\n';
    yield '';

    let v = yield '# Layer 2\n';

    while (typeof v !== 'undefined') {
        v = yield `# RLayer ${v}\n`;
    }

    yield '# Output 1\n';

    while (true) yield '';
}

PrintStuff();
.as-console-wrapper { max-height: 100% !important; top: 0; }

Related