Created
October 10, 2023 18:15
-
-
Save gfldex/852e5c129e43b07b5c6b750b241f2a7a to your computer and use it in GitHub Desktop.
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
my atomicint $spawn-limit = 8; | |
multi sub spawn(&c, |args) { | |
# dd ⚛$spawn-limit; | |
⚛$spawn-limit > 0 | |
?? ($spawn-limit⚛-- ;start { LEAVE $spawn-limit⚛++; c(|args) }) | |
!! c(|args) | |
} | |
sub collect(*@things) { | |
slip @things.map: -> $thing { $thing ~~ Promise ?? await $thing !! $thing } | |
} | |
# multi quicksort([]) { () } | |
multi quicksort(*@empty where @empty ~~ ()) { () } | |
multi quicksort(*@rest) { | |
my $pivot = shift @rest; | |
my @before = @rest.grep(* before $pivot); | |
my @after = @rest.grep(* !before $pivot); | |
flat collect spawn(&?ROUTINE.dispatcher, @before), $pivot, spawn(&?ROUTINE.dispatcher, @after) | |
} | |
my @a = ('a'..'z').roll(12).join xx 2**16; | |
# dd @a; | |
# say quicksort @a; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment