Skip to content

Instantly share code, notes, and snippets.

@hryk
Created April 13, 2010 08:56
Show Gist options
  • Save hryk/364445 to your computer and use it in GitHub Desktop.
Save hryk/364445 to your computer and use it in GitHub Desktop.
順列 perl
#!/usr/bin/perl
use strict;
use warnings;
my $main = sub {
my $h = shift;
print $h."\n";
};
my @ascii = qw/2 1/;
my $length = 4;
nise_permutate(\@ascii, $main, $length);
sub nise_permutate {
my ($set, $subref, $length) = @_;
my $nestfor = sub { nestfor($set, $_[0], $_[1]); };
if ($length != 0) { $subref = gen($subref) for 1..$length; }
$nestfor->($subref, '');
}
sub gen {
my $s = shift;
return sub {nestfor(\@ascii,$s,shift)};
}
sub nestfor {
my $ary = shift;
my $sub = shift;
my $bind = shift || '';
$sub->("$bind$_") for @$ary;
}
@hryk
Copy link
Author

hryk commented Apr 13, 2010

順列

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment