Wednesday, March 30, 2011

Perl 6 for finding non trivial powers less than 2 ^ 20

I recently noticed Terry Jones's blog post, "The eighty six non-trivial powers ≤ 2^20." I was immediately struck by how well this lent itself to being implemented in Perl 6, so I grabbed a handy Rakudo interpreter and translated it rather literally and then played with some slightly more idiomatic approaches. I still think I'm missing something with respect to the main loop, but I like what I came up with. The code is on github in my scripts repo.

In comparing the two (Terry's Python and my Perl), I really felt like this summed up the reason I want to code in Perl 6 more often. I work in Python for work, these days, and while it's a fine language with some very nice tools (django is especially nice), I frequently find myself wishing Rakudo were just a bit more production-ready.

So, without further ado, some thoughts about the two versions...

Code length is ignorable. Sure, the Perl is shorter in some areas and longer in others, but if that's interesting to you in terms of a mathematical problem, then there may be a problem.

However, in terms of clarity, I find two distinguishing features. One is strongly in Perl's favor, and one is mildly in Python's favor:

Pro Perl:

The largest thing I notice is that last block. It's much clearer to me that were'e concatenating the key with the value, after both are transformed in some way.

This reads a bit like, "print the formatted number in the key, followed by an equal followed by the values, joined internally with "^" and externally with commas."

Meanwhile the Python is something like, "print a formatted number, and equal and a formatted string where the number is in n and the formatted string is a comma joining a formatted pair of numbers joined with a "^" where the numbers are a and b where a and b are the tuples in powers indexed by n."

It's not that the code is *longer* that's the problem, but that it's far more disjoint. Even the indentation has to be complicated to suit the way they're mixing infix, functional and postfix grammatical elements.

Pro Python:

The declaration and use of powers is far cleaner in Python due to the handy defaultdict concept. This bypasses the defaulting that we do in Perl:

    (%powers{+$n} //= []).push([$a,$b]);

Beyond being punctuation-heavy, this is exactly the same underlying behavior as the Python. We default the bucket at $n to an empty list and then append the new item. The difference is that in Python the defaulting behavior is taken care of behind the scenes.

I say that this is minor because I understand this to be functionality that Rakudo Perl 6 has simply not implemented yet. You will eventually write:

    my Array %powers;
    ...
    %powers{$n}.push([$a, $b]);

very much like its Python counterpart.

Update: I'm told by the good folks on #perl6 that the right way to push onto a hash entry with a default empty array is:

    %powers.push: $n => [$a, $b];

Which is certainly cleaner than all of the  above, so woot!

Sunday, March 20, 2011

Two free albums worth paying for

As readers of my essays know, I've been listening to free music recently. That's not to say that I participate in file sharing of otherwise for-pay music. Rather, I enjoy music which artists choose to share for free. I've even donated a bit or bought for-pay albums as a result.

This month, I've found two new items that really tickle my fancy. The first is Commence, the second free album from Paul Lisak & After the Ice, the first being It Happens All the Time.That album's clear and nearly hypnotic rifs and commanding vocals were my first taste of free music that I could actually love. Commence doesn't live up to this standard, but it's a solid rock/pop/alternative album which far surpasses the bulk of the free music I've been listening to of late. It's a bit bluesier than Happens which can definitely be nice if that's what you're in the mood for.

The second is the instrumental/ambient album, Shedneryan by Roger Subirana Mata whose work was covered in 2008 on Free Albums Galore (I've also linked to one of his CDs on Amazon, to the left, if you're not ad-blocking), an excellent blog for finding free music. This new album was released last month and I'm not sure that I agree with one poster to Jamendo who said that it was on-par with Tubular Bells, but it's an excellent atmospheric piece that I'd compare favorably with Tangerine Dream (especially the first track), Vas (the second track) and many of my other favorites. There's even what I think is a little bit of dark soundtrack ala Danny Elfman and John Massari thrown in for good measure.

Tuesday, March 15, 2011

Nuclear power: why is it so terrifying?

The earthquake triggers a massive
gas explosion.
Credit: Reuters
I'm reading accounts of the Fukushima Daiichi nuclear plant and its ongoing woes after Japan's 8.9 (now the USGS is saying 9.0) earthquake on the 11th. Now, this is a tragic event, to be sure, but I'm baffled by the laser-focus that this one event is getting. Power-generation of many sorts suffer catastrophic failure under these circumstances. Gas mains ignite, fuel tankers spill, oil is dumped into oceans and rivers. And yet, for some reason we're focusing on the one event that has yet to injure anyone. Sure, it might result in a deadly release of radiation that prevents a wide area from being re-built for the foreseeable future, but I get the impression that people are somehow equating this to some fundamental instability in nuclear power.

Keep in mind that this earthquake has literally removed entire towns. They'r gone. The buildings don't exist anymore, except as rubble, swept miles away. Under these circumstances, the low death toll (5000-10000, estimated) is a tribute to Japan's excellent preparedness. There's no one sitting a block away from the power plant wondering, "oh dear, is it going to affect me?" No, everyone is either dead or evacuated. Thousands are dead. Thousands more are missing. The fact that a nuclear power plant is suffering a failure is quite simply not changing the scope of this disaster.

But still, we continue to raise alarms and demand stricter regulations as a result of our irrational fear of what is clearly our safest form of power generation. We continue to put out gas fires and clean up oil spills at an ever-increasing rate, but after a 9.0 earthquake a nuclear plant is damaged? It must be time for stricter regulation!

Don't get me wrong. I'm all for strong safety surrounding nuclear power. I think Chernobyl teaches us what happens when basic safety technology is ignored. However, you don't then ratchet up the level of regulation every time a new worst-case scenario occurs. You perform reasonable risk assessments and regulate as a result of their findings when necessary. It's actually not very hard.