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

Problem wrapping prototyped function [SOLVED]

by LanX (Abbot)
on Dec 17, 2013 at 17:21 UTC ( #1067505=perlquestion: print w/ replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm trying to write a core any (details here) but I'm having an unexpected bug.

Not my day today, could plz someone explain me whats wrong here?

DB<194> @a=1..5 => (1, 2, 3, 4, 5) DB<195> first {$_ eq 3 } @a => 3 DB<196> prototype \&List::Util::first => "&\@" DB<197> sub any (&\@) { &List::Util::first } DB<198> any {$_ eq 3 } @a => undef

It's a pure wrapper which should return the same results.

from perlsub

  &NAME;         # Makes current @_ visible to called subroutine.

SOLVED
Sorry I got bitten by escaping

DB<210> sub any (&\@) { &List::Util::first } DB<211> prototype \&any => "&\\\@" DB<212> sub any (&@) { &List::Util::first } Prototype mismatch: sub DB::any (&\@) vs (&@) at (eval 254)[multi_perl +5db.pl:2279] line 1. DB<213> prototype \&any => "&\@" DB<214> any {$_ eq 0 } @a => undef DB<215> @a => (1, 2, 3, 4, 5) DB<216> any {$_ eq 0 } @a => undef DB<217> any {$_ eq 5 } @a => 5

Cheers Rolf

( addicted to the Perl Programming Language)

Comment on Problem wrapping prototyped function [SOLVED]
Select or Download Code
Re: Problem wrapping prototyped function [SOLVED]
by hdb (Parson) on Dec 17, 2013 at 17:27 UTC

    Semikolon missing?



Re: Problem wrapping prototyped function [SOLVED]
by Laurent_R (Priest) on Dec 17, 2013 at 18:59 UTC
      I'm afraid you missed the point.

      The reason why people want to use any instead of grep is to break as soon as the condition is true.

      Example: Test array for odd numbers:

      DB<47> use List::MoreUtils qw/any/ DB<48> @x=1..10000;() DB<49> $i=0; $flag= any { $i++; $_ %2 } @x; ($i,$flag) => (1, 1) DB<50> $i=0; $flag= grep { $i++; $_ %2 } @x; ($i,$flag) => (10000, 5000)

      Looks like you reimplemented grep, just slower.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Well, part of the discussion in the other post was about the fact that using grep in a Boolean context for simulating an any function is not very clear.

        The other point, which I should have mentioned and you could not guess it, is that I am not really using the List::Util version of reduce, but my own experimental lazy version of reduce, so that the process stops as soon as it has satisfied the relevant condition. (It should probably called something else than reduce, but OK, that's the idea.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-04-19 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls