mpetrovich/dash

Support generators

maiermic opened this issue · 2 comments

Operators like filter and map take an iterable but return an array. All values of the iterable have to be iterated during evaluation. The following example will run out of memory because ints is an infinite iterable:

function ints() {
    $counter = 0;
    while (true) {
        yield $counter;
        ++$counter;
    }
}

$result = Dash\_chain(ints())
    ->filter('Dash\isEven')
    ->take(3)
    ->join(', ')
    ->value();

The same works fine if you use iterables as intermediate results like nikic/iter does

$result = iter\join(', ',
    iter\take(3,
        iter\filter('Dash\isEven',
            ints())));

Very interesting, we hadn’t considered non-terminating generators. Thanks for raising this issue, we’ll take a look and offer a solution.

Support for generators (including non-terminating ones) has been added in the generators branch.

Edit: Please note that in the initial alpha release, only filter() and take() support generators. Future alpha releases will add generator support to additional operations.

Take a look and let us know what you think!