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
# function to be wrapped: | |
sub square-root($x) { $x.sqrt } | |
&square-root.wrap(sub ($num) { | |
nextsame if $num >= 0; | |
1i * callwith(abs($num)); | |
}); | |
say square-root(4); # 2 | |
say square-root(-4); # 0+2i |
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
class Command { | |
has Str $.name; | |
method ACCEPTS(Any:D $other) { | |
return $other ~~ Str and $other ~~ $!name; | |
} | |
} | |
my $c = Command.new(name => 'foo'); | |
say ('foo' ~~ $c); #outputs: True |
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 %results; | |
for ^10 { | |
%results{$_}<bytes> = (1..10).pick; | |
} | |
say %results.perl; | |
say %results.keys.sort({ | |
my $c = %results{$^a}<bytes> cmp %results{$^b}<bytes>; | |
$c ?? $c !! $^a.Int cmp $^b.Int; | |
}); |