Remix.run Logo
mort96 8 days ago

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
ptx 7 days ago | parent | next [-]

Apparently "foo(...)" is just the PHP syntax for a function reference, according to the "first-class callable" RFC [1] linked from the article.

So where in Python you would say e.g.

  callbacks = [f, g]
PHP requires the syntax

  $callbacks = [f(...), g(...)];
As for the purpose of the feature as a whole, although it seems like it could be replaced with function composition as mentioned at the end of the article, and the function composition could be implemented with a utility function instead of dedicated syntax, the advantage of adding these operators is apparently [2] performance (fewer function calls) and facilitating static type-checking.

[1] https://wiki.php.net/rfc/first_class_callable_syntax

[2] https://wiki.php.net/rfc/function-composition#why_in_the_eng...

mort96 7 days ago | parent [-]

Thanks, that makes sense!

moebrowne 7 days ago | parent | prev | next [-]

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

7 days ago | parent | prev | next [-]
[deleted]
tossandthrow 8 days ago | parent | prev [-]

It is to interject the chained value at the right position in the function.

They write that elixir has a slightly fancier version, it is likely around this, they mean (where elixir has first class support for arity > 1 functions)

mort96 7 days ago | parent [-]

But the example suggests that it can't interject the chained value at the right position; if that was the case, the example would've been written as `|> array_column('tags', ...)`.

wink 7 days ago | parent [-]

yeah that sounds weird. defaulting to the first (or only) parameter would have made sense.