Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
say "hello";
subset Natural of Int where * > -1;
# Using a given/when (like a case statement)
sub conv1(Natural $x) {
given $x {
when 0 { "0" }
when 1 { "1" }
when * %% 2 { conv1($x div 2) ~ "0" }
default { conv1($x div 2) ~ "1" }
}
}
# Multi-dispatch, like elixir or haskell
multi conv2(0) { "0" }
multi conv2(1) { "1" }
multi conv2(Natural $x where * %% 2) {
conv2($x div 2) ~ "0"
}
multi conv2(Natural $x where * !%% 2) {
conv2($x div 2) ~ "1"
}
sub postfix:<→b>($x) {
conv2($x);
}
say "Given/when style:";
say conv1(1233);
say conv1(7);
say conv1(0);
say "Multi dispatch style:";
say conv2(1233);
say conv2(7);
say conv2(0);
say "Crazy postfix style:";
say 1233→b;
say 7→b;
say 0→b;
say "goodbye";
hello
Given/when style:
10011010001
111
0
Multi dispatch style:
10011010001
111
0
Crazy postfix style:
10011010001
111
0
goodbye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.