Just now, I wanted to define the all function in Python, which takes a sequence as its argument and returns True iff no element of the sequence is False. My instinct was to do it with reduce:
all = lambda seq: reduce(and, seq, True)
But and is a syntactic keyword, so that doesn't work. However, abusing the fact that Python follows Iverson's convention, we can map int.__mul__ instead:
all = lambda seq: reduce(int.__mul__, seq, True)
Evil, delicious evil. Do check out David Jones' weblog if you haven't already, it's really interesting. I started reading it the other day and couldn't stop :)
Comments are closed.