Thursday, May 21, 2009

Pythonic Tidbits

Functional programming is awesome. But while I have learned to respect it: the syntax of Anonymous inner-classes, in Java, could use a stronger dose of brevity (actually if closure support ever comes to Java this may happen). Fortunately, Python already has some seriously cool functional programming capabilities. Functions can be created on the fly, passed around, assigned to variables, sent through flaming hoops: whatever you want. In language-design speak this means that functions are first-class objects. Take a look at this interesting snipplet, which I have lifted from the code-base of my UTransit application (from the previous post):

# A list of methods for accessing schedules
methods = [(getSchedulesForToday, {'TripPeriod': 'Regular'}),
(lambda t, s: getSchedulesAfterTodayMidnight(s), {'TripPeriod': 'NextDay'}),
(getSchedulesForEarlyToday, {'TripPeriod': 'Early'})]

# For each schedule access method, we invoke it and then get all schedules from it
for query, annotation in [(m[0](startTime, getStationByShortName(startStation)), m[1]) for m in methods]:
for begin in query:
itinerary = computeItineraryFrom(begin, destinationStation)
if not itinerary == None:
itinerary.update({'StartStation': startStation, 'State': 'RouteFound'})
itinerary.update(annotation)
return itinerary

The idea here is that we have multiple methods for accessing scheduling information, which is a list of functions. The for loop iterates over each one invoking it by passing the start time and station to it, since the second function only needs the later of the two arguments, we wrap a lambda around it that serves as an adapter (alright Gang-of-Four Junkies, I am aware this is officially known as the Decorator pattern). Note, that the methods list actually contains 2-tuples (method and dictionary). The dictionaries are merged with the computed itinerary (which, of course, is also a dictiony). Why is the itinerary a dictionary? Well, it so happens the return value is passed into a Django template.

Friday, May 15, 2009

Into a parallel universe

I have been a loyal fan of the Java language for nearly six years. As such, when Google's App Engine went live: I was thoroughly disappointed to hear that it did not have support for Java. Just recently, however, Google made Java development available for a select number of developers. I was quick to sign up for this. But something interesting happened between the time that I was accepted into the Java pre-release program and the time that I started working with the Google App Engine: I started learning Python. That's right, I've managed to find myself swimming in what you might think of as a parallel universe. I emphasize universe as I have just learned that the tools, methodologies, work-flow for dynamic languages such as Python is very different than that of Java. I am not going to rehash the pros and cons of both Java and Python here (just google "Java vs. Python") here. But instead, let me point you to http://utransit.appspot.com. I wouldn't depend on it for making any important meetings, just yet. But, it took me about two days of studying and working with Python, Django, ORM modeling and the Google App Engine (all of which are new to me) to build this. Not to pat myself on the back: but that's pretty impressive. We are talking about a whole new stack of web technologies being applied towards making a functional application. In all liklihood, I will have more to say about Python and Django (which are tools I have been very happy to work with) but for now, a few words of advice to my fellow java developers: take a few moments to visit the Python universe: I think you'll agree that it will be an invaluable addition to your bag of tools.

Tuesday, April 28, 2009

No stars here, move along

At first glance, I imagine one might guess this would be a display panel on a telescope where something has gone horribly wrong.  But, a closer look would reveal that this is merely one of those Compact Disc listening stations (as often found at bookstores) where something has gone ... horribly wrong.  It's too bad the system was completely hosed: it would have been cool to hear what the "Null" language sounds like.