Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generators #3

Open
maiermic opened this issue Mar 15, 2018 · 2 comments
Open

Support generators #3

maiermic opened this issue Mar 15, 2018 · 2 comments

Comments

@maiermic
Copy link

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())));
@mpetrovich
Copy link
Owner

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

@mpetrovich mpetrovich added this to the 1.x milestone Jun 16, 2018
@mpetrovich mpetrovich changed the title use iterable as return type instead of array Support non-terminating generators Aug 19, 2018
@mpetrovich mpetrovich removed this from the 1.x milestone Aug 19, 2018
@mpetrovich
Copy link
Owner

mpetrovich commented Aug 19, 2018

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 mpetrovich added this to the v2.x milestone Aug 19, 2018
@mpetrovich mpetrovich changed the title Support non-terminating generators Support generators Aug 20, 2018
@mpetrovich mpetrovich removed this from the v2.x milestone Aug 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants