4 Nov 2005 (updated 4 Nov 2005 at 23:37 UTC)
»
Algorithm::Combinatorics
When I thought the interface of Algorithm::Combinatorics I devised this usage in scalar context:
my $iter = combinations(\@data, $k);
while (my @c = $iter->next) {
# ...
}
and this one in list context:
my @all_combinations = combinations(\@data, $k);
When I was done with the first release (a couple of days ago), I realized something was wrong. When $k is zero the empty list has to be returned, because the empty list is a subset of @data with size $k = 0 (you know, the rationale for n choose 0 = 1).
The problem is that in the while loop the condition is an assignment to an array in boolean context, which evaluates to false if the iterator returns the empty list. Thus there was no way to support $k = 0.
The first reaction was to rule that call out, to stick with the interface I would expect from such a module. But it was a wrong decision, no matter whether it is a corner case or not, it has to be supported because I would expect combinations (and the rest of subroutines) to return that empty list. Thus I changed the iterator to return always an arrayref:
my $iter = combinations(\@data, $k);
while (my $c = $iter->next) {
# ...
}
and released 0.05 tonight.