2.2 bytecodehacks.xapply

This module is inspired by Don Beaudry's functor module, which is available in the contrib section of the Python ftp site.

Basically xapply can be thought of as ``lazy apply.'' You supply a function and some of its arguments to xapply and it returns a function with these arguments prebound. The name ``curry'' has also been suggested for this function, if that's the sort of word you understand. An example:

from bytecodehacks.xapply import xapply

def make_adder(n):
    def f(x,y):
        return x+y
    return xapply(f,n)

Then in usage:

>>> add1=make_adder(1)
>>> add1(4)
5

This module is not (yet?) as general as the functor module as it only allows wrapping of functions and instance methods, but it's getting there; I think the only incompatibility left is that varadic parameters (i.e. ``*'' ones) are basically invisible to the xapply function.

It's main advantage over the functor module is that of speed; while functor.xapply returns an object that is twice as slow as a regular function, what bytecodehacks.xapply.xapply returns is a function, and calling it is no slower than calling any other (in most cases; if the the passed function takes ``**kw'' style arguments and you pass in keyword arguments not in the functions parameter list then there will be a small overhead as the kw dict gets updated).

There is but one exported function from this module:

xapply (func, *args, **kw)
Return a new function based on func with the passed arguments bound. If
f = xapply(func,arg1,...,argk,key1=val1,...,keyl=vall)
then
f(arg{k+1},...,argn,key{l+1}=val{l+1},...,keym=valm)
is equivalent to
func(arg1,...,argn,key1=val1,...,keym=valm)

Send comments to mwh@python.net