A Lazy Sequence

On iteration

A Python programmer discovering the magic of itertools is three months from looking for a new language that better supports the functional style.

There is an observable trend in Python programmers that results in a reasonable section of them moving to functional programming languages. This trend is encouraged by the Python language, and has a couple of temporal considerations.

Firstly the language. Python has strong support for describing operations over aggregates. Most programmers coming to Python are coming from other procedural and OO languages, and these languages at best may support a for each style loop1. The iterations of understanding (and understanding of iterations) follow a familiar path:

The new Python programmer will begin by writing programs that use the familiar for loop to process collections. At some point the syntax for List Comprehensions will be discovered. He will begin to write more and more code in this form. Following List Comprehensions are Generator Comprehensions, and perhaps Generator functions and the yield keyword.

Eventually he will stumble across itertools. This module becomes many Python programmers' favorite standard library component. This is the beginning of the end for his interest in Python. Why? Two key reasons:

  1. Python's design and leadership prefer procedural and OO to the functional programming style. While the language can facilitate it, the library and syntax don't support it naturally.
  2. Generators are a poor substitute for real lazy data structures. You can do a lot with them, but you hit the pain points fairly quickly.

Secondly the temporal considerations. Python is currently going through a major version change. This fixes many warts and problems in the language, but requires projects to be ported to the newer, cleaner Python 3.

Concurrently, Functional Programming has come to the fore in geekdom as an answer to difficulties with concurrency and expression. Two languages in particular are well positioned to be an easy jump for an experienced Python programmer: Clojure, and F#. Haskell is also a popular choice with more academically minded over F#. Scala may be preferred by those who still have a strong attachment to mixing OO and Functional styles.

F# provides the familiar whitespaced based syntax. It has syntax blocks for statement based-procedurally oriented programming, and a class syntax that feels very familiar to expat Pythonista.

Clojure is the other contender. As a modern lisp for the JVM it provides a familiar development enviroment. Clojure also provides a large palette of tools for concurrent programming -- a particular pain point for Pythonistas3 -- and is capable of running very fast.

Python's ease of development has been matched by these more recent languages. They provide features that the experienced Pythonista wishes he had, and without sacrificing speed or expression. The choice will be dictated by the enviroment and personal choice, but there are clear choices beyond Python.

See also

  1. This is changing with C# providing LINQ (to objects in particular).
  2. Along with Smalltalk, recent C#s, Ruby, and other OO languages
  3. Via the Global Interpreter Lock, mutability as a default and a lack of primatives.
5 February 2010