Rejection Sampling with Perl 6

Perl 6 provides a way to create lazy lists using the gather/take keywords. What I wanted to do was create an infinite list of samples from a known distribution of values. A simple way to sample from a known distribution is to do Rejection Sampling and doing this in Perl 6 is super easy.


sub sample(%distribution) {
  gather {
    loop {
      my $v = %distribution.pick;
      take $v.key if rand <= $v.value;
    }
  }
}

This function creates a Seq and you grab values from it in a lazy way. Here is a simple example assigning 100 samples from a distribution
to an array.


my %distribution = a=> 0.3, b=> 0.4, c=>0.1, d=>0.2;
my @samples = sample(%distribution)[0..^100];

Perl 6 is super fun. It has taken all the cool features from all the other languages and ignored the bad stuff. In this case, lazy lists from Haskell.

Read Seqs, Drugs, and Rock’n Roll to learn more about Sequences in Perl 6.

Strange Things are Afoot at the Circle-K

First of all, could we talk about an industry as a person? I am really starting to worry about Mr Software. Strange things are afoot at the Circle-K.

There are many smart people making software today. We got some geniuses don’t we? We make beaucoup bucks writing scribblies all day long. It really is the perfect crime. However, as flies swarm to shit, we have these MIT grads buzzing around financial institutions. We have geniuses with sun tans making guidance software for programs like “Drop it like its hot, Mr Gaddafi.”

As anyone who has filled out their taxes, or had to muddle through any government jungle, might have noticed that computers are now part of every little thing in your life. In Japan, they have toilets with more computing power than fresh air. Cars now have millions of lines of code, and when you consider how high bug densities can be per thousand lines of code, it might make you press the wrong peddle out of absolute defeat. Might as well end it now, before the software does!

Software is in a unique position in society. It can do stuff, and it’s everywhere. It touches the lives of everyone. However, we have these blokes deciding for themselves that being rich is cool, and it’s best to write software that skims the cream off the top of some morally reprehensible if not murderous dealings. Mr Software, your wife, she is not a hat.

You begin to wonder how these great minds can get by doing these bad and, at best, useless things. Are they evil? Were they conceived as vampires from Hitler’s blood? I really do not think so. Because we make software in context. We are a shape that is molded by the world. It is just that some people forget what shape they became.

Phil Zimbardo’s prison experiment (http://www.prisonexp.org/) demonstrated this artistic shaping of our selves. People really are natural born Thespians, and give them the chance, they will play the part you give them. You should visit some of the videos of the prison experiment, they will floor you. They are endlessly fascinating.

Even when we decide our shape is drooping and that we need to straighten ourselves, we have people like Stanly Miligram (http://en.wikipedia.org/wiki/Milgram_experiment) telling you that when the going gets tough, you will chicken out. But this is ok, because most do. I think the smart people call it ‘Rationalisation’.

So in the words of mighty Lenin, what is to be done? The world has so many problems to solve, how can we get people solving them. I mean, sure the MIT genius working for a financial company is solving problems. I guess, Mr Software, constipation is a problem too.