On 4/30/2016 5:23 PM, Marco Pivetta wrote:
> This feature has nothing to do with fluent interfaces, nor has their flaws.
> The readability of piped processing for functional code is simply a
> no-brainer here, and doesn't require any API changes in function signatures
> either: it is not "a different way of doing the same thing".
> On Apr 30, 2016 17:05, "Fleshgrinder" <p...@fleshgrinder.com> wrote:
> 

This is the example code from the RFC written in a normal procedural way.

$files = scandir($arg);
$files = array_filter($files, function ($x) {
  return $x !== '.' && $x !== '..';
});
$files = array_map(function ($x) use ($arg) {
  return $path . '/' . $x;
}, $files);
$ret = array_merge($ret, getFileArg($files));

This is the example code with the pipe operator.

$ret = scandir($arg)
  |> array_filter($$, function($x) { return $x !== '.' && $x != '..'; })
  |> array_map(function ($x) use ($arg) { return $arg . '/' . $x; }, $$)
  |> getFileArg($$)
  |> array_merge($ret, $$);

Definitely looks like "a different way of doing the same thing" to me.
So does the initial super ugly example code of nesting.

Again, I am not really against the proposal nor do I argue against the
fact that it improves readability of certain code constructs. I am just
meh on it because such code can pretty much always be rewritten in a way
that makes it more readable, easier to debug, and even faster. In the
example code we have:

array_filter with O(n)

array_map with O(n)

array_merge with O(∑ array_i, i != 1) and in our case O(n) where n
equals the total count of elements in $files/$$.

In my universe `getFileArg` (note the absence of a plural form) would
operate on a single path and not on an array of paths:

foreach (new DirectoryIterator($arg) as $path) {
  if ($path->isDot() === false) {
    $ret[] = getFileArg($arg . DIRECTORY_SEPARATOR . $path);
  }
}

I cannot tell if this construct is really equivalent to the example from
the RFC (because I do not know what $ret contains) but it should do
exactly the same. It is more readable, easier to debug, and much faster.

Now you may argue that the example is simply flawed. Well, maybe, but
functional programming is nice if you have a language that is
functional. The only functional part in PHP are callbacks, everything
else is just plain old procedural + OO and importing more functional
concepts will not change that. However, I am not saying that every
functional concept makes no sense just that the pipes are not very
appealing to me because I only see "a different way of doing the same
thing" with slower code. :P

-- 
Richard "Fleshgrinder" Fussenegger

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to