Authors,
I have a list of elements, qw[a b c d e] and I wanted to steal some code
from CPAN to divvy it up into two subsets of 2 and 3 members. And
enumerate all the possibilities. For instance:
(a b) (c d e)
(a c) (b d e)
(a d) (b c e)
(a e) (b c d)
(b c) (a d e)
(b d) (a c e)
(b e) (a c d)
(c d) (a b e)
(c e) (a b d)
(d e) (a b c)
I didn't find any code to steal, which surprised me. I searched for
various combination of Set List Data Partition and drew a blank. So I
wrote some code, and the documentation looks like this:
NAME
Set::Partition - Enumerate all arrangements of a set in fixed subsets
VERSION
This document describes version 0.01 of Set::Partition, released
2006-05-22.
SYNOPSIS
use Set::Partition;
my $s = Set::Partition->new(
list => [qw('a' .. 'e')],
partition => [2, 3],
);
while (my $p = $s->next) {
print join( ' ', map { "(@{$p->[$_]})" } 0..$#$p ), "\n";
}
# produces
(a b) (c d e)
(a c) (b d e)
(a d) (b c e)
(a e) (b c d)
(b c) (a d e)
(b d) (a c e)
(b e) (a c d)
(c d) (a b e)
(c e) (a b d)
(d e) (a b c)
DESCRIPTION
"Set::Partition" takes a list of elements (scalars or references or
whatever) and a list numbers that represent the sizes of the
partitions into which the list of elements should be arranged.
The resulting object can then be used as an iterator which returns a
reference to an array of lists, that represents the original list
arranged according to the given partitioning information. All possible
arrangements are returned, and the object returns "undef" when the
entire combination space has been exhausted.
METHODS
new Creates a new "Set::Partition" object. A set of key/value
parameters can be supplied to control the finer details of the
object's behaviour.
list: points to the list of elements in the set.
partition: the list of integers representing the size of the
partitions used to arrange the set. The sum should be equal to
the number of elements given by list. If it less than the
number of elements, a dummy partition will be added to
equalise the count. This partition will be returned during
iteration. If the sum is greater than the number of elements,
"new()" will "croak" with a fatal error.
next Returns the next arrangement of subsets, or "undef" when all
arrangements have been enumerated.
reset Resets the object, which causes it to enumerate the
arrangements from the beginning.
$p->reset; # begin again
DIAGNOSTICS
None.
NOTES
The order within a set is unimportant, thus, if
(a b) (c d)
is produced, then the following arrangement will never be encountered:
(a b) (d c)
On the other hand, the order of the sets is important, which means
that the following arrangement *will* be encountered:
(c d) (a b)
__END__
Now my observation is that Set::Partition seems a little to generic
and/or I would be trampling on namespace with the proposed name. So
either do people have strong feelings about the name and/or a better
name for a module? Hint: When you read the subject of this message, did
you have a pretty good idea of what this module does?
Or did I overlook a module that already does this?
Thanks for your insights,
David
--
"It's overkill of course, but you can never have too much overkill."