Skip to content

Instantly share code, notes, and snippets.

@cognominal
Created January 8, 2022 19:46
Show Gist options
  • Select an option

  • Save cognominal/2dc55c220f6eaac7f34abd44f20f43d3 to your computer and use it in GitHub Desktop.

Select an option

Save cognominal/2dc55c220f6eaac7f34abd44f20f43d3 to your computer and use it in GitHub Desktop.
cursor $!a
# the goal of this experiment is to access the long name of a method
# that result in the current match.
# A match being derived from cursor, maybe its $!name is what I want.
# I try to add a &name method in Cursor.nqp but it does not it.
# I list the method name to check.
# that was obvious. I tested using nqp instead of ./nqp
sub quicksort(@list, $low, $high, &compare) {
if $low < $high {
my $q := partition(@list, $low, $high, &compare);
quicksort(@list, $low, $q-1, &compare);
quicksort(@list, $q+1, $high, &compare);
}
@list
}
sub partition(@list, $low, $high, &compare) {
my $pivot := @list[$high];
my $i := $low - 1;
my $j := $i;
while (++$j) < $high {
swap(@list, ++$i, $j) if compare($pivot, @list[$j]) < 0;
}
swap(@list, ++$i, $high);
$i;
}
sub swap(@list, $i, $j) { my $tmp := @list[$i]; @list[$i] := @list[$j]; @list[$j] := $tmp; }
my &cmp := -> $a, $b { $a gt $b ?? -1 !! $a lt $b ?? 1 !! 0 }
sub sort(@list, &compare = &cmp ) {
@list := nqp::clone(@list); # take a copy
quicksort(@list, 0, nqp::elems(@list)-1, &compare);
}
sub print-list(@l) {
# say(nqp::join(" ", @l))
my @ll;
nqp::push(@ll, ~$_) for @l;
say(nqp::join(" ", @ll))
}
print-list([1, 2]);
my @l := [ 3, 33, 11, 1, 22 ];
print-list( sort(@l));
grammar A {
token TOP { <a> }
proto token a { <...> }
token a:sym<a> { a }
}
my $/ := A.parse("a");
# for $/.HOW.parents($/) {
# say($_.HOW.name($/));
# }
# for $/.HOW.does($/) {
# say($_.HOW.name($/));
# }
say(~$/);
say("name " ~ $<a>.name);
@l := [];
nqp::push(@l, $_.name) for $/.HOW.methods($/);
@l := sort(@l);
print-list(@l);
# say($/.name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment