Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass in an empty generator parameter?

I have a method which takes a generator plus some additional parameters and returns a new generator:

function merge(\Generator $carry, array $additional)
{
    foreach ( $carry as $item ) {
        yield $item;
    }
    foreach ( $additional as $item ) {
        yield $item;
    }
}

The usual use case for this function is similar to this:

function source()
{
    for ( $i = 0; $i < 3; $i++ ) {
        yield $i;
    }
}

foreach ( merge(source(), [4, 5]) as $item ) {
    var_dump($item);
}

But the problem is that sometimes I need to pass empty source to the merge method. Ideally I would like to be able to do something like this:

merge(\Generator::getEmpty(), [4, 5]);

Which is exactly how I would do in C# (there is a IEnumerable<T>.Empty property). But I don't see any kind of empty generator in the manual.

I've managed to work around this (for now) by using this function:

function sourceEmpty()
{
    if ( false ) {
        yield;
    }
}

And this works. The code:

foreach ( merge(sourceEmpty(), [4, 5]) as $item ) {
    var_dump($item);
}

correctly outputs:

int(4)
int(5)

But this is obviously not an ideal solution. What would be the proper way of passing an empty generator to the merge method?

like image 939
Maciej Sz Avatar asked Aug 21 '14 14:08

Maciej Sz


1 Answers

Bit late, but needed an empty generator myself, and realized creating one is actually quite easy...

function empty_generator(): Generator
{
    yield from [];
}

Don't know if that's better than using the EmptyIterator, but this way you get exactly the same type as non-empty generators at least.

like image 99
Svish Avatar answered Oct 14 '22 20:10

Svish