Skip to content

Instantly share code, notes, and snippets.

@xaicron
Created May 19, 2011 15:04
Show Gist options
  • Save xaicron/980963 to your computer and use it in GitHub Desktop.
Save xaicron/980963 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use AE;
use List::Util qw(max);
use Data::Dumper;
sub sleep_sort {
my @args = @_;
my (@w, @rv);
my $cv = AE::cv;
my $wait = 0;
for my $argv (@args) {
push @w, AE::timer $argv, 0, sub { push @rv, $argv };
$wait = max($wait, $argv);
}
my $sender = AE::timer $wait + 0.1, 0, sub { $cv->send(@rv) };
return $cv->recv;
}
print Dumper [ sleep_sort 2, 4, 1, 3 ];
print Dumper [ sleep_sort 2.4, 0.5, 1.111, 3.14 ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment