Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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
#! /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 / 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}}};
@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 / git-edit-commit
Created June 11, 2014 11:51
A git-edit-commit subcommand
COMMIT="$1"
echo "${COMMIT:=HEAD}"
OLD_HEAD=`git rev-parse HEAD`
git reset --hard $COMMIT
git reset --mixed HEAD~
git commit --patch || (git reset --hard $OLD_HEAD && exit)
git checkout .
git rebase -X theirs $OLD_HEAD

I noticed that in these two years, no-one had gone ahead an written a Haskell version of the task. So I went ahead and did it. Code below.

I had to learn a bit about Haskell date and calendar handling, looping, monads, and comprehensions before succeeding. But RosettaCode and #haskell were both good sports.

I've optimized for clarity. The isFriday condition doesn't need to be pulled out into its own function, but I felt it helped readability a bit.

import Data.Time.Calendar
import Data.Time.Calendar.WeekDate

isFriday :: Day -> Bool

@masak
masak / fridays.hs
Last active August 29, 2015 14:02
Find all Fridays-the-13th in the years 2012..2017
import Control.Monad
import Data.Time.Calendar
import Data.Time.Calendar.WeekDate
main = do
forM_ [2012..2017] $ \year -> do
forM_ [1..12] $ \month -> do
let date = fromGregorian year month 13
let (_, _, weekDay) = toWeekDate date
when (weekDay == 5) (putStrLn . showGregorian $ date)
@masak
masak / story.txt
Created May 24, 2014 18:54
A kind of proof of concept for stories
Story: writing a blog post
user selects "new blog post"
app goes to "new blog post" screen
screen stays active while the user edits it
{{DONE}}
user selects "publish post"
{{SAVE}}
app publishes post