Perl 6 small stuff #15: Long story about short answers to Perl Weekly Challenge no. 1

// JavaScript/Node solution using if statements
for (i = 0; i < 20; i++) {
if (i % 3 == 0 && i % 5 == 0) console.log("Fizz Buzz");
else if (i % 3 == 0) console.log("Fizz");
else if (i % 5 == 0) console.log("Buzz");
else console.log(i);
}
# A more Perl 6 variant with "switch/case" syntax
for 1..20 {
given $_ {
when $_ %% 3 && $_ %% 5 {
say "Fizz Buzz":
}
when $_ %% 3 {
say "Fizz";
}
when $_ %% 5 {
say "Buzz";
}
default {
say $_;
}
}
}
say gather { take "Fizz " if $_ %% 3; take "Buzz" if $_ %% 5 } || $_ for 1..20
1
2
(Fizz )
4
(Buzz)
(Fizz)
7
8
(Fizz)
(Buzz)
11
(Fizz)
13
14
(Fizz Buzz)
16
17
(Fizz)
19
(Buzz)
say join " ", gather { take "Fizz" if $_ %% 3; take "Buzz" if $_ %% 5; } || $_ for 1..20
my $text = "Perl Weekly Challenge";
$text ~~ s:g/e/E/;
say $text;
say "E's: " ~ $text.comb('e').elems;
my $t = "test 1-2-3";
my $t2 = S:g/\d/NUMBER/ given $t;
say $t; # unchanged
say $t2;
# Output:
test 1-2-3
test NUMBER-NUMBER-NUMBER
say S:g/i/I/ given "original";# Output: 
orIgInal
say "{S:g/e/E/ given "Perl Weekly Challenge"}";# Output:
PErl WEEkly ChallEngE
say "{S:g/e/E/ given "Perl Weekly Challenge"}, E={$/.elems}";# Output:
PErl WEEkly ChallEngE, E=5
print "Perl Weekly Challenge" =~ s/e(?{$c++})/E/gr . ", E=$c\n";# Output:
PErl WEEkly ChallEngE, E=5
print map { "$_\n" } ($_, qw{Fizz Buzz}, "Fizz Buzz")[!($_ % 3) + !($_ % 5) * 2 ] for (1..20);# Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz Buzz
16
17
Fizz
19
Buzz

Norwegian with many interests. Programming being one of them.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store