On 11 March 2025 09:00:52 GMT, "Côme Chilliet" <c...@chilliet.eu> wrote:
>This would be way better on performances as a single foreach, no?
>I feel like this pipe operator encourages coders to use array_* functions with 
>closures, which is often terrible performances compared to a loop.

I think this highlights something that has been mentioned a few times over the 
years: PHP badly needs more native functions for working with iterators. If 
each stage of the pipeline is lazily consuming an iterator and yielding each 
value in turn, one major source of performance impact goes away, because we 
don't have to repeatedly allocate intermediate arrays. It also makes it much 
easier to work with infinite inputs, which obviously can't be flattened to an 
array.

It also highlights why just letting all array functions accept iterable would 
*not* be the right approach: array_map(iterable):array would still have to 
eagerly iterate its input, so we need a separate 
iter_map(iterable):NonRewindableIterator (or whatever name). Even iter_sum() 
might shortcut if an invalid value was defined as an Error rather than Warning.

This feels like one of those cases where different proposals complement rather 
than blocking each other: iterator functions make pipes more efficient to use, 
and pipes make iterator functions more pleasant to use. I'd like both please. :)

Rowan Tommins
[IMSoP]

Reply via email to