Does Java really lack a sum() function?

slaniel | Java | Wednesday, December 30th, 2009

I’m writing a lot of code that needs to sum over reasonably long collections. Somewhere on the things-that-you-just-shouldn’t-have-to-write checklist, right next to “a date-parsing library”, is “a function to sum over collections.” The naïve way to do it is simply

public double sum(List<Number> numbers) {
    double retSum = 0;
    for(Number i : numbers) {
        retSum += i.doubleValue();
    }
    return retSum;
}

At one level, this code is annoying because it’s not generic enough. You’d like to be able to add complex numbers, for instance. Or matrices. Anywhere that ‘+’ is sufficiently “number-like,” you want this function to work. You wouldn’t really want it to work with, say, strings, even though Java overloads ‘+’ for them. Or maybe you would? Essentially, maybe you want to duck-type: if ‘+’ makes any kind of sense for the objects, allow them to be summed. In any case, I want sum() to be a function that takes a Collection of objects of some type T and returns another T.

What’s ickier about this code is that there are many more elegant ways to write it, at least in other languages. A language which supports first-class functions (see Joel Spolsky’s excellent Can Your Programming Language Do This?, whence the remarkable Java-as-kingdom-of-nouns parable) would allow you to define the wonderful map() and reduce() abstractions, whence (to use Python as an example) addition becomes just one use for reduce():

def sum(inList):
    """
    Return the sum of the items in inList.
    """
    return reduce( lambda x,y: x+y, inList )

One great virtue of map() and reduce() is that they’re highly parallelizable: the sauce that makes Google tick involves breaking many common tasks into a map() step and a reduce() step, then farming those separate steps out across thousands of machines.

My friend Ken Shan, by the way, pointed me to an absolutely wonderful talk by Guy Steele on the need for more parallel style in modern programming, now that we’ve entered the age of multicore machines. Whenever I see any sequential programming, like our sum() function above, I look for some way to turn it into a balanced tree: divide the list up into two sublists, for instance, and then sum the sublists; repeat this recursively until you’re down to the sum of zero- or one-item sublists, which is trivial to compute. Yes, I could write this now in Java, but I’d like a more general construct that would allow a great many functions — apart from sum() — to be likewise parallelized.

First-class functions make those sorts of abstractions easy, but I wonder whether they could be accomplished in Java with a bit more work. Perhaps it can be done with a functor? I know not about functors. This is perhaps something to research.

The broader point is just that it’s very silly not to already have a sum() function written for me — thoroughly generalized, well-tested, and highly optimized.

Performance debugging OS X?

slaniel | OS X | Monday, December 28th, 2009

I only recently started using OS X (starting to write iPhone apps once I get my feet fully under me at work), so I’ve not yet touched on higher-level tasks like performance debugging. It seems as though I need to start on this, because the performance of this laptop — MacBook Pro, 2.66-GHz dual core, 4 gigs of RAM — is becoming unacceptable. Most of the time it’s fine, but very often it hangs for a couple seconds in the middle of whatever I’m doing. At that point I get the spinning OS X beach umbrella of death. Eventually it comes back. But those delays are really annoying when you’re in the middle of, say, watching a YouTube video.

I have a small array of standard techniques at my disposal to debug this sort of problem. The main one would be “Start with no applications open and keep opening apps until the problem appears.” Does anyone know of a faster way to zoom in on whatever is causing my system to hang at those moments? Profiling tools, etc.?

Sure, Reid and Obama may be gutless, but have some perspective

A lot of people have been calling Obama and Harry Reid gutless for the compromises they’ve struck on the way to health reform. Yes, those compromises suck, and many of us would prefer single payer. Short of single payer, we’d prefer a robust public option. Short of a robust public option, we’d prefer a weak public option run by the states. We didn’t get any of those. So that’s galling. What we did get is coverage for 30 million people who couldn’t afford it before, and a deficit reduction over the next ten years. That’s nothing to sneeze at.

Blaming Obama and Reid is rather short-sighted. Look back at the last Democratic attempt at health reform under Clinton. It failed utterly. If, as looks likely, the Senate health plan gets signed into law, it will be the first major progressive piece of legislation since Lyndon Johnson. If you were a betting man and had been asked to put money on health reform back in 2008, you should have bet against it or bet only a tiny amount in its favor.

“But what about Obama’s overwhelming electoral victory in 2008?” you might ask. Well, what of it? “The reigning ideology had been roundly discredited, we were in the middle of the worst recession since the Great Depression, and still he couldn’t do what Roosevelt did?” Right. Ronald Reagan couldn’t get all of what he wanted in the 1980s, either, even though the reigning welfare-state ideology had been roundly discredited and we were in the middle of “stagflation”. Reagan tried and failed to dismantle Social Security. He got a huge increase in the military’s budget, certainly. But the actual remnants of the Reagan Revolution, if I’m not mistaken, are just a general distrust of the welfare state. That may well have been with us anyway.

“But what about Bush?” Bush got a lot more than we wanted: he got the PATRIOT Act, he got to torture whomever he pleased, he got a massive tax cut for the wealthy, he got to invent a war, and he pushed our society further along the road to a police state. But he also failed to privatize Social Security, even after being re-elected. And I’m willing to put war into another category: Things Presidents Seem To Be Able To Get Whenever They Want Them.

I take two conclusions from these observations:

  1. It’s hard to get root-and-branch change, and it gets harder as time goes on. The Politics Presidents Make argues this in greater detail.

  2. Progressive legislation is really rare.

If you believe that progressive legislation is rare in no small part because liberals are wimps, it’s worth asking why. Why have we not had a ballbuster since LBJ? And why was even LBJ, the ballbustingest president of them all, only able to secure insurance coverage for the elderly when what he wanted was insurance coverage for all Americans? Why have we been fighting the universal-coverage battle since at least Truman? I submit that there are structural reasons why we shouldn’t expect major progressive legislation in this country.

The trouble is that we’re here on the ground, watching each new detail every day, rejoicing in the gains (the House bill has a public option! Obama delivered a great address to Congress that roused the troops!) and rending our garments with each new loss (it’s a crazy summer filled with “death panels”; Joe Lieberman is holding everyone hostage). It was like this during the presidential campaign, too: Oh no, Reverend Wright is going to kill the Obama campaign! But no, he salvaged it with The Speech On Race! Sarah Palin looks like the death knell for the Obama campaign! But no, she’s a flash in the pan!

The 24-hour news cycle demands that we constantly be fed excitement. Yet much of what I’ve read suggests that the fundamentals are set well in advance. Look at presidential campaigns: any number of books — Unequal Democracy, say — make clear that the unemployment rate and the GDP in the year leading up to an election are the prime drivers of who wins: if we’re in a recession, the reigning party is likely to be kicked out; if the economy is expanding, the incumbent wins.

Likewise with health care. We all should have known from the beginning that the Senate is an intensely conservative body with intensely conservative institutions like the filibuster. It is heavily weighted against change. Part of me wants to say that it’s weighted against progressive change, whereas a PATRIOT Act is easier to pass. But I can’t really argue that to myself convincingly, and besides: a 9/11 doesn’t happen every day; when it does, the president will get whatever he wants.

Once again I find myself ignoring a principle that I should have been following all along: the less we pay attention to the news, the more likely we are to be calm in the face of joys and sorrows. The health debate was mostly decided long ago by the structure of the U.S. government; whether Obama gives a speech, or whether people bark about “death panels,” is unlikely to shake those fundamentals. We get the government we’ve always had.

Health-care reform: it isn’t about you

slaniel | Health care and insurance; Helping the Less Fortunate | Sunday, December 20th, 2009

Paul Krugman’s latest piece about health reform says something that’s been nagging at me for a while in a less-clear form:

But back to Obama: the important thing to bear in mind is that this isn’t about him; and, equally important, it isn’t about you. If you’ve fallen out of love with a politician, well, so what? You should just keep working for the things you believe in.

It isn’t about you. It’s about providing health coverage for the poor. It’s about restoring some of our tattered safety net, because capitalism — for all the innovating it does — can leave people destitute for macroeconomic reasons beyond their control. It’s the job of a civilized society to protect its citizens against forces beyond their control.

Liberals — of whom I am unabashedly one — have made health reform a vanity exercise of late. As long as people have focused on the public option, they have taken their eye off the ball: protecting the poor. Indeed, even if the public option were part of the bill, we’d still need to resolve a lot of questions about, for instance, how much the poor would be expected to pay out of pocket (for a public plan or a private one). These questions don’t just disappear when the government offers a health plan to compete with private insurers.

So really: progressives, it’s not about you. It’s about spreading coverage to America’s neediest, and it’s about controlling the cost of health care. Focus on those two things. Focus on ends rather than means. The public option was always a means, but there are other ways to achieve the same ends. Progressives have far too easily proposed sacrificing the ends in pursuit of the means.

