Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created October 10, 2023 18:15
Show Gist options
  • Save gfldex/852e5c129e43b07b5c6b750b241f2a7a to your computer and use it in GitHub Desktop.
Save gfldex/852e5c129e43b07b5c6b750b241f2a7a to your computer and use it in GitHub Desktop.
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