Live data from Hacker News

PHP 8.5 adds pipe operator

thephp.foundation

41–50 of 282 posts

Re: PHP 8.5 adds pipe operator

#41
post #29

Earlier quoted context omitted.

PHP string / array functions are consistent. string functions use (haystack, needle) and array functions use (needle, haystack) because that's the way the underlying C libraries also worked

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

#45

I love the pipe operator - one of the things I dig about Elixir though many languages have it. It's so much easier to reason about: $result = $arr |> fn($x) => array_column($x, 'tags') |> fn($x) => array_merge(...$x) |> array_unique(...) |> array_values(...) VS array_values(array_unique(array_merge(...array_column($arr, 'tags'))));

I don't see how this is hard to reason about, assuming this is the resulting code when using variables:

  $tags        = ...array_column($arr, 'tags');
  $merged_tags = array_merge($tags);
  $unique_tags = array_unique($merged_tags);
  $tag_values  = array_values($unique_tags);
It also makes it easier to inspect the values after each step.

Re: PHP 8.5 adds pipe operator

#46

I love the pipe operator - one of the things I dig about Elixir though many languages have it. It's so much easier to reason about: $result = $arr |> fn($x) => array_column($x, 'tags') |> fn($x) => array_merge(...$x) |> array_unique(...) |> array_values(...) VS array_values(array_unique(array_merge(...array_column($arr, 'tags'))));

> It's so much easier to reason about

Is it though? I don't think so.

Re: PHP 8.5 adds pipe operator

#48
post #45

I love the pipe operator - one of the things I dig about Elixir though many languages have it. It's so much easier to reason about: $result = $arr |> fn($x) => array_column($x, 'tags') |> fn($x) => array_merge(...$x) |> array_unique(...) |> array_values(...) VS array_values(array_unique(array_merge(...array_column($arr, 'tags'))));

I don't see how this is hard to reason about, assuming this is the resulting code when using variables: $tags = ...array_column($arr, 'tags'); $merged_tags = array_merge($tags); $unique_tags = array_unique($merged_tags); $tag_values = array_values($unique_tags); It also makes it easier to inspect the values after each step.

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.

Re: PHP 8.5 adds pipe operator

#49
post #6

Earlier quoted context omitted.

It’s really not needed, syntax sugar. With dots you do almost the same. Php doesn’t have chaining. Adding more and more complexity doesn’t make a language better.

Nothing is really needed, C89 was good enough. Dots are not the same, nobody wants to use chaining like underscore/lodash allowed because it makes dead code elimination impossible.

K&R C was good enough for UNIX System V, why bother with C89.

Re: PHP 8.5 adds pipe operator

#50
post #48
post #45

Earlier quoted context omitted.

I don't see how this is hard to reason about, assuming this is the resulting code when using variables: $tags = ...array_column($arr, 'tags'); $merged_tags = array_merge($tags); $unique_tags = array_unique($merged_tags); $tag_values = array_values($unique_tags); It also makes it easier to inspect the values after each step.

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.

> so they are more memory efficient

They can be. It depends on the language, interpreter, compiler, and whether you do anything with those intermediate variables and the optimiser can get rid of them.

Post reply on HN