Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93034 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48148 invoked from network); 3 May 2016 00:57:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 May 2016 00:57:44 -0000 X-Host-Fingerprint: 68.118.157.39 68-118-157-39.dhcp.mdsn.wi.charter.com Received: from [68.118.157.39] ([68.118.157.39:1608] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AA/0D-03860-787F7275 for ; Mon, 02 May 2016 20:57:44 -0400 Message-ID: To: internals@lists.php.net References: <4fc01507-3d07-2309-a4e4-4cad7325249b@gmail.com> Date: Mon, 2 May 2016 19:57:40 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 MIME-Version: 1.0 In-Reply-To: <4fc01507-3d07-2309-a4e4-4cad7325249b@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 68.118.157.39 Subject: Re: [PHP-DEV] [RFC] Pipe Operator From: me@stephencoakley.com (Stephen Coakley) 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) > |> 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. 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? -- Stephen