python hack of the day -- "listable" functions

Christian Tismer tismer at appliedbiometrics.com
Thu May 13 15:53:54 EDT 1999


Stefan Franke wrote:
> 
> On 12 May 1999 16:30:34 -0700, Michael Vanier <mvanier at bbb.caltech.edu> wrote:
> 
> >
> >Having nothing better to do one day, I whipped up a quick class that
> >transforms functions into "listable" functions i.e. functions that
> >automatically map themselves over a sequence.  The idea was stolen from
> >Mathematica, but it's amazing how easy it is to implement this in python.
> 
> Or, IOW,
> 
> def listable(f):
>     return xapply (map, f)
> 
> ... if Michael Hudson's recently posted bytecodehacks were only able to hack
> those intractable builtins <wink>.

Now, allow for one little wrapper, and then:

>>> def listable(f):
... 	return xapply (lambda a, b:map(a, b), f)
>>> f=listable(lambda x:x+1)
>>> f(range(5))
[1, 2, 3, 4, 5]
>>> 

not so bad - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list