Kludge Everywhere

July 22, 2008

Despite nearly 400 years of scientific revolution, Biology has been unable to deliver on crucial problems like effective cures for viral infections or cancer. Some of our best progress, like antibiotics, has been due to chance and random experimentation. You start a clinical trial for a hypertension drug and suddenly - whoah - all your subjects have hard-ons! Viagra is born. To be sure, chance plays a role in all endeavours, but Physics and Chemistry have a comprehensive theoretical basis powering systematic improvements, whereas Biology has been largely confined to kludges. Wanna treat cancer? Here, blast the patient with radiation and poison and hopefully the cancer will die first. They’re brilliant kludges, and I’m happy to have them, but it’s a far cry from the precision we’ve had elsewhere.

That’s from an excellent essay by Gustavo Duarte.

Nice concise definitions of closures, map, fold, unfold and filter sneaked their way into the middle of a Dr. Dobbs article (via Raganwald):

Closures, are anonymous functions created at run-time which can refer to variables that are visible where the function is declared.

Closures are especially useful when performing operations over elements in a collection. The most common collection operations (aggregate operations) in functional programming are map, filterfold, and unfold operations.

A map operation transforms a list into a new same-sized list by applying a transformation function (such as a closure) to each element in the original list. A common example of a map operation is when performing a vector scaling operation.

A filter operation creates a list from another list using only those items for which a predicate function returns true.

The fold operation combines values in a list using a binary function and an initial value. A summation function is a simple example of a fold operation.

The unfold operation creates a list from an initial value and by successively applying a generator function, until a terimination predicate returns true.

These are usually defined in much more obfuscated terms. It’s good to see them so clearly defined.

Last Day of the Month

June 15, 2008

When a blog has never been visited my dissertation software retrieves any posts from the past 24 hours. This isn’t hard:

from datetime import datetime
lastvisit = datetime.utcnow().replace(day=(datetime.utcnow().day-1))

But when this happens and it’s the first day of the month you get:
ValueError: day is out of range for month
Since my project is only going to run for this summer, I did changed it so that it will work this year but not next year (sorry, WordPress strips out all of the tabs…):
from datetime import datetime

n = datetime.utcnow()
if n.day != 1:
    lastvisit = datetime.utcnow().replace(day=(datetime.utcnow().day-1))
elif n.month - 1 in (1, 3, 5, 7, 8, 10, 12):
    lastvisit = datetime.utcnow().replace(month=(n.month-1), day=31)
elif n.month - 1 in (4, 6, 9, 11):
    lastvisit = datetime.utcnow().replace(month=(n.month-1), day=30)
elif n.month - 1 == 2:
    try:
        lastvisit = datetime.utcnow().replace(month=(n.month-1), day=29)
    except ValueError:
        lastvisit = datetime.utcnow().replace(month=(n.month-1), day=2 8)


Today I ran across this article on ASPN. And now here’s what I would write (omitting some of the boilerplate from above):

if n.day == 1:
    days_in_month = [calendar.monthrange(year,month)[1] for year in [n.year] for month in range(1,13)]
    datetime.utcnow().replace(day=days_in_month[n.month-2], month=n.month-1)
elif:
    datetime.utcnow().replace(day=n.day-1)


If I would have spent 5 minutes looking for The Right WayTM to do this, I would have saved a little time and had cleaner code. Now I don’t want to change what I’ve got working well enough.

Dear Lazyweb: How do I post pretty code snippets?

Random Data

June 15, 2008

In the last 3 and a bit days my software has gathered over 125,288 posts from 96,000 blogs. 37,594 blogs have at least one post from that period.

Below’s a crappy table ripped right from the mysql console. The first column is the total number of posts and the second column is the number of sites that have the corresponding number of posts.

