This space intentionally not left blank

 

Mercy killing

17 Sep 2009  around lunchtime  Matt Winckler

This blog is the horse with three broken legs and a wheezing cough. It is the dog with three metastasized cancerous tumors the size of grapefruits and open sores all over. It is…no, that’s enough.

This is a grisly notice to the two of you still subscribed to this blog that you may now join me in removing it from your RSS reader. I’ll leave the software up a while until I get some reasonable redirects worked out for the more popular and useful technical entries, but this is The Last Post. Perhaps (nay, likely) someday when I get spare time I’ll start afresh with a re-imagined online presence. Until then, re-imagine it on your own.

I could write something sentimental here, but I’m not like that.

*BLAM*

Code Magic

21 Aug 2009  around lunchtime  Matt Winckler

Magic is bad. There should not be anything magical in production code, because by definition, magic is something not understood. I’m on board with this notion.

But what happens when magic is twice as fast (or faster) than normal code? Fast magic is a temptation difficult to resist, particularly when the performance of the normal code is not acceptable to begin with.

And so it is that I have done something I thought I never would: I have implemented the Y combinator, in a useful fashion(!), in a C# project I’m working on. I used to think Y combinator had no place in C#, because the language itself supports recursion. However, LINQ doesn’t, and I don’t know of another way to employ it generically short of using Y.

I don’t understand all the ins and outs and mathematical equations and untyped lambda calculus on that Wikipedia page (in fact, I don’t understand any of them. Side question: do math-related Wikipedia pages make sense to anybody, including mathematicians?). But I do understand that after trying to understand the real-world mechanics and looking at an example or two, I was able to implement a function of my own that delves through a TreeView and gets all the TreeNodes that are checked. And, depending on how many nodes there are and how many are checked, this function is at least twice as fast as the conventional way of doing it, a speedup I attribute mostly (if not entirely) to LINQ’s deferred execution.

public static Func<A, R> Y<A, R>(Func<Func<A, R>, Func<A, R>> f) {
    Func<A, R> g = null;
    g = f(a => g(a));
    return g;
}

/// <summary>
/// Gets all checked TreeNodes in the specified TreeView.
/// </summary>
/// <param name="treeview"></param>
/// <returns></returns>
public static IEnumerable<TreeNode> GetCheckedNodes(this TreeView treeview) {
    var getNodes = Y<TreeNode, IEnumerable<TreeNode>>(
        f => n => new[] {n}.Concat(
            (from TreeNode node in n.Nodes select node).SelectMany(f)
        ));
    return (from TreeNode N in treeview.Nodes select N)
            .SelectMany(getNodes).Where(n => n.Checked);
}

(Yes…I desperately need to get a syntax highlighting plugin here. It’s on the todo list along with the site revamp. Copy the code into Visual Studio and pretend it wasn’t a hassle.)

Plus, the magic gave me a tingly sensation, which changed from frightening to slightly pleasant once I decided that this fixed-point combinator untyped lambda calculus business wasn’t going to open a portal to some other dimension and let a bunch of aliens through to take over the world. (I thought I saw the wall behind my monitors starting to distort into a torus shape, but after rubbing my eyes the effect disappeared.)

I still profess to be a hack and a neophyte at this magic, so gurus are invited to point me to a better way of accomplishing this without resorting to Y combinator magic. Though I do fear that tingly sensation may be habit-forming.

Why hackers are often libertarians

04 Aug 2009  in the early morning  Matt Winckler

Code Free or Die() is an interesting short essay that attempts to explain why hackers are very often also libertarians. It is worth reading even if only to admire the author’s correct usage of the phrase “begs the question”, which occurrence is a rare thing of beauty indeed in today’s illiterate society. A few choice excerpts from the essay follow.

All the economic theory that goes along with libertarianism is a side show. Libertarianism isn’t about what makes us rich; it’s about what’s right. “We hold these truths to be self-evident” and all that. Libertarians don’t oppose big government because it’s clumsy or wasteful. They oppose it because it’s evil.

[...] I propose that we’ve been looking at the problem from the wrong angle. It isn’t that hackers tend to adopt libertarian politics, nor is there any third factor that influences both. Rather, people with a naturally anti-authoritarian attitude tend to become attracted to programming.

[...] People who have no interest in understanding computers are entirely accustomed to getting bossed around by them; it just seems like a fact of life. [...] But to those of libertarian temperament, this is an unacceptable state of affairs. Getting bossed around by government is bad enough. But getting bossed around by an inanimate object? Simply intolerable.

Count source code lines with grep

31 Jul 2009  in the early evening  Matt Winckler

Here’s a handy way to count lines in C# source files using grep and wc, excluding (most) comments and blank lines:

grep -Pr --include=*.cs "^.+$" TargetDirectory | grep -v "^\s*//" | wc -l

Note, however, that this expression still counts comment blocks enclosed in /* */…my grep-fu (and level of caring) evaporated once I recognized that fact, and the expression above got me close enough. If you’re using Visual Studio and have that option turned on that prefixes each extended comment line with an asterisk, you could modify the second expression slightly to become ^(\s*//)|(\s*\*) and that should grab them.

Based on this calculation, I estimate that I’ve written about 60 lines per hour on a side project I’ve been working on this spring/summer. I’m not sure whether that’s good, bad, or (most likely) meaningless, which is about par for the course as far as statistics are concerned.

Why don’t I just take care of that, then

21 Jul 2009  in the early morning  Matt Winckler

I wish I had known about this (if it existed) three years ago. Better yet, I wish I’d invented it! As with all good ideas, it is both simple and beautiful. Now, at last, I have an appropriate way to answer the people who come to me asking inane questions before conducting even the most rudimentary Google search.

How do you publish a website in VB.NET, anyway?