SPUG: first vs //

Dave O cxreg at pobox.com
Wed Jul 18 13:27:40 PDT 2007


And if one or more of your list elements are scalars

     $var = first { $_->() } \( &foo, sub { $bar }, &baz );

:)

On Wed, 18 Jul 2007, Colin Meyer wrote:

> On Wed, Jul 18, 2007 at 11:37:42AM -0700, Michael R. Wolf wrote:
> > > Interestingly, Larry's 'first' suggestion is now available in the core:
> > >
> > >    use List::Util qw/first/;
> > >
> > >    $var  = first { defined } $foo, $bar, $baz;
> >
> > In the world of TMTOWTDI, I like.... *both*.
> >
> > I initially liked the use of 'first' as more intuitive (for my definition of
> > intuitive).  In reading the "old arguments", I realized that the 'first'
> > subroutine does not (in fact, cannot) do short circuiting, in which case the
> > // operator is better.
> >
> > $var = $foo // $bar // $baz;
> >
> > Short circuiting (lazy evaluation) makes much more sense when functions are
> > called since they could have a non-trivial cost and side-effects that may
> > need to be avoided.  In some cases the later functions must be guarded
> > against execution if earlier ones succeed.
> >
> > $var = foo() // bar() // baz();
>
> Short circuiting with first:
>
>     $var = first { $_->() } \( &foo, &bar, &baz );
>
> -Colin.
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
>      POST TO: spug-list at pm.org
> SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>     MEETINGS: 3rd Tuesdays
>     WEB PAGE: http://seattleperl.org/
>
>
>


More information about the spug-list mailing list