More Simple Stats with Perl 6

Last time, a time long, long ago, in a universe far, far away, I left you with the following Perl 6 code (or something like it). This code reads in a series of book heights and displays the number of books of each height (in order from shortest to tallest).

my %num_of_height;
for lines() {
    if m/^ \s* (\d+[\.\d+]?) [\s* x \s* (\d+)]? \s* $/ {
        my $height = +$0;
        my $num = +($1 || 1);
        %num_of_height{$height} += $num;
    } elsif ! m/^\s*$/ {
        note "Invalid line ignored: $_";
    }
}

#| unique book heights seen, in order from shortest to tallest
my @heights = sort { $^a <=> $^b }, keys %num_of_height;

say "Histogram data:";
for @heights -> $height {
    my $num = %num_of_height{$height};
    say "$height x $num";
}

As you may recall, this little bit of somewhat less-than-useful P6 script was inspired by a homework assignment in my daughter’s high-school statistics course. While she manually counted up the heights of the books we measured, in order to draw a histogram of the data, I wrote a Perl script to do the same counting.

Since then, for kicks, I’ve also hacked together about 50 lines of code to display an actual histogram of these data, with horizontal bars of *****. I’m not going to go into that code here, because I did not write it at the time, but if you want, you can check it out on Github.

However, I did discover how succinctly Perl 6 could answer the other questions from the assignment. Continue reading

Posted in Uncategorized | Tagged , , | 4 Comments

Reason #2 Why We Write Unit Tests

Seen on DamnLOL

Ninety-nine little bugs in the code,
Ninety-nine little bugs!
You take one down, patch it around,
A hundred and seventeen bugs in the code!

(See also Twelve Benefits of Writing Unit Tests First.)

Posted in Uncategorized | Tagged , , , | Leave a comment

Some Simple Stats with Perl 6

When my daughter told me she was taking statistics, I wondered how she could do that without knowing any calculus. Then on second thought, I guess there is a certain amount you can do with statistics, even without knowing any calculus.

Ah, yes. And last night’s homework assignment drilled that home. She asked me to help her. First, she needed to find a quantitative data set she could use to construct a histogram. Looking around the living room, I suggested, “What about the heights of the books on my book shelves? You could measure the spines and write them down in a data table.”

Now, I currently have nine full shelves of books in my living room, everything from Dave Barry and Dilbert to Peopleware to marketing and memoirs, theology and psychology, short stories and the theory of writing, to Holly Lisle, Douglas Adams, Robert Heinlein, and The Sisterhood of the Traveling Pants. (And I am not making this up.) And those are just the books that are actually on shelves, not the books that are still in boxes.

I think she was a little overwhelmed by the idea.

“You don’t have to include all the books, just one or two shelves worth. Maybe that one over there, half full of mass-market paperbacks all the same size.” Easy to measure, easy to count.

So we chose that shelf and the shelf just under it, a nice cross-section of fiction in mass-market, trade paperback, and hardcover. And then while she was manually collating the data, inspiration struck me. Why don’t I write a quick Perl 6 script to check her results. It’ll give me something to write about; plus I’ll see whether Perl 6 is really useful for anything, especially minor scripts.

And as it turns out, it might be. Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

How Fast (or Slow) Is Perl 6?

Racing at the 2009 Camel Cup in Alice Springs, Australia
Photo © 2009 Toby Hudson CC BY-SA

In my previous post, I created a short, simple, sweet, and très élégant Perl 6 function to find all the primes up to a given maximum.

Unfortunately, Rakudo spent over 11 seconds to find the 168 primes up to 1000.

Now, granted, this algorithm is exceedingly naïve. It divides every candidate integer by every prime that came before it, resulting in some 91,873 trial divisions (and another 91,873 boolean tests). Even so, that boils down to some 280,000 CPU clock cycles per inner loop (on my old 2.33 GHz Intel Core 2 Duo, with one core tied behind its back).

As a comparison, an implementation of the same algorithm in Perl 5 took only 44 milliseconds to find the same 168 primes using the same number of loops, or about 1100 CPU clock cycles per loop. Still not blazing fast, but more like what I would expect from an interpreted language… Of course I have Rakudo running on the JVM, and I’m benchmarking it after the JIT compiler has squeezed every last little bit of performance it can out of the code it’s given. A native-Java implementation ran even faster— but I’m getting ahead of myself. Continue reading

Posted in Uncategorized | Tagged , , , | 5 Comments

Perl 6 and the Price of Elegant Code

The project that first made me fall in love with Perl…

Perl 5 had recently launched, with new features for object-oriented programming, includable modules, lexically scoped variables, and references—including closures. At the time, I was writing boot code, diagnostics, and production test software for Kurzweil-brand electronic musical gear. Part of the challenge of doing embedded systems back then was getting the machine code into the actual machine. The C compiler, assembler, and linker generated a binary file, which needed to be physically programmed into multiple EPROM chips in order to run the code on our custom hardware. We actually had software tools to pad out, checksum, and split up the binary data so it could programmed into multiple EPROMs according to our specifications.

And then we started designing systems that exceeded the capabilities of our tools. And I had to hack together custom C programs to reformat the binary data according to the new requirements. I think I might have done that twice. And then I asked myself whether Perl, which was always very good with strings of data—and what is a binary file but a big string of data? I asked myself whether Perl might be able to do a better job. Continue reading

Posted in Uncategorized | Tagged , , , , | 3 Comments

Perl 6’s Top 3 Coolest Features

Perl 6 has some really cool features. Features I’d like to use in production projects. But is Perl 6 suitable for production?