Great health-reform advice from Matt Yglesias

slaniel | Health care and insurance; Helping the Less Fortunate | Wednesday, December 16th, 2009

If you want to know what to do about [health-care reform], read the last paragraph of “Politics as a Vocation”.

(via Yglesias on Twitter)

Well hey, I can use The Google! Heeeere’s the appropriate paragraph from Weber:

Politics is a strong and slow boring of hard boards. It takes both passion and perspective. Certainly all historical experience confirms the truth –that man would not have attained the possible unless time and again he had reached out for the impossible. But to do that a man must be a leader, and not only a leader but a hero as well, in a very sober sense of the word. And even those who are neither leaders nor heroes must arm themselves with that steadfastness of heart which can brave even the crumbling of all hopes. This is necessary right now, or else men will not be able to attain even that which is possible today. Only he has the calling for politics who is sure that he shall not crumble when the world from his point of view is too stupid or too base for what he wants to offer. Only he who in the face of all this can say ‘In spite of all!’ has the calling for politics.

How many syllables can you pack into a beat?

slaniel | Music | Tuesday, December 15th, 2009

I was just thinking about a great line from a Roots song:

Backstage whisperin’ to management, like
“Change the order, it’s no way that we can rock after them.”
My man sport the ‘fro like What's Happenin'?

In that last line, you could rap any number of people in the place of “My man”: 1 syllable

Dre sport the ‘fro like What's Happenin'?

or 2 syllables, like The Roots sing it, or 3 syllables

Steve Laniel sport the ‘fro like What's Happenin'?

or even four

Gary Coleman sport the ‘fro like What's Happenin'?

Presumably there is some limit past which you just can’t pack any more syllables in there. But what’s the limit? And what determines it?

Mourning Paul Samuelson: we shall not look upon his like again

slaniel | Samuelson, Paul | Tuesday, December 15th, 2009

Paul Samuelson’s death has saddened me a lot more than I expected it to, and I had already expected it to. I had been trying to think of some way to meet the great man during his life, knowing that he was an old fellow and that his days weren’t likely to be long. I guess I missed out.

Samuelson essentially defined economics for the last 50 years. His contributions are all over the map, and are deep in every realm that they touched. I couldn’t do a better job than Paul Krugman, or than The New York Times’s obituary, so I won’t try.

The thing I want to point out about Samuelson is what a good mathematician he was. I mean something specific here: Samuelson’s mathematical economics — he was the father of mathematical economics — knew exactly when to stop. His conclusions never went further than his theorems allowed him to go. Indeed, his theorems often define the limitations on the standard economic view — as in his classic public goods paper. As Krugman puts it:

4. Public goods: Why must some goods and services be provided by the government? What makes some, but only some, goods suitable for private markets? It all goes back to Samuelson’s 1954 “Pure theory of public expenditure”.

Too often, economists — particularly anarcho-capitalist libertarians — are failed mathematicians. “If A is true, and B is true, then clearly C is true.” But you’ve not stated the assumptions on A and B clearly enough; if you tease apart exactly what you’re saying, you’ll see that A and B are only true under certain conditions that are never satisfied in practice.

Economics has taken a beating recently, and justifiably so: the assumptions underlying much of modern finance were horribly broken. Whenever I read about finance, I always expect Samuelson to receive a bit of the beating as well; he did, after all, establish one of the fundamental results of modern finance, namely that prices fluctuate randomly under certain conditions. But whenever it looks like Samuelson’s reputation will be tarred by a broad brush coating his fellow economists, he escapes cleanly — precisely because he’s a good mathematician who makes only the assumptions that he’s allowed to make, and therefore only draws the conclusions he’s entitled to draw.

Mathematics isn’t a game that allows you to derive absurd conclusions, the more absurd the more entertaining; it’s a tool for getting at the truth. Samuelson was the pre-eminent mathematical economist. I’m really going to miss him.

Discovery of the day: scriptable mutt

slaniel | mutt | Friday, December 4th, 2009

I’ve known for a while that you could script mutt. What I didn’t know was that if you want to script, you need to hand in something on stdin. If you try to do it like so

mutt -i /path/to/message_body -s "some subject line" steve@stevereads.com

, mutt will prompt you for the information you’ve already submitted. Whereas if you do

echo '' | mutt -i /path/to/message_body -s "some subject line" steve@stevereads.com

it will fire off an email with no prompting — which is what you want in a script.

self.lesson.setLearned();

Bad Behavior has blocked 819 access attempts in the last 7 days.