So I've cooked up some very simple functions to add to functools - to expand it into a more general-purpose module.<br><br><br>def cat(x): return x<br><br>class nullfunc(object):<br> def __call__(self, *args, **kargs): return self
<br> def __getattr__(self, name): return getattr(None, name)<br><br>def multimap(func, seq, n=2):<br> assert n > 0, "n must be positive"<br> if n == 1: return map(func, seq)<br> else: return map(lambda x: multimap(func, x, n-1), seq)
<br><br>def multifilter(func, seq, n=2):<br> return multimap(lambda x: filter(func, x), seq, n-1)<br><br>def multireduce(func, seq, n=2):<br> return multimap(lambda x: reduce(func, x), seq, n-1)<br><br>In an expression, cat achieves the effect of doing nothing - which makes it a nice default value for some filter-like functions. It's not a huge improvement - lambda x: x is almost as trivial to define, but I think it would be nice to have a standard identity function that all of Python could recognize.
<br><br>nullfunc is a function that *chomps* away at its input while otherwise retaining the properties of None - much like the recently proposed callable None - except not as disasterous to existing practices and potentially more useful as an optional behavior. This is something that cannot be as quickly implemented as the cat function.
<br><br>multimap is a multi-dimensional mapping function that recursively decends a sequence and applies a function to the data within.<br><br>multifilter is a multi-dimensional filter function that recursively descends a sequence and applies a function to the data within
<br><br>multireduce - well... you get the idea<br><br>A better place to put some of these functions might be __builtin__, so you don't have to waste time importing something as basic as cat.<br><br><br><br><br><br><br>
<br><br clear="all"><br>-- <br>"What's money? A man is a success if he gets up in the morning and goes to bed at night and in between does what he wants to do." ~ Bob Dylan