Remix.run Logo
mappu 8 days ago

Every single one of those steps buffers into a temporary variable - this isn't efficient like a bash pipe.

quietbritishjim 8 days ago | parent | next [-]

Genuine question from a non-PHP user:

Does PHP support iterator-like objects? Like Python I mean, where mydict.values() produces values on demand, not immediately realised as a list. Or are all steps necessarily guaranteed to be fully realised into a complete list?

severak_cz 8 days ago | parent | next [-]

yes, for a long time - https://www.php.net/manual/en/class.iterator.php

quietbritishjim 7 days ago | parent [-]

Interesting, but I suppose I was particularly interested if that's what's actually happening with the transformations in the example in the article. Are those making use of this protocol? The comment I originally replied to seems to imply they aren't.

Timwi 8 days ago | parent | prev | next [-]

The section where the article mentions function composition implies that it doesn't. The article says that compositing the functions before passing them into map would be an optimization. I take that to mean that without the composition, each map fully processes an array passed to it from the previous map, and the first map fully reads the whole file in the example. If it were iterable, the function composition would make no difference compared to a pipeline of multiple maps.

Meanwhile, I'm confused as to why it sometimes says “map” and sometimes “array_map”. The latter is what I'm familiar with and I know that it operates on a whole array with no lazy evaluation. If “map” isn't just a shorthand and actually creates a lazy-evaluated iterable, then I'm confused as to why the function composition would make any difference.

throw_m239339 8 days ago | parent | prev [-]

PHP does have generators and iterators yes, although I personally rarely use them directly.

troupo 7 days ago | parent | prev [-]

That's how function calls work in every language. Unless it's streams.