Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Shortcutting grep in boolean context

by perlancar (Hermit)
on Jan 03, 2017 at 21:31 UTC ( [id://1178877]=note: print w/replies, xml ) Need Help??


in reply to Re: Shortcutting grep in boolean context
in thread Shortcutting grep in boolean context

Ah yes, this is a nice idiom, and to use it in an expression, one can use e.g. do:

if (do { my $found; $_ == $wanted and $found = 1 and last for @ary; $found }) { say "match" }'

Replies are listed 'Best First'.
Re^3: Shortcutting grep in boolean context
by tybalt89 (Monsignor) on Jan 03, 2017 at 22:28 UTC

    To avoid a temporary variable:

    if( sub{ $_ == $wanted and return 1 for @ary; 0 }->() ) { say "match" +}
      The sub version has a higher overhead than the do version, but the speed won't matter unless we do hundreds of thousands of calls per second or more.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1178877]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found