Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Smartmatch alternatives

by LanX (Saint)
on Dec 17, 2013 at 15:56 UTC ( [id://1067487]=note: print w/replies, xml ) Need Help??


in reply to Re: Smartmatch alternatives
in thread Smartmatch alternatives

Agreed, but any is in List::MoreUtils (which isn't core. =)

And XS and it's alternative pure Perl implementation was buggy last time I looked into it.

update

oh did you mean first ? :)

use List::Util qw(first max maxstr min minstr reduce shuffl +e sum); first BLOCK LIST ... "first" returns the first element where the result from BLOCK is a true value.

update

grr @%$§ !!!...

DB<121> @a=0..10 => (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) DB<122> first { "0" eq $_ } @a => 0

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^3: Smartmatch alternatives
by davido (Cardinal) on Dec 17, 2013 at 16:05 UTC

    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

      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

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

        Funny, I thought it went back farther than that.


        Dave

      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^3: Smartmatch alternatives
by Anonymous Monk on Dec 17, 2013 at 16:21 UTC

    Well, L'U'first returns undef "[li]f BLOCK never returns true or LIST was empty" (as of version 1.32), so need to do check further if value is defined.

      so this should be a fast and core fallback if no better module available.

      DB<125> @a=(0)x10 => (0, 0, 0, 0, 0, 0, 0, 0, 0, 0) DB<126> sub any (&@) { my $cr=shift; defined first { &$cr } @_ } DB<127> any {$_==0} @a => 1

      well could be faster if I could avoid '$cr' but my golfing foo is weak today. =)

      update

      got it :)

      DB<138> sub any (&@) { defined &first (shift, @_) } DB<139> any {$_==0} @a => 1

      update

      better :)

      DB<144> sub any (&\@) { defined &first } DB<145> any {$_==0} @a => 1

      UPDATE

      DARN! still buggy with false positives!

      didn't sleep enough...

      update

      this seems to work...

      DB<230> sub any (&@) { my $x=&List::Util::first; defined $x } DB<231> any { $_ eq 0 } @a => "" DB<232> any { $_ eq 5 } @a => 1 DB<233> @a => (1, 2, 3, 4, 5)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Works fine until you want to see if an array contains any false values...

        use Data::Dumper; use List::Util (); sub any (&@) { my $x = &List::Util::first; defined $x } my @x = (1..3, undef, 5..10); # undef is false print Dumper(any { !$_ } @a);
        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

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

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

    No recent polls found