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

Re^4: Smartmatch alternatives

by Anonymous Monk
on Dec 17, 2013 at 16:14 UTC ( #1067490=note: print w/replies, xml ) Need Help??

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

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)

Replies are listed 'Best First'.
Re^5: Smartmatch alternatives
by Laurent_R (Canon) on Dec 17, 2013 at 18:52 UTC
    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)

        Oops, you are absolutely right. I must have changed something at the last moment, and don't remember exactly what. It was working OK, but not anymore. I am too tired now to figure out what happened, it will have to be tomorrow. I tested it with various arrays, it was working perfectly alright. But it's no longer the case.

        OK, to work correctly, the first element of the array passed to any needs to be false. There are two ways of doing it, I tested both ways, but then I made the mistake of removing both ways from my post. It can be done either as in the update I made in my post above (prepending a 0 to the list passed to any) or by changing the any function as follows:

        sub any (&@) { my $code_ref = shift; unshift @_, 0; reduce { $a or $code_ref->(local $_ = $b) } @_; }
        Thank you Rolf for your remark.

        Update: It could also be done this way, perhaps the best one:

        sub any (&@) { my $code_ref = shift; reduce { $a or $code_ref->(local $_ = $b) } 0, @_; }

Re^5: Smartmatch alternatives
by davido (Archbishop) on Dec 17, 2013 at 16:19 UTC

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

    Funny, I thought it went back farther than that.


      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

        Well, since I'm running Perl 5.18.2, I guess the fact that I have List::Util::any must mean that somewhere along the way I've installed the latest dual-lived version of the module.

        Anyway, is there any disagreement that "any" (under whichever banner it marches) is a reasonable utility for determining if any of the elements in an array meets some criteria? If I recall, it's also a PBP recommendation for the same reason I gave: That, unlike grep, it gives an appropriate and clear name to the functionality at work.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067490]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-02-18 04:12 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (250 votes). Check out past polls.