Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Smartmatch alternatives

by davido (Cardinal)
on Dec 17, 2013 at 16:05 UTC ( [id://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

Replies are listed 'Best First'.
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)
      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)

      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
Re^4: Smartmatch alternatives
by LanX (Saint) 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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 19:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found