This article was written in 2001, and python now includes first-class constructs for many of these.<p>Map:<p><pre><code> >>> [ord(char) for char in "Hello"]
[72, 101, 108, 108, 111]
</code></pre>
Filter:<p><pre><code> >>> [x for x in xrange(10) if x % 2]
[1, 3, 5, 7, 9]
</code></pre>
For laziness, use parenthesis to turn the above list comprehensions into a generator comprehension. Though I don't demonstrate it, you can work over infinite sequences of objects this way with constant memory usage (without allocating all of them upfront).<p><pre><code> >>> gen = (x for x in range(10) if x % 2)
>>> gen
<generator object <genexpr> at 0xb729f6bc>
>>> for element in gen:
... print element
...
1
3
5
7
9
</code></pre>
Alice, the rabbit hole begins here --> <a href="http://docs.python.org/library/itertools.html" rel="nofollow">http://docs.python.org/library/itertools.html</a>
They are using the old 'foo and bar or baz' style as a cheap substitute for the ternary operator. That should be 'bar if foo else baz' in modern Python.
Although I'm fond of using some functional constructs where they make sense, I don't like functional languages and this article is an example of why: in search for a functional style, clear code becomes opaque and simple code becomes complex.
Ever since I've dabbled a bit with Lisp my Python (and javascript) code has become much more functional-y.<p>Still not sure whether this is a good or bad thing.