←back to thread

116 points doekenorg | 4 comments | | HN request time: 0.001s | source
Show context
Amakanski ◴[] No.44498559[source]
>> This would require a yield to syntax, which doesn't exist.

There is however the 'yield from' statement.

replies(3): >>44498922 #>>44499113 #>>44499996 #
Cyykratahk ◴[] No.44499113[source]
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
replies(1): >>44499518 #
bvrmn ◴[] No.44499518[source]
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 => ...)`.
replies(2): >>44499619 #>>44512570 #
jw1224 ◴[] No.44499619[source]
PHP doesn’t force keys... You can omit the key and simply write `foreach($items as $value)`
replies(2): >>44503392 #>>44503403 #
bvrmn ◴[] No.44503403[source]
BTW: https://www.php.net/manual/en/iterator.key.php

It's literally in the interface.

replies(1): >>44508770 #
jw1224 ◴[] No.44508770[source]
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 not being used in the parent comment’s example either.

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.

replies(1): >>44539760 #
1. bvrmn ◴[] No.44539760[source]
> 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.

replies(1): >>44609644 #
2. jw1224 ◴[] No.44609644[source]
I don’t think you understand the purpose of PHP’s Iterator interface.
replies(1): >>44619867 #
3. bvrmn ◴[] No.44619867[source]
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.
replies(1): >>44641200 #
4. jw1224 ◴[] No.44641200{3}[source]
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.