Instantly share code, notes, and snippets.

View rock-paper-scissors.p6
enum Hand <Rock Paper Scissors>;
multi infix:<beats>(Paper, Rock) { True }
multi infix:<beats>(Rock, Scissors) { True }
multi infix:<beats>(Scissors, Paper) { True }
multi infix:<beats>($, $) { False }
my $p1 = Hand.roll;
say "Player 1 chooses {$p1}";
View euclidean-division.p6
multi MAIN {
my $a = prompt "a (bigger number): ";
my $b = prompt "b (smaller number): ";
my ($q, $r) = euclidean($a, $b);
say "q: $q";
say "r: $r";
}
multi MAIN("test") {
View fizzbuzz.p6
# fizzbuzz without conditionals in Perl 6
# Simple pattern matching using multi subs
sub fizzbuzz($n) {
multi case($, $) { $n }
multi case(0, $) { "fizz" }
multi case($, 0) { "buzz" }
multi case(0, 0) { "fizzbuzz" }
case $n % 3, $n % 5;
View find.p6
# see http://irclog.perlgeek.de/perl6/2015-03-11#i_10258447 for background
my @values = 5, 33, 44, 52, 66;
constant DESIRED_RESULT = 283;
for 1..Inf -> $elems {
# Three symmetry breakers:
#
# (a) Terms in sums happen in nondecreasing order of their first factor
# (b) Factors in products happen in nondecreasing order
View timings
TIME moves placements swaps resigns timeouts TOTAL req/s s/req
================================================================================================
47m 36.012s 18770 18314 225 443 13 37765 13.22 0.075
36m 51.226s 9385 18314 225 443 13 28380 12.83 0.078
26m 6.464s 9385 9157 225 443 13 19223 12.27 0.081
15m 11.009s 4693 4579 225 443 13 9953 10.93 0.091
9m 42.419s 2347 2290 225 443 13 5318 9.13 0.110
6m 52.158s 1174 1146 225 443 13 3002 7.28 0.137
5m 28.929s 588 574 225 443 13 1843 5.60 0.179
4m 46.100s 295 288 225 443 13 1264 4.42 0.226
View convert.p6
sub paragraphs { $^s.split: /\n\n+/ }
sub is-code-block { $^s ~~ /^ ' ' ** 4/ }
sub escape { $^s.trans: ['>', '<', '&'] => ['&gt;', '&lt;', '&amp;'] }
for paragraphs(slurp.trim) -> $_ is copy {
if is-code-block($_) {
say "<code><pre>{ .indent(-4).&escape }</code></pre>";
}
else {
.=subst(:g, / '[' (<-[\]]>+) '](' (<-[)]>+) ')' /, -> $/ { qq[<a href="$1">{$0}</a>] });
View bound-unbound.txt
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class C:
... def __init__(self, name):
... self.name = name
... def foo(self):
... print self.name
...
View normalize-hex-coords.p6
# The input coordinate system looks like this:
#
# (-3,+2) (-2,+2) (-1,+2) ( 0,+2) (+1,+2) (+2,+2) (+3,+2)
#
# (-3,+1) (-2,+1) (-1,+1) ( 0,+1) (+1,+1) (+2,+1) (+3,+1)
#
# (-3, 0) (-2, 0) (-1, 0) ( 0, 0) (+1, 0) (+2, 0) (+3, 0)
#
# (-3,-1) (-2,-1) (-1,-1) ( 0,-1) (+1,-1) (+2,-1) (+3,-1)
#
View session.txt
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> val = 1e3000
>>> val
inf
>>> 1 ** val
1.0
View example-output.txt
$ perl6 namer.p6
ahay ahe akak ake ane apa bib bisu
bizi bowuw but buxu cub cug dirux dotu
dozi ehe epe gib gito guc guf hemaj
heya ibo ibogi ibos ico iqic iqo itoto
ivi iwiqi ixod ixot izu jek lam lap
lem len maj meka nak obic obif ocigi
ocod ocufo odu ogiv ogu osu ovuw owoxu
oxi oxo oxu ozi pala pel qig qiq
qoci qor ric riv rof rox rus soxo