I keep hearing, no, because it’s unstable, the spec is still being hammered out, there are still too many bugs, it’s still too slow, the documentation lacks (or is just plain wrong), key features are still missing, it has insufficient module support.

But I have never been one who blindly accepts the evaluations of the crowd, because the crowd is often dominated by zealots twisted by hate and zealots twisted by love, and I’ve found a great beauty and symmetry in that nuance that lies in between zealotries.

Besides which, Perl 6 (hereinafter referred to as “P6”) includes a number of features that fit a particular real-world application that I happen to be thinking about. That’s another post. But several P6 features struck me. Here are the top three: Continue reading

Posted in Uncategorized | Tagged | 2 Comments

This Site Is Such a Hack

Hacking in a suite at clarionPhoto © 2010 Johan Nilsson CC BY-NC 2.0

Hacking in a suite at clarion
Photo © 2010 Johan Nilsson CC BY-NC 2.0

I’ve been wanting to set up a software-development blog for some time. And for some time I’ve been wrestling with the pains of managing multiple WordPress blogs. Back in the day, when I had just my writing blog and a LiveJournal, it wasn’t such a big deal. Since then, I’ve retired the LiveJournal—sorry, LJ friends—and have set up a number of other WordPress sites, for different niches in which I write. In particular, I have a personal blog, a political blog (because I decided I should not pollute my personal blog with political posts), and now a software-development blog (which you are currently reading).

When I spun off the political blog, I got a taste of what it meant to migrate posts from one WordPress instance to another. And my experience with this SD blog was ten times worse, because there were 10 times as many posts involved.

The short version of the story: WordPress is broken, and it will always be broken.

The long version of the story… is a little more of an acerbic joke. Continue reading

Posted in Uncategorized | Tagged , , | 1 Comment

State-of-the-Art Computer Folklore (part 5)

Photo © 2011 Giuseppe Savo CC BY-NC-ND 2.0

This is part 5 in my series of how the Mac reminded me why I fell in love with software development, and why it still matters.

While reading Andy Hertzfeld’s anecdotes (and those of his colleagues) of designing the original Macintosh computer, I was inspired, inspired to take account of my own passions, the passions that these stories reminded me of. Today, I continue that list:

  • I love to create new patterns. I love solving problems through discovery, inventing that which has never existed before. (From part 1.)

  • I love applying principles in new ways. I love working with abstractions, and turning them into concrete expression. I love challenging the status quo, breaking through the limits of what everyone else says is “possible.” (From part 2.)

  • I love achieving status through collaboration, which is compassionate conflict. I am not a baboon. I do not achieve a sense of status by beating up (literally or figuratively) on my colleagues and friends. But I do expect to be recognized for the ideas I bring to the table, and I want to be taken seriously. (From part 3.)

  • I love moving in the right direction. I believe in making the world a better place, one step at a time, and that helps fulfill my need for purpose. Whatever I work on, it’s more than the work itself; it’s also how that work changes the bigger picture. (From part 4.)

  • I love taking time to think, time to learn. A relatively small portion of my day is normally spent actively producing output, whether writing or developing software.

Continue reading

Posted in Uncategorized | Tagged , , , , , | Leave a comment

State-of-the-Art Computer Folklore (part 4)

“I want to believe.”
Photo © 2008 Sunny Ripert CC BY-SA 2.0

This is part 4 in my series of how the Mac reminded me why I fell in love with software development, and why it still matters.

While reading Andy Hertzfeld’s anecdotes (and those of his colleagues) of designing the original Macintosh computer, I was inspired, inspired to take account of my own passions, the passions that these stories reminded me of. Today, I continue that list:

  • I love to create new patterns. I love solving problems through discovery, inventing that which has never existed before. (From part 1.)

  • I love applying principles in new ways. I love working with abstractions, and turning them into concrete expression. I love challenging the status quo, breaking through the limits of what everyone else says is “possible.” (From part 2.)

  • I love achieving status through collaboration, which is compassionate conflict. I am not a baboon. I do not achieve a sense of status by beating up (literally or figuratively) on my colleagues and friends. But I do expect to be recognized for the ideas I bring to the table, and I want to be taken seriously. (From part 3.)

  • I love moving in the right direction. I believe in making the world a better place, one step at a time, and that helps fulfill my need for purpose. Whatever I work on, it’s more than the work itself; it’s also how that work changes the bigger picture.

Continue reading

Posted in Uncategorized | Tagged , , , | 1 Comment

State-of-the-Art Computer Folklore (part 3)

This is part 3 in my series of how the Mac reminded me why I fell in love with software development, and why it still matters.

While reading Andy Hertzfeld’s anecdotes (and those of his colleagues) of designing the original Macintosh computer, I was inspired, inspired to take account of my own passions, the passions that these stories reminded me of. Today, I continue that list:

“The Team Pair-programming”
Photo © 2005 Michael Caroe Andersen CC BY-NC 2.0

  • I love to create new patterns. I love solving problems through discovery, inventing that which has never existed before. (From part 1.)

  • I love applying principles in new ways. I love working with abstractions, and turning them into concrete expression. I love challenging the status quo, breaking through the limits of what everyone else says is “possible.” (From part 2.)

  • I love achieving status through collaboration, which is compassionate conflict. I am not a baboon. I do not achieve a sense of status by beating up (literally or figuratively) on my colleagues and friends. But I do expect to be recognized for the ideas I bring to the table, and I want to be taken seriously.

Continue reading

Posted in Uncategorized | Tagged , , , , , , | Leave a comment