Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Perl 6's part()

by BrentDax (Hermit)
on Jan 10, 2003 at 03:21 UTC ( #225721=snippet: print w/ replies, xml ) Need Help??

Description: Perl 6 is slated to include a new list manipulation function, part (for partition, although Damian likes it 'cause it can mean so many other things). It takes a codeblock and a list; the codeblock returns an index, and part puts it into an arrayref corresponding to that index. For example:
my($good, $bad, $ugly)=part { /good/ ? 0 : /bad/ ? 1 : /ugly/ ? 2 : (warn "$_ didn't categorize" and 3) } @people;
If you replace the $good, $bad, and $ugly with *good, *bad, and *ugly in that example, you'll get the arrays in @good, @bad and @ugly.
sub part(&@) {
    my($code, @list)=@_;
    my @ret=();
    push @{$ret[&$code()]}, $_ for @list;
    return @ret;
}
Comment on Perl 6's part()
Download Code
Re: Perl 6's part()
by jdporter (Canon) on Jan 10, 2003 at 03:52 UTC
    Hmm. Interesting.
    I like that I can do this:
    my %h; @h{qw( good bad ugly )} = part { /good/ ? 0 . . . } @people;

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Perl 6's part()
by mojotoad (Monsignor) on Jan 10, 2003 at 17:58 UTC

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-09-20 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls