I'd use `yield from` more if I didn't have to re-index arrays before yielding from them: function numbers() { yield from [1,2,3]; yield from [4,5,6]; }; foreach (numbers() as $k => $v) { echo "$k => $v\n"; } 0 => 1 1 => 2 2 => 3 0 => 4 1 => 5 2 => 6
PHP generators/iterators is a hot mess. I don't know any other language which forces iterators to output a key for `foreach(... as $key => ...)`.
"It's a hot mess" - someone who knows absolutely nothing about what they're talking about.
PHP watched the absolute disaster of python 2 to 3 version upgrade and rightfully decided that it will be as much backwards compatible as possible. PHP 10 with a clean slate is a cute idea that would wreak havoc. Perl, or whatever it is called now, tried this and it was a total disaster.
I personally never perceived Python 2 to 3 as a disaster, and the language has come out better on this end. It would have been a disaster to keep all the cruft of Python 2 forever and keep writing software in it. When Python 3 became stable, I simply never looked back at Python 2 again. Sure, many libraries had to change a bit, to accommodate Python 3. But any sane project would start with Python 3 anyway, and over t…
I was maintaining some debian servers back then, and for more than a decade there was great confusion around python. What you describe as "Sure, many libraries had to change a bit" was a major pain in the butt for many people for many years. I'd describe this as a disaster. The "just start a new project"-mentality is not what happens on servers and companies of the world. They support stacks, that are literary decades old.
BTW: https://www.php.net/manual/en/iterator.key.php It's literally in the interface.
I’m not sure I follow, what exactly is your complaint? The Iterator interface is described as: > Interface for external iterators or objects that can be iterated themselves internally Note “external iterators or objects”. The Iterator interface is not exactly everyday PHP, it’s a specialist utility for making classes iterable so they can be accessed like arrays. Most developers will rarely use it directly, and it’s n…
> Iterating over something requires knowing where you are in the sequence, so of course you would need to implement a method to get the current position of the iteration.
No you don't. Other languages don't require it. There is no issue to get a position outside of iterator and it's more generic approach.
Genuine question, what kind of problems would using fibers help with? To be clear, my point wasn’t that I think fibers are useless or that people shouldn’t use them. I think it was a great addition. Just that they wouldn’t be directly useful to the average PHP dev, until they’re being used by frameworks/libraries/extensions, say for example: a HTTP library that can fire off multiple requests asynchronously.
One of my standard answers here is a chat server. You've got thousands upon thousands of connections to it, which are all live, and they're cross-talking with each other in arbitrary ways. The easiest way to write that is to assign a fiber per connection. You shouldn't do that in PHP today. "Just that they wouldn’t be directly useful to the average PHP dev," But you're restating my point, whether you realize it or no…
>>The easiest way to write that is to assign a fiber per connection. You shouldn't do that in PHP today.
Can I ask why? Is the implementation not performant enough?
I’m not sure I follow, what exactly is your complaint? The Iterator interface is described as: > Interface for external iterators or objects that can be iterated themselves internally Note “external iterators or objects”. The Iterator interface is not exactly everyday PHP, it’s a specialist utility for making classes iterable so they can be accessed like arrays. Most developers will rarely use it directly, and it’s n…
> Iterating over something requires knowing where you are in the sequence, so of course you would need to implement a method to get the current position of the iteration. No you don't. Other languages don't require it. There is no issue to get a position outside of iterator and it's more generic approach.
I don’t think you understand the purpose of PHP’s Iterator interface.
> Iterating over something requires knowing where you are in the sequence, so of course you would need to implement a method to get the current position of the iteration. No you don't. Other languages don't require it. There is no issue to get a position outside of iterator and it's more generic approach.
I don’t think you understand the purpose of PHP’s Iterator interface.
Let me guess. To somehow patch iteration on associative arrays? And instead of bringing pairs or tuples as first class citizens it extends iterators with `key` and `value`. And now any Iterator implementation should track own sequentially increasing key. Very nice design indeed.
I don’t think you understand the purpose of PHP’s Iterator interface.
Let me guess. To somehow patch iteration on associative arrays? And instead of bringing pairs or tuples as first class citizens it extends iterators with `key` and `value`. And now any Iterator implementation should track own sequentially increasing key. Very nice design indeed.
Associative arrays are iterable internally without needing an interface or key tracking. You can just foreach them.
> Let me guess.
This just proves my point, you call this a “hot mess” whilst completely misunderstanding what it’s even used for.
The Iterator interface isn’t even used in the comment you first replied to.