Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Smartmatch alternatives

by davido (Archbishop)
on Dec 17, 2013 at 16:05 UTC ( #1067488=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Smartmatch alternatives
in thread Smartmatch alternatives

No, I meant List::Util::any.

$ perl -MList::Util -E 'say "Yes" if List::Util::any { $_ == 42 } @{[q +w/1 3 42 5/]}' Yes

It shows up in both modules. There may be subtle, undocumented differences.


Dave


Comment on Re^3: Smartmatch alternatives
Download Code
Re^4: Smartmatch alternatives
by LanX (Canon) on Dec 17, 2013 at 16:13 UTC
    OK your CPAN- version doesn't show it in the synopsis

    SYNOPSIS use List::Util qw(first max maxstr min minstr reduce shuffle sum);

    and my 5.10 version does only list it as suggested.

    Anyway good news! (for me at least =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      :) Let's just say that whether one uses List::Util::any, or List::MoreUtils::any, it's a good idea to use "any" when "any" semantics are what is wanted.


      Dave

Re^4: Smartmatch alternatives
by Anonymous Monk on Dec 17, 2013 at 16:14 UTC

    I see that it was added after 1.32 (most recent version installed here) ...

    1.33 -- Sun Oct 13 01:35 UTC 2013 * Added any, all, none, notall list reduction functions (inspired by List::MoreUtils)

      Another good reason to run a recent Perl, I guess. ;) lol.

      Funny, I thought it went back farther than that.


      Dave

        The most recent stable release of Perl only comes with List::Util 1.27.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      If you don't have any, it is easy to implement it using the reduce function of List::Utils with something like this:
      sub any { my $code_ref = shift; reduce { $a or $code_ref->(local $_ = $b) } @_; }
      Or, better:
      sub any(&@) { my $code_ref = shift; reduce { $a or $code_ref->(local $_ = $b) } @_; }
      which makes it possible to call it with a syntax similar to grep:
      <strike>print "true\n" if any { $_> 11 } qw /3 12 4 5 7/; # prints tru +e
      Update: the above line need to be this:
      print "true\n" if any { $_> 11 } 0, qw /3 12 4 5 7/; # prints true
        your any is always true!

        DB<12> use List::Util qw/reduce/ DB<13> sub any(&@) { my $code_ref = shift; reduce { $a or $code_ref->(local $_ = $b) } @_; } DB<15> print "true\n" if any { $_ < 0 } qw /3 12 4 5 7/; true

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-09-19 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls