Tuesday, February 24, 2009

Source Code Highlighting in Blogger

... is something Google don't have a good solution for right now and is a huge source of pain for me and a lot of other bloggers, judging by the large quantity of posts related to the subject. Hopefully it's something Google can provide in the near future.


Message for Blogger Labs.


In the meantime, I'm using the solution posted by Gilad Naor, which uses the "TOhtml" feature found in Vim. It's not a bad solution, and I've found that putting "let html_use_css = 1" in your .vimrc tells Vim to output CSS (yeah baby!), which makes everything easier on the eye and more maintainable.

Huge thanks to Gilad for being the first person to alert me to the possibility of using everyone's favourite terminal editor in this way, and I would've posted to thank him, except Blogger's word verification isn't working. :(

Monday, February 23, 2009

List Comprehensions

I love them. They're just so Pythonic.

What they do is provide you a succinct way to describe a list. You know, our favourite little squiggly snakes with [] at the ends. If you haven't the foggiest what a list comprehension is (and you'd be missing out on a lot of fun), here's a little example to get your juices flowing. Let's say, just for argument's sake, you want to represent the following mathematical expression in your program:

M = {x| x in S and x odd}

How would you do that in Java? You would likely go:

ArrayList M = new ArrayList();

for (int x: S) {
  if (x % 2 == 1)
    M.add(x);
}

How utterly clumsy and unPythonic. In Python you'd just do this:

M = [x for x in S if x % 2 == 1]

So easily comprehensible that anyone not well-versed in Python's syntax could probably guess what it is doing, and very, very snug, spanning only one line! (Then again, snakes do have a reputation of having flexible bodies. ;) ) Doesn't it make you say, “If only [insert here your favourite language before you fell in love with the beauty that is Python] had such awesome syntax?”

So next time you have the urge to create a list in your super-duper Python program (and the urge should be often), do it the Python way. Now, please excuse me while I go off to drool at all the posters of Pythonidae in my bedroom ;).