in reply to Derangements iterator
I know it's cool to do iterators, but something about derange { print "@_\n" } 1 .. 5; seems more perlish to me...
sub _derange { my( $cb, $todo, @v ) = @_; @$todo or return $cb->( @v ); my %seen; @seen{@v}=(); my( $range, @todo ) = @$todo; _derange( $cb, \@todo, @v, $_ ) for grep { ! exists $seen{$_} } @$range; } sub derange(&@) { my $cb = shift; _derange( $cb, [ map { my $x = $_; [ grep { $_ ne $x } @_ ] } @_ ] ); }
If one needed to allow for deranging a list which contains duplicates, one could simply derange the list of its indices. E.g.:
my @x = ( 1 .. 4, 4 ); derange { print "@x[@_]\n" } 0 .. $#x;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Derangements iterator (callbacks)
by tye (Sage) on Dec 29, 2005 at 19:43 UTC | |
by Roy Johnson (Monsignor) on Dec 29, 2005 at 19:47 UTC | |
by tye (Sage) on Dec 29, 2005 at 19:56 UTC | |
by Roy Johnson (Monsignor) on Dec 30, 2005 at 18:22 UTC | |
by tye (Sage) on Dec 30, 2005 at 19:11 UTC | |
| |
by Anonymous Monk on Jan 01, 2006 at 17:21 UTC | |
by Corion (Patriarch) on Jan 01, 2006 at 17:53 UTC | |
by Anonymous Monk on Jan 01, 2006 at 18:21 UTC | |
by demerphq (Chancellor) on Jan 02, 2006 at 12:33 UTC | |
| |
|
Re^2: Derangements iterator (duplicates)
by tye (Sage) on Jan 08, 2008 at 15:35 UTC |