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

Introduce class ipl\Stdlib\CallbackFilterIterator #54

Closed
wants to merge 1 commit into from

Conversation

nilmerg
Copy link
Member

@nilmerg nilmerg commented Mar 26, 2024

This class stems from the motivation to have a filter iterator that uses callbacks, which behaves like any other iterator. Just like the SPL's CallbackFilterIterator, but that works differently: It isn't valid unless explicitly rewound first.

Any other iterator, especially generators, doesn't need to be rewound first. That's most obvious with a generator that yields conditionally, just like a filter iterator would.

Any call to valid(), key() or current() should return the same result as the very first iteration step would.

@nilmerg nilmerg self-assigned this Mar 26, 2024
@cla-bot cla-bot bot added the cla/signed label Mar 26, 2024
@nilmerg nilmerg force-pushed the add-callback-filter-iterator branch from 4c24216 to a46ac9e Compare March 26, 2024 08:58
This class stems from the motivation to have a filter iterator
that uses callbacks, which behaves like any other iterator.
Just like the SPL's `CallbackFilterIterator`, but that works
differently: It isn't valid unless explicitly rewound first.

Any other iterator, especially generators, doesn't need to be
rewound first. That's most obvious with a generator that yields
conditionally, just like a filter iterator would.

Any call to `valid()`, `key()` or `current()` should return the
same result as the very first iteration step would.
@nilmerg nilmerg force-pushed the add-callback-filter-iterator branch from a46ac9e to ed7cb0e Compare March 26, 2024 09:00
@nilmerg
Copy link
Member Author

nilmerg commented Mar 26, 2024

I refuse to put the errors reported by phpstan into the baseline. I expect phpstan to not complain about these on 7.x if it doesn't on 8.x -.-

@nilmerg
Copy link
Member Author

nilmerg commented Mar 26, 2024

It may optionally be sufficient to really call rewind() explicitly where required. Which wouldn't necessitate this custom implementation. Though, I still wonder why generators behave differently to the CallbackFilterIterator. A generator isn't just one upon reaching the first yield at runtime, it's one as PHP detects that yield is used upon parsing. So it's not much of a difference to an iterator which is created at runtime (Iike a generator as closure). But it doesn't need to be rewound first, before a call to valid(), key() or current().

So, yeah, it's an edge-case, but maybe one that pops up in the future again? And then there may be indeed the requirement for a method parameter that accepts generators and ((callback)filter)iterators.

@nilmerg nilmerg closed this Apr 8, 2024
@nilmerg nilmerg deleted the add-callback-filter-iterator branch April 8, 2024 07:02
@nilmerg nilmerg removed the request for review from lippserd April 8, 2024 07:02
@nilmerg nilmerg removed their assignment Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant