in reply to Efficient Unique Nested Combinations

When the "symbols" are not ordered in the initial AoA, it skips some results.
my @symbols=( ['c','b','e'], ['a','f','d'], ); hash(\@symbols); ab ac ae bd bf cd cf de ef combo(\@symbols); c f c d b f b d e f
The "greater than" solution thus assumes an order that is not neccessarily there in the initial AoA.
Cheers

Replies are listed 'Best First'.
Re^2: Efficient Unique Nested Combinations
by Roy Johnson (Monsignor) on Jun 26, 2007 at 15:25 UTC
    I resorted to using a hash to catch what had been seen. The model still offers some short-circuiting, and is moderately faster than "hash" in my benchmarking (the more duplicates there are to avoid, the better "combo" does). Note also that you didn't change my code to use the passed-in array ref. I have done so here.
    sub combo { my $symbols = shift; my %seen; my $combos = NestedLoops([ $symbols->[0], map { my $_hold = $_; sub { [grep {!$seen{join(':', sort @_, $_)}++} @{$symbols->[$_hold +]}] } } 1..$#$symbols ]); while (my @result = $combos->()) { print "@result\n"; } print "\n"; }

    Caution: Contents may have been coded under pressure.