Skip Menu |

This queue is for tickets about the Scalar-List-Utils CPAN distribution.

Report information
The Basics
Id: 132282
Status: rejected
Priority: 0/
Queue: Scalar-List-Utils

People
Owner: Nobody in particular
Requestors: ether [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: wishlist: partition function
I know this is just spitting in the wind unless I JFDI, but I had a thought in passing so I wanted to jot it down here so it isn't forgotten. A 'partition' function would be useful, to divide a list into 2 or more lists according to some condition. It is fairly straightforward to implement this in terms of 'reduce', but it would no doubt be faster if it was done directly in XS. Here is the reduce implementation (where the provided sub returns the index to use for the destination lists): sub partition (&@) { my $sub = shift; my $results = reduce { push @{ $a->[$sub->($b)] }, $b; $a; } [ [], [] ], @_; return @$results; } e.g. partition(sub { $_[0] % 2 }, 0..10) yields the result: [ 0, 2, 4, 6, 8, 10 ], [ 1, 3, 5, 7, 9 ],
Isn't this List::UtilsBy::partition_by? https://metacpan.org/pod/List::UtilsBy#partition_by %parts = partition_by { KEYFUNC } @vals Returns a key/value list of ARRAY refs containing all the original values distributed according to the result of the key function block. Each value will be an ARRAY ref containing all the values which returned the string from the key function, in their original order. my %balls_by_colour = partition_by { $_->colour } @balls; Because the values returned by the key function are used as hash keys, they ought to either be strings, or at least well-behaved as strings (such as numbers, or object references which overload stringification in a suitable manner). -- Paul Evans
ahahahahah cheers :)