On Tue, Apr 09, 2013 at 10:24:45PM -0400, Bob Rogers wrote:
>    map should produce better code and does reads better but it also
>    iterates the entire list and you can't break out of it . . .
> 
> Not quite true, as you can "goto" a label outside of the block:
> 
>     sub find_odd {
>       # Return the first odd argument.
>       my (@values) = @_;
> 
>       my $result;
>       map { $result = $_ and goto LAST
>                 if $_ & 1;
>       } @values;
>       LAST:
>       return $result;
>     }

Indeed, you are correct. However, when you break out of a map in this way,
the map returns an empty list, regardless of how many results had been 
previously
collected. 

Because your example handles a single result, it is not clear that, in fact,
any return values MUST be collected similarly. Unless, of course, map is used
simply for side effects.
 
> It's hard to think of a case where this would be better than a simple
> loop, which is both smaller and more readable.  However, this technique
> also gives you an early exit when mapping over a tree recursively.

Even then, the need to handle collected values manually would seem to obviate
any advantage in doing so.

This discussion has confirmed, for me, that writing code is, in many ways,
similar to writing prose. Though there may initially, appear to be many ways
to communicate, thoughtfulness, good taste and circumstance quickly show that
there are only a few good ways to do so.

-Gyepi 

_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to