Stephen Coakley wrote on 03/05/2016 01:57:
On 04/30/2016 06:14 PM, Rowan Collins wrote:
The basic pattern would be:
|=> $tempVar; // terminate the chain and capture the value
// do stuff with $tempVar
$tempVar // restart the chain
So:
scandir($arg)
|> array_filter($$, function($x) { return $x !== '.' && $x !=
'..'; })
|> array_map(function ($x) use ($arg) { return $arg . '/' . $x;
}, $$)
|=> $fileList;
if ( someCheck($fileList) {
something();
}
$fileList
|> getFileArg($$)
|> array_merge($ret, $$)
|=> $ret;
If I don't need the condition any more, I can delete lines 4 to 8, and
I've got back my original chain.
Could you use a closure instead to accomplish this? (Again yes, Sara
could you clarify if this is permitted?)
$ret = scandir($arg)
|> array_filter($$, function($x) { return $x !== '.' && $x !=
'..'; })
|> array_map(function ($x) use ($arg) { return $arg . '/' .
$x; }, $$)
|> (function($fileList) {
if (someCheck($fileList)) {
something();
}
return $fileList;
})($$)
|> getFileArg($$)
|> array_merge($ret, $$);
Not completely the best, but perhaps there's some sort of an idea here?
The disadvantage here is that you're still having to mutate your code to
fit the piped style, even if it's just wrapping into the closure. I was
aiming for a style that would let me freely mix piped and unpiped code.
Meanwhile, I still see having the assignment at the end of the chain as
an end in itself - the ability to use the pipe operator as an expression
seems like a nightmare waiting to happen to me. As currently proposed,
you could write this, which is just horrible:
if (
scandir($arg)
|> array_filter($$, function($x) { return $x !== '.' && $x !=
'..'; })
|> count($$)
>
scandir($arg)
|> count($$)
) { ... }
Obviously, it's always possible to show horrible abuses of a feature,
but I can't think of any sensible place to use a pipe as an expression
except in order to either assign it to a variable, or return it from a
function.
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php