Skip to content

Instantly share code, notes, and snippets.

@BenGoldberg1
Last active October 17, 2015 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenGoldberg1/47b3a982b1bdd630cbbc to your computer and use it in GitHub Desktop.
Save BenGoldberg1/47b3a982b1bdd630cbbc to your computer and use it in GitHub Desktop.
Sort Using Supplies
#!/usr/bin/env perl6
use v6;
sub mysort( Supply $s ) {
my (@same, @supplies, @promises);
my $p = Promise.new;
$s.tap( -> $val {
if @same {
given $val <=> @same[0] {
when Same { push @same, $val }
when Less { @supplies[0].emit( $val ) }
when More { @supplies[1].emit( $val ) }
}
} else {
push @same, $val;
@supplies = Supply.new xx 2;
@promises = map { mysort( $_ ) }, @supplies;
}
}, done => {
if @same {
.done for @supplies;
$p.keep( [|@promises[0].result, |@same, |@promises[1].result] );
} else {
$p.keep( [] );
}
} );
$p;
}
my $sort-supply = Supply.new;
my $sort-promise = mysort($sort-supply);
$sort-supply.emit($_) for (1..20).pick(*);
$sort-supply.done;
.say for $sort-promise.result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment