Most active commenters
  • bvrmn(6)
  • jw1224(4)

←back to thread

116 points doekenorg | 19 comments | | HN request time: 0.875s | source | bottom
1. 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 #
2. zelphirkalt ◴[] No.44498922[source]
This means, that most likely the keyword will not be "yield". PHP is not known for keeping a low number of keywords. Rather they introduce new ones, to tack on things to the language, like one can see in the implementation of anonymous functions, where one needs to use "use" explicitly, instead of the language having the semantics that most other languages have for lambdas. Which immediately turns in to a source for nullability errors.
replies(1): >>44499114 #
3. 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 #
4. djxfade ◴[] No.44499114[source]
That's not totally correct. PHP has a "short function" syntax for that specific use case, it automatically captures data without the 'use' statement.

    $a = 5;
    $b = fn ($c) => $a * $b;
    print($b(2)); // 10
    print($b(4)); // 20
replies(2): >>44499861 #>>44502869 #
5. 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 #
6. jw1224 ◴[] No.44499619{3}[source]
PHP doesn’t force keys... You can omit the key and simply write `foreach($items as $value)`
replies(2): >>44503392 #>>44503403 #
7. ◴[] No.44499861{3}[source]
8. doekenorg ◴[] No.44499996[source]
Yes, there is! However, even if the `iterable` it was yielding from a different Generator, the "outer" generator would still be "running", which still makes it asymmetrical. `yield from` is mentioned in the other post on Generators, though.
9. dotancohen ◴[] No.44502869{3}[source]
This "arrow function" syntax was introduced much later, though.
10. bvrmn ◴[] No.44503392{4}[source]
What's idiomatic way to get index (0, n-1) with the value? Parent example shows you could not use $key as a generic solution.
replies(1): >>44509486 #
11. bvrmn ◴[] No.44503403{4}[source]
BTW: https://www.php.net/manual/en/iterator.key.php

It's literally in the interface.

replies(1): >>44508770 #
12. jw1224 ◴[] No.44508770{5}[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 #
13. doekenorg ◴[] No.44509486{5}[source]
You can use the SPL LimitIterator, feed it the generator, and give it the offset and limit.

``` foreach (new LimitIterator($generator, 0, 3) as $value) { echo $value; } ```

https://www.php.net/manual/en/class.limititerator.php

14. Fidelix ◴[] No.44512570{3}[source]
"It's a hot mess" - someone who knows absolutely nothing about what they're talking about.

Typical HN comment about php.

replies(1): >>44539767 #
15. bvrmn ◴[] No.44539760{6}[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 #
16. bvrmn ◴[] No.44539767{4}[source]
Defenders of PHP's iterators clearly tasted only carrots.
17. jw1224 ◴[] No.44609644{7}[source]
I don’t think you understand the purpose of PHP’s Iterator interface.
replies(1): >>44619867 #
18. bvrmn ◴[] No.44619867{8}[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 #
19. jw1224 ◴[] No.44641200{9}[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.