Skip to content

Instantly share code, notes, and snippets.

@masak
masak / rock-paper-scissors.p6
Last active Aug 3, 2016
A modern take on rock-paper-scissors
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}";
@masak
masak / euclidean-division.p6
Last active Nov 24, 2015
Euclidean division: emulating infix:</> and infix:<%> with only addition, subtraction, multiplication, conditionals, comparison, and recursion
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") {
@masak
masak / fizzbuzz.p6
Last active Sep 10, 2017 — forked from walkermatt/fizzbuzz.clj
fizzbuzz without conditionals in Perl 6
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;
@masak
masak / find.p6
Last active Aug 29, 2015
Find all the ways to combine a set of integers with a set of operators to get a desired result
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
@masak
masak / timings
Last active Aug 29, 2015
Measuring the time it takes to run a script with a certain number of database roundtrips
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
@masak
masak / convert.p6
Last active Dec 22, 2015
A short converter script for people who want to write their post in Markdown but convert it to Wordpress broken HTML
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>] });
@masak
masak / bound-unbound.txt
Created Nov 19, 2014
bound and unbound methods in Python
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
...
@masak
masak / normalize-hex-coords.p6
Last active Aug 29, 2015
Normalize hex board coordinates
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)
#
@masak
masak / session.txt
Created Oct 2, 2014
Python has 1 ** Inf as 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
@masak
masak / example-output.txt
Created Sep 7, 2014
Generate typing-friendly names
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
You can’t perform that action at this time.