Skip to content

Instantly share code, notes, and snippets.

@masak
masak / 01-stat
Created May 10, 2019 11:36
Playing around with generating data for box plots
$ cat stat
a249ded 68.390
a249ded 67.585
a249ded 66.976
a249ded 67.658
a249ded 66.592
a249ded 68.217
a249ded 67.328
a249ded 66.319
a249ded 68.024
@masak
masak / rock-paper-scissors.p6
Last active August 3, 2016 12:46
A modern take on rock-paper-scissors
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 November 24, 2015 16:18
Euclidean division: emulating infix:</> and infix:<%> with only addition, subtraction, multiplication, conditionals, comparison, and recursion
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 September 10, 2017 03:02 — forked from walkermatt/fizzbuzz.clj
fizzbuzz without conditionals in Perl 6
# 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 August 29, 2015 14:16
Find all the ways to combine a set of integers with a set of operators to get a desired result
# 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 August 29, 2015 14:13
Measuring the time it takes to run a script with a certain number of database roundtrips
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 December 22, 2015 23:13
A short converter script for people who want to write their post in Markdown but convert it to Wordpress broken HTML
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 November 19, 2014 14:14
bound and unbound methods in Python
$ 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 August 29, 2015 14:07
Normalize hex board coordinates
# 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 October 2, 2014 05:36
Python has 1 ** Inf as 1
$ 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