Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: RFC: List::Extract

by eserte (Deacon)
on Nov 24, 2007 at 21:11 UTC ( [id://652759]=note: print w/replies, xml ) Need Help??


in reply to RFC: List::Extract

I think this function should go to List::MoreUtils (no, it's not yet there).

Replies are listed 'Best First'.
Re^2: RFC: List::Extract
by doom (Deacon) on Nov 25, 2007 at 00:22 UTC
    Overall, I like the idea of "extract", and I don't know of any equivalent (it sounds vaguely like some sort of set operation, but there's nothing like it in Set::Scalar, for example). It would indeed be a good addition to List::MoreUtils, if the maintainers of it are amendable -- but I can understand why you might want to just go ahead and package it up on CPAN and skip the negotiation process. I like the name "extract", myself (an alternate name might be "move"). I think the naming implies that any modifications to $_ should be discarded (but maybe that's reasonable in an un-perl like way, eh?). I would suggest writing variant versions that preserve side-effects but use different names for them, possibly "map_extract" or something like that.

      Thanks for you feedback.

      About keeping or discarding changes. Currently I'm leaning towards keeping changes in the returned list, but discarding changes for the other elements.

      Here's an example that motivates keeping the changes for the returned list:

      my @keywords = qw/ foo !bar baz /; my @exclude = extract { s/^!// } @keywords;
      If changes are discarded you'd have to do ugly workarounds to not duplicate the logic. But if the changes are kept and you don't want them it's trivial to change the behaviour: just add local $_ = $_;.

      The reason I want to discard changes for elements left in the array is that if changes is kept for the returned array then changes are kept for all elements, and then you can usually just as well do those changes to the array before extract.

      lodin

        i agree, especially since grep has the same 'unimplied' aliasing of $_.
      By the way, I looked around on CPAN again for something like "extract". The closest I've found is List::Part, which takes a list and subdivides into two according to a given critereon.
      my ($success, $failure) = part { /^ERR/ } <LOG>;

      It follows the "grep" convention: modifications to $_ effect both the source and the results, as I verified with a test script:

      use List::Part; my @candidates = qw( !britney !paris bettie patti ); my ($hot, $not) = part { s{^!}{} } @candidates; print "hot: ", Dumper( $hot ), "\n"; print "not: ", Dumper( $not ), "\n"; print "stuff: ", Dumper( \@candidates ), "\n";

Log In?
Username:
Password:

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

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

    No recent polls found