>>>>> "R" == Ruud <rvtol+use...@isolution.nl> writes:
R> On 2011-01-12 18:08, Uri Guttman wrote: >> Ruud writes: >>> On 2011-01-11 11:44, Uri Guttman wrote: >>>> these are equivilent: >>>> >>>> my @out = grep /foo/, @in ; >>>> my @out = map { /foo/ ? $_ : () } @in ; >>> >>> Indeed equivalent. There is a difference though >>> in what comes out of map and grep: >>> >>> perl -wle' >>> my @in = 1 .. 4; >>> print "@in"; >>> >>> ++$_ for map { $_ % 2 ? $_ : () } @in; # copies >>> print "@in"; >>> >>> ++$_ for grep { $_ % 2 } @in; # aliases >>> print "@in"; >>> ' >>> >>> 1 2 3 4 >>> 1 2 3 4 >>> 2 2 4 4 >> >> interesting and evil. my rule is grep/map should never do side effects >> and do read only work. if you want modifications use a proper loop. R> With map and grep I prefer a functional coding style too. Still it R> helps to know implementation details like aliasing. and it actually makes some odd sense when you know that for modifier does aliasing too (like for loops). it was added to be the proper way to do side effects on a list (which map isn't meant to do). too many used map for loops without using the return list. why grep uses aliases is not clear but it could be historical and for efficiency reasons. map wasn't changed to aliases since we have for modifier. anyhow, keeping their usage functional is proper. uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/