Last active
October 17, 2015 18:16
-
-
Save BenGoldberg1/47b3a982b1bdd630cbbc to your computer and use it in GitHub Desktop.
Sort Using Supplies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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