Skip to content

Instantly share code, notes, and snippets.

@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 / balanced-bracket.p6
Last active May 11, 2018 07:45
Uniformly selecting a balanced-bracket string of lengh n
# Recursive definition: a balanced-bracket string is always empty or of the form
#
# [S1]S2
#
# where S1 and S2 are balanced-bracket strings.
#
# Let n(S) be the number of bracket pairs in the balanced-bracket string S. And let
# C(n) be the number of balanced-bracket strings with exactly n bracket pairs.
# C(n) happens to be the Catalan sequence <http://oeis.org/A000108>:
#
@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;
Hello,
I'm curious about the Perl 6 job offer that I found on this page:
<http://www.carriere-info.fr/offre-emploi-perl-88929.html?_c51>
I'm a Perl 6 programmer, and a group of us are currently in the process of writing
up a book for Perl 6 programming, set to coincide with the release of Rakudo Star.
You can find the book here:
@masak
masak / classify.txt
Created July 31, 2013 13:42
A classify of my own
>>> oddness = lambda n: n % 2
>>> oddness(0)
0
>>> oddness(3)
1
>>> def classify(things, func):
... result = {}
... for t in things:
... if func(t) in result:
... result[func(t)].append(t)
@masak
masak / bounded-contexts.txt
Created August 7, 2011 16:08
airport model
AR \ BC | Check in | Drop L. | Passp. C | Board | Transit |
----------+----------+---------+----------+-------+---------+
Passenger | Y | | Y | Y | |
----------+----------+---------+----------+-------+---------+
Luggage | Y | Y | | | Y |
----------+----------+---------+----------+-------+---------+
Flight | Y | | | | |
----------+----------+---------+----------+-------+---------+
Three bounded contexts:
@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 / control-shift.diff
Created January 6, 2014 20:07
like this, diakopter?
diff --git a/t/jvm/01-continuations.t b/t/jvm/01-continuations.t
index 1ec9228..cdb9acb 100644
--- a/t/jvm/01-continuations.t
+++ b/t/jvm/01-continuations.t
@@ -29,7 +29,7 @@ plan(22);
# nqp::continuationcontrol(0, nqp::null(), { $run++ });
# CATCH { $ex := $! }
# }
- # ok( $ex ~~ /'no matching'/, 'shift dies with no reset' );
+ # ok( $ex ~~ /'no matching'/, 'control dies with no reset' );
@masak
masak / post.md
Last active December 31, 2015 09:29
Day 15 - Numbers and ways of writing them

Consider the humble integer.

my $number-of-dwarfs = 7;
say $number-of-dwarfs.WHAT;   # (Int)

Integers are great. (Well, many of them are.) Computers basically run on integers. Also, God made the integers, and everything else is basically cheap Chinese knock-offs of the real thing. If they're so important, we'd expect to have lots of good ways of writing them in any respectable programming language.

Do we have lots of ways of writing integers in Perl 6? We do. (Does that make Perl 6 respectable? No, because it's a necessary but not sufficient condition. Duh.)

One thing we might want to do, for example, is write our numbers in other number bases. The need for this crops up now and then, for example if you're stranded on Binarium where everyone has a total of two fingers:

@masak
masak / post.md
Last active December 30, 2015 17:49
Hashes and pairs

Hashes are nice. They can work as a kind of "poor man's objects" when creating a class seems like just too much work for the occasion.

my $employee = {
    name => 'Fred',
    age => 51,
    skills => <sweeping accounting barking>,
};

Perl (both 5 and 6) takes hashes seriously. So seriously, even that there's a dedicated sigil for them, and by using it you can omit the braces in the above code: