-
-
Save peschwa/6c3521d3190c51f80eb8 to your computer and use it in GitHub Desktop.
Translate English to Pig Latin with ease!
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
multi MAIN($arg where *.IO.f) { | |
say translate($arg.IO.slurp).Str | |
} | |
multi MAIN($arg) { | |
say translate($arg).Str | |
} | |
sub translate(Str:D $english) { $english.words.map: &translate-word } | |
sub translate-word($word) { | |
# 'y' is left in the consonants because | |
# in English it is always a consonant when | |
# at the beginning of a word. | |
my @vowels = <a e i o u A E I O U>; | |
my @consonants = (("a".."z", "A".."Z") (-) @vowels).keys.cache; | |
given $word { | |
when $word.starts-with(any(@vowels)) { | |
$word ~ 'yay'; | |
} | |
when $word.starts-with(any(@consonants)) { | |
samecase( | |
([~] $word.comb(/./).cache.rotate(1)) ~ 'ay', | |
$word ~ 'ay' | |
); | |
} | |
default { $word } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment