[Python-Dev] functools additions

SevenInchBread adamadamadamamiadam at gmail.com
Sun Apr 15 12:38:43 CEST 2007


So.... if it's alright with the privledged folk - I'd like to commit these
minor (and probably non-controversial) additions to the functools module.



def cat(x): return x

def multimap(func, s, n=2):
    assert n > 0, "n must be positive"
    return (map(func, seq)
            if n == 1 else
            map(lambda x: multimap(func, x, n-1),
                seq))

def multifilter(func, s, n=2):
    return multimap(lambda x: filter(func, x), s, n-1)

def multireduce(func, s, n=2):
    return multimap(lambda x: reduce(func, x), s, n-1)


class nullfunc(object):
    def __call__(self, *a, **k): return self
    def __getattr(self, name): return getattr(None, name)



cat is a generic identity function - useful for some higher-order functions
to specify a function that "does nothing". multimap, multifilter, and
multireduce, are all multi-dimensional versions of map, filter, and reduce.
nullfunc is a special callable object that emulates the failed callable None
proposal - which wasn't really a good idea, but would have been more useful
as a different object apart from None.

you could probably even put cat in __builtins__ - so you don't need to waste
effort importing such a trivial function.

--.
"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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20070415/bc896418/attachment.html 


More information about the Python-Dev mailing list