On 3 May 2016 at 10:57, Stephen Coakley <m...@stephencoakley.com> wrote:
> 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > Doesn't Nikic's scalar objects (https://github.com/nikic/scalar_objects) more or less achieve the same thing while also cleaning up the std lib? $ret = scandir($arg) ->filter(function(){}) ->map(function(){}) ->merge($someOtherArray); Terry Cullen