Live data from Hacker News

PHP 8.5 adds pipe operator

thephp.foundation

161–170 of 282 posts

Re: PHP 8.5 adds pipe operator

#161
post #115

Earlier quoted context omitted.

I'm not tempting you do to it or anything, but I want to say given your point of view, if one day you need a crude+ app and try to do it using laravel, you might be really surprised by what modern php actually is. There was a point were I thought the language and it ecosystem was going down the drain but then they recovered and modern php is 90% what do you want to do and don't worry about the how, it's easy. I don't…

what about deployment? I assume I need to scp files like Python or keep everything in a single giant PHP file? is that an option?

Laravel Forge handles auto-deployment on push to master. Or if you want production zero downtime deployments, use Laravel Envoyer.

Re: PHP 8.5 adds pipe operator

#162
post #91

I'm surprised that the example requires lambdas... What's the purpose of the `|> foo(...)' syntax if the function has to take exactly one operand? Why is it necessary to write this? $arr |> fn($x) => array_column($x, 'tags') Why doesn't this work? $arr |> array_column(..., 'tags') And when that doesn't work, why doesn't this work? $arr |> array_unique

There is a complementary RFC for partial function application which will allow calling a function with more than one parameter.

https://wiki.php.net/rfc/partial_function_application_v2 https://wiki.php.net/rfc/pipe-operator-v3#rejected_features

Re: PHP 8.5 adds pipe operator

#163
post #91

I'm surprised that the example requires lambdas... What's the purpose of the `|> foo(...)' syntax if the function has to take exactly one operand? Why is it necessary to write this? $arr |> fn($x) => array_column($x, 'tags') Why doesn't this work? $arr |> array_column(..., 'tags') And when that doesn't work, why doesn't this work? $arr |> array_unique

[deleted]

Re: PHP 8.5 adds pipe operator

#164
post #25
post #4

While I appreciate the effort and like the approach in general, in this use case I really would prefer extensions / extension functions (like in Kotlin[1]) or an IEnumerable / iterator approach (like in C#). $arr = [ new Widget(tags: ['a', 'b', 'c']), new Widget(tags: ['c', 'd', 'e']), new Widget(tags: ['x', 'y', 'a']), ]; $result = $arr |> fn($x) => array_column($x, 'tags') // Gets an array of arrays |> fn($x) => ar…

PHP has traits, just invent that API, put it in a trait and add it to your data classes.

Well, while traits might be a workaround for certain use cases, simple arrays with scalar data types could not be extended via traits.

While I know that there are Collection classes in Symfony, Laravel, etc., I'm not a huge fan of wrapping a PHP array with a class to get method chaining, even with generators.

  $sum = [1, 2, 3]->filter(fn($x) => $x%2!= 0)->concat([5,7])->sum();
cannot be solved with traits. Additionally, I think traits should be used very carefully and they do not have this many use cases that aren't a code smell to me.

Re: PHP 8.5 adds pipe operator

#165
post #11
post #4

While I appreciate the effort and like the approach in general, in this use case I really would prefer extensions / extension functions (like in Kotlin[1]) or an IEnumerable / iterator approach (like in C#). $arr = [ new Widget(tags: ['a', 'b', 'c']), new Widget(tags: ['c', 'd', 'e']), new Widget(tags: ['x', 'y', 'a']), ]; $result = $arr |> fn($x) => array_column($x, 'tags') // Gets an array of arrays |> fn($x) => ar…

The advantage is that pipes don't care about the type of the return value. Let's say you add a reduce in the middle of that chain. With extension methods that would be the last one you call in the chain. With pipes you'd just pipe the result into the next function

Yeah, I agree. That's an advantage of pipes - although much harder to read and write than chained methods in my opinion.

The use-case in the article could still be solved easier with extension methods in my opinion :-)

Re: PHP 8.5 adds pipe operator

#166
post #32
post #23

I tried to emulate something similar with PHP at one point. But the problem with PHP was parameter order. Especially in functions like array_key_exists() the array element is the 2nd parameter, while pipe operator expects the object to work on be the 1st parameter, the array in these cases. I believe they have solved this problem by now. Though no idea how.

The usual solution is to wrap it with a closure. function($x) { return array_key_exists('needle', $x); } Or using the arrow function syntax: fn($x) => array_key_exists('needle', $x) The same trick also helps when you need to use functions with mandatory extra parameters, functions with pass-by-value parameters, etc.

If the Partial Function Application RFC passes then the closure wont be necessary

https://wiki.php.net/rfc/partial_function_application_v2

Re: PHP 8.5 adds pipe operator

#167

composition would be much nicer than this, maybe soon

Might be sooner than you think. There are already RFCs for Partial Function Application and Function Composition:

https://wiki.php.net/rfc/partial_function_application_v2 https://wiki.php.net/rfc/function-composition

Re: PHP 8.5 adds pipe operator

#168

PHP is that weird beast that no one wants to praise and yet it works tremendously well for those who manage to tame it. I would likely never touch it as there are too many languages to use and what I know is more than enough to do my job, but I am super excited to see languages like PHP that aren't mainstream in my bubble to keep evolving

I'll gladly praise it. It's a very practical language with good tooling and excellent amounts of libraries and scripts available. Performance is decent, development speed for tools, toys and prototypes is extreme.

The standard library has a lot of good stuff for calling API:s, handling JSON, shelling out, string juggling and HTML publishing on a socket. In every typical install you also have common database interfaces. I've done so much problem solving at breathtaking speed in single file PHP scripts and PsySH over the years.

The threading story isn't or wasn't very good so typically I've done logic in PHP and then driven it from something like a Scheme, Picolisp or Elixir when I've needed it.

Re: PHP 8.5 adds pipe operator

#169
This article makes a great case WHY the pipe operator is useful, but why didn't they just rewrite those functions to support method chaining? ` $profit = [1, 4, 5] .loadSeveral() .filter(isOnSale()) .map(sellWidget()) .array_sum(); ` this has the side benefit of 'looking normal'

Re: PHP 8.5 adds pipe operator

#170

The first typed programming language where I've seen pipe operator |> in action was in F#. You can write something like: sum 1 2 |> multiply 3 and it works because |> pushes the output of the left expression as the last parameter into the right-hand function. multiply has to be defined as: let multiply b c = b \* c so that b becomes 3, and c receives the result of sum 1 2 . RHS can also be a lambda too: sum 1 2 |> (f…

Haskell seems pretty dead as well. Good think php has another option for line noise though.
Post reply on HN