Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AnyEventでsleep_sort/自前で簡易イベントループ実装したの巻/List::Utilいらなくね?/もう何も要らなくね←いまここ
sub array_dumper(@){
my @array = @_;
my $str = '[';
if(@array){
foreach my $p (@array){
if(defined $p){
if(ref($p)){
if(ref($p) eq 'ARRAY'){
$str .= array_dumper(@$p) . ', ';
}elsif(ref($p) eq 'HASH'){
$str .= hash_dumper(%$p) . ', ';
}
}else{
if(int($p) or $p == 0){
$str .= "${p}, ";
}else{
$str .= "'${p}', ";
}
}
}else{
$str .= 'undef, ';
}
}
chop($str);
chop($str);
}
return $str . "]\n";
}
sub hash_dumper(%){
die 'You should write hash_dumper().';
}
sub timer(&$){
my($code, $time) = @_;
return sub{
my $start_time = shift;
my $now_time = shift;
if(($now_time - $start_time) >= $time){
$code->();
return 1;
}else{
return 0;
}
}
}
sub timer_run(@){
my @event = @_;
my $all = scalar(@event);
my $count = 0;
my @endflang = (0) x $all;
my $start = time;
while($count < $all){
foreach my $i (0 .. $#event){
unless($endflang[$i]){
my $stc = $event[$i]->($start, time);
$count += $endflang[$i] = $stc;
}
}
}
}
sub sleep_sort{
my(@args) = @_;
my @out;
my $max = max(@args);
my @event;
foreach my $i (@args){
push( @event, timer{
push(@out, $i);
} $i);
}
timer_run(@event);
return @out;
}
my @args = @ARGV or die "Usage: $0 3 5 6 7 2 1 55 6";
print "before...\n";
print array_dumper(@args);
my @sorted = sleep_sort(@args);
print "after...\n";
print array_dumper(@sorted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.