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

[8.x] Allow lazy collection to be instantiated from a generator #36738

Merged
merged 2 commits into from
Mar 25, 2021

Conversation

victorlap
Copy link
Contributor

Sometimes you may be in a position where you have a generator, which you would like to pass into a lazy collection. Right now you would have to do something like the following:

$source = /* some function that returns a generator */;
LazyCollection::make(function () use (&$source) {
    yield from $source;
});

With this change, you would be able to call LazyCollection::make($source); directly.

@driesvints driesvints changed the title Allow lazy collection to be instantiated from a generator [8.x] Allow lazy collection to be instantiated from a generator Mar 25, 2021
@taylorotwell taylorotwell merged commit f75e510 into laravel:8.x Mar 25, 2021
driesvints added a commit that referenced this pull request Apr 1, 2021
taylorotwell pushed a commit that referenced this pull request Apr 1, 2021
Comment on lines +72 to +95
public function testCanCreateCollectionFromGenerator()
{
$iterable = function () {
yield 1;
yield 2;
yield 3;
};
$data = LazyCollection::make($iterable());

$this->assertSame([1, 2, 3], $data->all());

$iterable = function () {
yield 'a' => 1;
yield 'b' => 2;
yield 'c' => 3;
};
$data = LazyCollection::make($iterable());

$this->assertSame([
'a' => 1,
'b' => 2,
'c' => 3,
], $data->all());
}
Copy link
Contributor

@JosephSilber JosephSilber Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests pass either way (even without the changes in this PR).

These tests do not check for laziness.

@JosephSilber
Copy link
Contributor

For anyone reading this in the future, be sure to read my comment here: #36837 (comment)

@victorlap victorlap deleted the lazy-collection-iterable branch November 10, 2021 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants