Re: [RFC] Pipe Operator
| From: | Stephen Coakley | Date: | Tue, 03 May 2016 00:57:40 +0000 |
| Subject: | Re: [RFC] Pipe Operator | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-93034@lists.php.net to get a copy of this message | ||
On 04/30/2016 06:14 PM, Rowan Collins wrote:
On 29/04/2016 20:58, Sara Golemon wrote: Let's say I want to add a condition just before getFileArg(); with the current version I've got to: - go to the beginning of the chain, and assign to something other than $ret - go to where I want to break the chain, and reintroduce the $ret assignment - add my check in the gap, using the variable I just added at the beginning of the chain $fileList = scandir($arg)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; },$$); if ( someCheck($fileList) {something();} $ret = getFileArg($$)|> array_merge($ret, $$);The syntax is fighting me here, making me jump around the code. But if assignment was always on the end of the chain, I would only need to make changes at the point where I was breaking the chain. 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.
|> 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?
--
Stephen