Core S2 Software Solutions

Power of Lambda; the powerful sorting example

Functional programming is really taking off – as it should! It provides some great short-hand ways of implementing complex or minimally annoying long-blocks of code very quickly. Though it does have some slight drawbacks in terms of maintainability and code clarity (try to debug closures without any sort of special tool / support in the debugger, you’ll go crazy). A classic example is the in-line support of functions (i.e. anonymous functions / lambda functions). If you need to write a custom comparison function for a sorting algorithm, several languages give you a simple framework to work against. The stdlib library of C gives you access to qsort, where you must implement your function and pass said function-pointer to the qsort function. It’s far from complicated, but the catch is that it isn’t flexible, as you can’t write this code in-line with your qsort function call.

Java takes, as expected, an OOP approach: your object must derive from the Comparable class and implement a basic set of required functions. Once this is done, you can sort your data using generic sorting functions in Java’s array implementations. Yet, this limits you since a class can only have a single comparison implementation, meaning you would have to re-derive from your base class, re-implement the Comparable class functions, and do quite a bit of overhead boilerplate code just to support different comparisons.

This is where lambda-functions step in with the best solution: it supports simple syntax and in-line embedding of code. Python, as an example, allows you define the comparison function through their standard sort function, and even better: the syntax is clear and condense, so no boilerplate overhead is required.

What’s interesting is this notion of lambda has become so helpful in many higher-level languages like Java and Python, there are several groups and companies implementing equivalents (and more!) in lower-level languages. Objective-C now has block-support, which allows for anonymous functions, threading of such functions, and even closure support! Though the latest C (C11) standard doesn’t support it, C++11 does in a somewhat unique way: slightly odd syntax (done so no new major syntax changes are required), implicit return types (not unusual, but I like C and C++ for their pseudo strong/weak-typing), and its bizarre variable closure operators / property symbols (the syntax looks like “[=]” and more). I do applaud the standards group to sticking with adding big ideas, but introducing little syntactical changes.

All in all, I’m glad that lower-level languages (C, C++, etc.) are taking advantage of powerful programming constructs like lambdas, and integrating them with great performance so we can use them on embedded systems (such as the iPhone). Though this new kind of programming is far from a revolution, it is a welcomed natural evolution of programming languages, and I hope to see even healthier improvements like this in the future!

This entry was posted in News & Updates. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


Sites map