Created
August 11, 2012 13:25
-
-
Save masak/3324433 to your computer and use it in GitHub Desktop.
Small and attractive Perl 6 snippets
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
my $birth-date-string = prompt "When were you born (yyyy-mm-dd)?"; | |
my $birth-date = Date.new($birth-date-string); | |
my $today = Date.today; | |
my $age = $today.year - $birth-date.year; | |
# But your birthday this year may not have been yet | |
if $today.day-of-year < $birth-date.day-of-year { | |
$age--; | |
} | |
say "You are $age years old!"; |
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
my @scale = ("$_$_" for 0..9, <a b c d e f>); | |
my @zeroes = '00' xx 16; | |
# Fade from red to blue | |
for @scale.reverse Z @zeroes Z @scale -> $r, $g, $b { | |
say "#$r$g$b"; | |
} |
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
for 10 ... 0 -> $count { | |
say "$count..."; | |
LAST { say "Liftoff!" }; | |
} |
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
my $sentence = "I am going to go feed the the cat."; | |
if $sentence ~~ / (\w+) \h+ $0 / { | |
say "Duplicate word '$0 $0' found at position $/.from()."; | |
} |
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
my @beers = 'beers', 'beer', 'beers' xx 97; | |
for reverse(1..99) Z reverse(0..98) -> $n, $nn { | |
say "$n @beers[$n] on the wall!"; | |
say "$n @beers[$n]!"; | |
say "Take one down, pass it around,"; | |
say "$nn @beers[$nn] on the wall!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment