Every single one of those steps buffers into a temporary variable - this isn't efficient like a bash pipe.
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?
PHP 8.5 adds pipe operator
61–70 of 282 posts
Re: PHP 8.5 adds pipe operator
#62Earlier quoted context omitted.
I actually don’t mind them, and I’ve been out of daily PHP work for a few years now. When I see people denote internal variables with _ or elements with $ in JS, it rubs me the wrong way, but in PHP the $ is kind of nice. I also prefer the look of ->, it’s _cool_
Other languages have all sorts of oversized arrows, like ==> and >>>. -> in PHP and C++ looks clean by comparison. I'll never forgive them for the brain fart they made of the namespace separator, though.
Re: PHP 8.5 adds pipe operator
#63Why doesn't PHP remove the horrid $ symbol for variables and the -> symbol for calling methods? I think those alone would do a lot more for its perception and adoption than adding the pipe operator.
Just to be clear: consistency does very much matter. The mental load of reading totally different styles of code is awful and a waste of energy.
Re: PHP 8.5 adds pipe operator
#64I like it. I really believe the thing PHP needs the most is a rework of string / array functions to make them more consistent and chain able. Now they are at least chainable. I'm not a fan of the ... syntax though, especially when mixed in the same chain with the spread operator
Agree, the ... syntax feels confusing when each fn($x) in the example uses $x as the name of its argument. My initial instinct would be to write like this: `$result = $arr |> fn($arr) => array_column($arr, 'tags') // Gets an array of arrays |> fn($cols) => array_merge(...$cols)` Which makes me wonder how this handles scope. I'd imagine the interior of some chained function can't reference the input $arr, right? Does…
function ($parameter) use ($data) { ... }
to capture stuff from the local environment.Edit: And you can pass by reference:
> $stuff = [1]
= [
1,
]
> $fn = function ($par) use (&$stuff) { $stuff[] = $par; }
= Closure($par) {#3980 …2}
> $fn(2)
= null
> $stuff
= [
1,
2,
]
Never done it in practice, though, not sure if there are any footguns besides the obvious hazards in remote mutation.Re: PHP 8.5 adds pipe operator
#65Earlier quoted context omitted.
This is "english-sentence-order-consistent", as it goes. Array filter is "filter this array with this function". Array map is "map this function over this array". But I agree any replacement function should be consistent with Haskell.
One can construct English sentences in the opposite order. There is no singular "English sentence order". "Filter for this function in this array" "Map over this array with this function"
One filters something with something else, in the real world. Filter water with a mesh etc.
And (in maths, at least) one maps something onto something else. (And less commonly one maps an area onto paper etc.)
Just because you can make your two sentences does not make them natural word order.
Re: PHP 8.5 adds pipe operator
#66Every single one of those steps buffers into a temporary variable - this isn't efficient like a bash pipe.
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?
Re: PHP 8.5 adds pipe operator
#67The syntax is ugly as hell.
Re: PHP 8.5 adds pipe operator
#68Earlier quoted context omitted.
Your version includes 4 variables. Pipes don't create those intermediate variables, so they are more memory efficient. Readability is mostly matter of habit. One reads easily what he/she is used to read.
It's true that pipes are more readable, and for many cases they will be the better option, but the example of nested functions just doesn't hold. That's like saying someone would use this: $result = $arr |> fn($x) => array_column($x, 'tags') |> fn($x) => array_merge(...$x) |> array_unique(...) |> array_values(...) which is harder to reason about than the nested functions. array_values( array_unique( array_merge( ...a…
Re: PHP 8.5 adds pipe operator
#69Earlier quoted context omitted.
They're not though. array_filter takes (arr, callback) https://www.php.net/manual/en/function.array-filter.php array_map takes (callback, arr) https://www.php.net/manual/en/function.array-map.php
This is "english-sentence-order-consistent", as it goes. Array filter is "filter this array with this function". Array map is "map this function over this array". But I agree any replacement function should be consistent with Haskell.
Re: PHP 8.5 adds pipe operator
#70Why doesn't PHP remove the horrid $ symbol for variables and the -> symbol for calling methods? I think those alone would do a lot more for its perception and adoption than adding the pipe operator.
Because it simply can't do that in a retro-compatible way. -> isn't so bad, C/C++ uses that as well. as for $ I guess it came from Perl. The point is already used for string concatenation, where other languages would overload the + operator.