+—————–+—————–+
| number of posts | number of sites |
+—————–+—————–+
|             100 |               1 |
|              89 |               1 |
|              87 |               1 |
|              83 |               1 |
|              79 |               2 |
|              75 |              12 |
|              74 |               1 |
|              72 |               1 |
|              70 |               1 |
|              69 |               1 |
|              68 |               3 |
|              66 |               1 |
|              65 |               3 |
|              63 |               3 |
|              62 |               2 |
|              61 |               1 |
|              60 |               3 |
|              59 |               2 |
|              58 |               4 |
|              57 |               1 |
|              56 |               2 |
|              55 |               2 |
|              54 |               2 |
|              53 |               4 |
|              52 |               8 |
|              51 |               2 |
|              50 |               2 |
|              49 |               3 |
|              48 |               6 |
|              47 |               4 |
|              46 |               2 |
|              45 |               3 |
|              44 |               4 |
|              43 |               8 |
|              42 |               9 |
|              41 |               8 |
|              40 |              10 |
|              39 |               5 |
|              38 |               7 |
|              37 |              10 |
|              36 |              10 |
|              35 |              12 |
|              34 |               7 |
|              33 |               7 |
|              32 |              12 |
|              31 |               9 |
|              30 |              16 |
|              29 |              19 |
|              28 |              16 |
|              27 |              32 |
|              26 |              27 |
|              25 |              43 |
|              24 |              28 |
|              23 |              45 |
|              22 |              45 |
|              21 |              49 |
|              20 |              66 |
|              19 |              69 |
|              18 |              78 |
|              17 |              71 |
|              16 |             116 |
|              15 |             106 |
|              14 |             140 |
|              13 |             166 |
|              12 |             184 |
|              11 |             259 |
|              10 |             312 |
|               9 |             410 |
|               8 |             548 |
|               7 |             791 |
|               6 |            1093 |
|               5 |            1699 |
|               4 |            2959 |
|               3 |            5139 |
|               2 |            8170 |
|               1 |           14695 |
+—————–+—————–+

Wanna know why?

Because every time I want to write I have to log into this big flashy website, when all I want to post is a little text and a few links. I don’t need tags. I don’t need categories. I don’t need previews. I don’t need rich editors and statistics.

Tinderbox

May 15, 2008

I’ve finally been pushed over the line from my position that Tinderbox is neat, but I probably wouldn’t use enough of its features to justify purchasing it. Reading Mark Bernstein’s recent post about it completely changed my mind.

Go read that post.

Amazing isn’t it? I do that sort of thing all of the time, enduring much more pain than Mark went through.

My purchase will have to wait until I have the money, probably some not-so-insignificant time after I get out of graduate school in September. Unless, that is, I dip into my next financial aid check (No, I’m not really going to do that. Settle down, Mom.).

Note: Yes, I’m aware that there’s a trial version; however, I’m not a fan of becoming addicted to a piece of software to be thwarted when it comes to actually buying a license, so I’ll just wait.

Blown Stack

April 18, 2008

Joel Spolsky and Jeff Atwood are working on a new site. I think they’ve got a great idea there.

What they should stop doing immediately is posting their phone conversations as a podcast. I tried listening to the first one and all I can say is no thanks. Please stop, now.

Nobody wants to listen to a boring conversation topped with poor sound quality.

Couldn’t Sleep…

April 9, 2008

… so I wrote most of a simple trivial pursuit game in Ruby. It’s very basic, and I don’t have any real questions for it, and I’m not doing the “pie” scoring so it’s really just a trivia game with a Trivial Pursuit shaped board.

It was a fun waste of time but I would have preferred sleep.

(No I won’t post the code. It’s throw-away crap to help me nail some more Ruby syntax into my brain.)

Another Neat Command

April 8, 2008

I was signing up for github and they wanted my SSH public key. In their instructions for how to get and generate the key they have you run a command like this:

  • cat id_rsa.pub | pbcopy

pbcopy is a neat Mac utility that takes it’s input and copies it to the clipboard.

So handy.

I Can Haz del.icio.us

March 16, 2008

I don’t do much with bookmarks after I make them in del.icio.us. What I would like is software that would let me enter tags to watch, and then aggregate the bookmarks that  match that tag. I would then be able to select the category of bookmars (Popular, All, Mine) I want to track.

If I had this, I would do so much more with bookmarks from del.icio.us. It should be fairly simple to write an aggregator with Planet Python and serve it with web.py. Maybe I can get around to doing that at some point.