Skip to content

Instantly share code, notes, and snippets.

@librasteve
Last active December 13, 2023 12:59
Show Gist options
  • Save librasteve/170121dc120921f4608b8baf841c294e to your computer and use it in GitHub Desktop.
Save librasteve/170121dc120921f4608b8baf841c294e to your computer and use it in GitHub Desktop.
Point Sort Example
class Point {
has Int $.x;
has Int $.y;
method distance(Point $other) {
abs(self.y - $other.y) + abs(self.x - $other.x);
}
#`[
method Str {
"(" ~ self.x ~ ", " ~ self.y ~ ")";
}
#]
}
multi infix:<\<=\>>(Point:D $a, Point:D $b) {
$a.x <=> $b.x or $a.y <=> $b.y
}
multi infix:<cmp>(Point:D $a, Point:D $b) {
$a <=> $b
}
my Point $a .= new(:x(0), :y(0));
my Point $b .= new(:x(1), :y(1));
my Point $c .= new(:x(2), :y(2));
say ($c, $a, $b).sort; #wrong, see https://stackoverflow.com/questions/53760999/perl6-sort-doesnt-use-overridden-cmp
say ($c, $a, $b).sort: &[cmp];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment