Skip to content

Instantly share code, notes, and snippets.

@masak
masak / boxes-and-pebbles.p6
Created June 17, 2014 08:06
Boxes and pebbles exploration
class Conf {
has @.boxes where *.all >= 0;
method gist { "[$.boxes]" }
}
sub conf(@boxes) { Conf.new(:@boxes) }
sub n(Conf $c) { $c.boxes.elems }
sub REPL($line) {
say "> $line";
@masak
masak / groups.p6
Last active August 29, 2015 14:04
Some fun with cyclic groups, direct products, and isomorphism
role Group {
method elements { ... }
method id { ... }
method op($l, $r) { ... }
}
macro assert($fact) {
quasi {
die "FAILED: ", $fact.Str
unless {{{$fact}}};
#! /usr/local/bin/perl6
use v6;
# don't need to use warnings or strict
# original code had the class after the .new call; that doesn't work
# because of one-pass parsing
# kinda un-idiomatic to have a class name not start with a capital;
# but it *works*, so I'll keep it
class life {
@masak
masak / draft.diff
Created August 29, 2014 14:59
Draft of change to smartmatching semantics
diff --git a/S03-operators.pod b/S03-operators.pod
index a6f3eb5..2d651b0 100644
--- a/S03-operators.pod
+++ b/S03-operators.pod
@@ -14,8 +14,8 @@ Synopsis 3: Operators
Created: 8 Mar 2004
- Last Modified: 18 Aug 2014
- Version: 279
@masak
masak / circles.md
Last active August 29, 2015 14:06
Two circles

If the circles have the same center, they intersect at no point if they have different radius, or at all points if they have the same radius.

Leaving that case aside, we can assume without loss of generality that one circle is centered at (0, 0) and has radius 1, while the other one is centered at (x, 0) with x > 0 and has radius R with R ≥ 1. We're left with three cases.

  • If R is outside the closed interval x-1..x+1, the circles don't intersect.

  • If R is on either endpoint of that interval, x-1 | x+1, the circles intersect at exactly one point, either (+1, 0) or (-1, 0), respectively.

  • Finally, if R is strictly inside the interval x-1..x+1, the two intersection points are of the form (cos ±α, sin ±α), where α satisfies (Pythagoras) R² == (x - cos α)² + sin²α == x² - 2 x cos α + cos²α + sin²α == x² - 2 x cos α + 1. Thus α = acos (x² - R² + 1)/(2 x).

@masak
masak / example-output.txt
Created September 7, 2014 09:15
Generate typing-friendly names
$ 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
@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
@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 / 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 / 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