Created
April 7, 2016 03:28
-
-
Save awwaiid/655f4dd4f7dd5bfa1719aae788a9cde9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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"; |
This file contains hidden or 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
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