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

Re: findone { coderef } @array

by jynx (Priest)
on Feb 26, 2002 at 04:15 UTC ( [id://147470]=note: print w/replies, xml ) Need Help??


in reply to findone { coderef } @array


A couple of comments on style:

  • You didn't use warnings or strict, even though your code seems to run fine with warnings turned on. The only difference for turning on strict is localizing %find_cache with my. My testing wasn't that extensive, just a little playing around, so there may be some deeper issues that i completely missed.

  • On the wantarray inside the if (&$coderef) block, it seems to read better if you put the return outside the ?: operator. This isn't necessarily like (ab)using map in a void context, but i consider it to be roughly similar (map has the for loop, ?: has the if/else block)

  • Since you don't sprinkle $_ anywhere but inside the for loop, the local $_; call can probably be moved to just inside the for loop. Whenever i see that at the top of a block i start expecting heavy usage of $_ to go on, but in this case i was reading carefully for no reason. Putting localization code in the smallest possible block is also generally a good thing (imho).

  • You include a hook for giving a starting point, but nothing for the ending point. It would be a simple change and add some (admittedly only moderately useful) functionality. This seems like a natural way to extend findone further though...

  • And speaking of extending code, now that you can findone you can write a wrapper to find as many as you want :-)

Unfortunately my CPAN search skills aren't very good, so i don't know if anyone else has written a lazy grep (which is what this is with some extra bells and whistles, if my understanding is correct). If no one has posted one to CPAN or if the code submitted isn't that good this seems a good candidate for packaging and sending in...

my $0_02,
jynx

Replies are listed 'Best First'.
Re: Re: findone { coderef } @array
by shotgunefx (Parson) on Feb 26, 2002 at 07:19 UTC
    Thanks for the feedback. I left out the my and the use (warnings && strict) while pasting. My bad.
    The reason I have the wantarray is because I wanted to be able to ignore the index with out forcing list context.
    my ($match,$index) = findone { $_ > 100 } @nums ; my $matchonly = findone { $_ > 100 } @nums ;
    Your right about the local $_ (Lava flow from first try). I lost it all together as $_ is localized by for anyway.
    Could you elaborate on what you had in mind for an end hook?

    What I think is interesting about this (for me anyway) is being able to retain state without resorting to objects. Using an %args hash for a function and this technique, it should be pretty easy to write iterator functions without OO. Nothing wrong with objects but for something like this, the grep syntax feels more natural and looks better to me.
    I don't know if there is anything on CPAN for this. The closest I found was first() in List::Util
    It was mainly an "I should be able to do that.." exercise.

    -Lee

    "To be civilized is to deny one's nature."

      For the wantarray, i see the usefulness, but i was commenting on moving the return outside of the ?:. It's not really a big deal, but it would look something like:
      return wantarray ? ($_,'blah') : $_;
      For the end hook, you have a start index, you can receive an extra scalar as the end index, and only pick it up if they've put in a start index as well. Prototype would be kinda like:

      findone { code } @list
      findone { code } @list, $start_index
      findone { code } @list, $start_index, $end_index

      So if they know they don't need to walk further than a certain place on the array they can be certain to stop there.

      jynx

        Good point. I added the end_index.

        -Lee

        "To be civilized is to deny one's nature."

Log In?
Username:
Password:

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

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

    No recent polls found