Bubble, Bubble, Toil and Trouble
Mar. 6th, 2007 10:47 pmSometimes, what I do feels like incantation. I wanted a word count on my "working directory" displays, which I maintain using python scripts. Once upon a time, this would have taken me a few lines. Maybe ten or so. And then I learned Ruby. I learned the difference between a fold, a filter, and a transform. And I learned when to do which.
def wc(f): return Numeric.sum([len(i.split()) for i in open(f, "r") if i])
I don't get paid enough for knowing this stuff.
def wc(f): return Numeric.sum([len(i.split()) for i in open(f, "r") if i])
I don't get paid enough for knowing this stuff.
no subject
Date: 2007-03-07 05:36 pm (UTC)You're right that the count is broken because of the 'if i' chunk. It doesn't do what I intended it to do (remove nulls caused by splits across multiple whitespaces).
And yes, there are generators in python and they completely rock. In fact, I've written Python-to-HTML and Python-to-JSON that silently swallow generators and functors, calling them directly and further interpreting their output. It's damn near DWIM code.