Live data from Hacker News

PHP 8.5 adds pipe operator

thephp.foundation

61–70 of 282 posts

Re: PHP 8.5 adds pipe operator

#61
post #57

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?

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

Re: PHP 8.5 adds pipe operator

#62
post #35

Earlier 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.

What would the alternative for a namespace separator be? The backslashes work well with PSR-4 to give a logical visual of the expected directory structure.

Re: PHP 8.5 adds pipe operator

#63

Why 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.

I honestly don't understand this. The syntax is one of the most boring parts of a programming language. It is solved by the IDE (and now LLMs). I don't care about syntax, I care about what I can build. Since the beginning of time people argue about things like tabs vs. spaces, or the dollar sign and I honestly don't understand why that is. It just doesn't matter.

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

#64

I 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…

You can do

     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

#65
post #58

Earlier 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"

Right, but these are both more unwieldy.

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

#66
post #57

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 does have generators and iterators yes, although I personally rarely use them directly.

Re: PHP 8.5 adds pipe operator

#68
post #53
post #48

Earlier 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…

It is more readable and better option — you have to parse it from the innermost function to the outermost just to understand what it's doing. With the pipe, it's more straightforward: you read it step by step — do this, then that, then the next — just like how you'd naturally read instructions.

Re: PHP 8.5 adds pipe operator

#69

Earlier 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.

[deleted]

Re: PHP 8.5 adds pipe operator

#70

Why 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.

> Why 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.

Post reply on HN