Entry
preapply
Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 376, Harald Hanche-Olsen
"""
Packages: functional_programming
"""
"""
| >>> from papply import X
| >>> map(X(t,(),73),range(10))
|
| The idea is that after a call to X(func,...) there are a number of
| `holes' (notated by an empty tuple) in the arglist that can be `filled
| in' by subsequent calls (to what X returns). This means you can't
| presupply the empty tuple to func, but I couldn't think of a better
| way.
|
| It's a bit like currying, but more general.
Useful, but a bit complicated methinks. I recently cooked up this
somewhat simpler class, to solve a commonly occuring variant of your
problem:
"""
class preapply:
'"Apply" a function to too few args, returning a function.'
def __init__(self, proc, *args):
self.args = args
self.proc = proc
self.ignore = 0
def ignoring(self, ignore):
'Make this class, when called, ignore this many initial args'
self.ignore = ignore
return self
def __call__(self, *args):
apply(self.proc, self.args + args[self.ignore:])
"""
I particularly like the option of binding preapply(f).ignoring(1) to a
Tkinter event, for all those cases when f has no need of the event
parameter itself. (No, I am not proud of the docstrings.)
or-maybe-I-should-have-named-the-class-Schoenfinkel-ly y'rs,
"""