-
Notifications
You must be signed in to change notification settings - Fork 69
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
Feature request for iter\head #38
Comments
One way is to do:
But I'm not sure if that has side-effects for non-rewindable generators. It fails because the subsequent use of the generator complains about already ran generator. It works for static arrays, but generators it fails, having a |
I'm afraid this is not really possible in the case of generators. The issue is that foreach performs an implicit rewind and generators only allow rewinds on generators where no elements have been consumed yet. See for example: https://3v4l.org/X6ssY The only way to split a generator in head + tail (where the tail is still usable as a normal iterator in foreach) would be to create an entirely new generator that passes through all values, so something like this: https://3v4l.org/34hPC Due to how |
I tried the generator functions to acquire the current head, but there's immutability when passing the generator instance to 2 or more functions, so heading off the generator in one function results in a mutation of the second generator. One way to solve this is structure sharing. |
Oops I meant but there's no immutability. |
I encountered the above trying to implement a lazy unzip. For reference, see: https://gist.github.com/CMCDragonkai/2ad2359a961ff13c82327da2fea0b9d8 |
To consume from an iterable, it needs to be an Iterator which does not rewind. Decorating an iterable with a NoRewindIterator can do this:
|
I implemented a lazy Zip/Unzip as well: $c = Collection::fromIterable([1, 2])
->zip([3, 4]);
print_r($c->all()); // [[1,3], [2, 4]] Find it here: https://github.com/loophp/collection |
The taken and slice variants don't consume the generator. I need the equivalent of array_splice or head in haskell. Take the head off the iterable, and consume it, so that when I use the iterable next, it doesn't have the head anymore. How can this be done?
The text was updated successfully, but these errors were encountered: