I recently had a discussion with a co-worker about feasibility of using anonymous functions in Python. We happen to overuse them quite a bit and this is not something I’m particularly fond of. For me lambdas in Python are looking pretty weird and thus I prefer to use them sparingly. I wasn’t entirely sure why is it so – given that I’m quite a fan of functional programming paradigm – until I noticed a seemingly insignificant fact.
lambda keyword is long. With six letters, it is among the longer keywords in Python 2.x, tied with
global, and beaten only by
finally. Quite likely, this is what causes lambdas in Python to stand out and require additional mental resources to process (assuming we’re comfortable enough with the very idea of anonymous functions). The long
lambda keyword seems slightly out of place because, in general, Python keywords are short.
Or are they?… Thinking about this, I’ve got an idea of comparing the average length of keywords from different programming languages. I didn’t really anticipate what kind of information would be exposed by applying such a metric but it seemed like a fun exercise. And it surely was; also, the results might provoke a thought or two.
Here they are:
|Language||Keyword||Total chars||Chars / keyword|
The newest incarnation of C++ seems to be losing badly in this competition, followed by C#. On the other side of the spectrum, Go and Python seem to be deliberately designed to avoid keyword bloat as much as possible. Java is somewhere in between when it comes to sheer numbers of keywords but their average length is definitely on the long side. This could very well be one of the reasons for the perceived verbosity of the language.
For those interested, the exact data and code I used to obtain these statistics are in this gist.
There’s a funny thing people in Emacs/Lispland often do – you can add a small piece of code to Emacs that collapses word “lambda” into symbol “λ”. It’s only a rendering thing (the word “lambda” is still there), but it makes code a bit more readable. So instead of:
I’ve got a colleague that has this. It replaces not only lambda, but also ‘and’, ‘or’, ‘True’, ‘False’, ‘None’ and probably some keywords and operators with their mathematical equivalents. Afterwards even ordinary Python code looks extremely cryptic :)
Hahaha, half-way to APL? :).
If you did that to Haskell, I think you would be extremely close :)
For the purpose of fairness: I think the count is off by 3 in C++11 favour ;-) Those pesky spaces!
I am curious though, why did you not include brainfuck? :-)
The lack of keywords whatsoever seems like a perfectly valid reason to omit it ;P
And by the way, the issue of what constitutes a keyword is not actually all that trivial. I had to omit all the Lisps and e.g. Haskell for precisely that reason. Maybe the whole concept of a keyword is very specific to certain class of languages: imperative ones, with Basic- or C-like syntax?…
TeMPOraL and Xion:
Did you hear about this interesting language? ;)