dash icon indicating copy to clipboard operation
dash copied to clipboard

Support generators

Open maiermic opened this issue 7 years ago • 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())));

maiermic avatar Mar 15 '18 18:03 maiermic

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

mpetrovich avatar Mar 16 '18 17:03 mpetrovich

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!

mpetrovich avatar Aug 19 '18 21:08 mpetrovich