A composable function wrapper for a regular Python function. This overloads the regular __call__ operator for currying, i.e., arguments passed to __call__ are remembered for the eventual function application. The final function application happens via the `of` method.
| 139 | |
| 140 | |
| 141 | class Function(Composable): |
| 142 | """A composable function wrapper for a regular Python function. |
| 143 | |
| 144 | This overloads the regular __call__ operator for currying, i.e., |
| 145 | arguments passed to __call__ are remembered for the eventual |
| 146 | function application. |
| 147 | |
| 148 | The final function application happens via the `of` method. |
| 149 | """ |
| 150 | |
| 151 | def __init__(self, f, *args, **kw): |
| 152 | if not callable(f): |
| 153 | raise ValueError("%s: is not callable" % f) |
| 154 | self.f = f |
| 155 | self.args = list(args) |
| 156 | self.kw = kw |
| 157 | |
| 158 | def __call__(self, *args, **kw): |
| 159 | new_args = list(args) + self.args |
| 160 | new_kw = self.kw.copy() |
| 161 | new_kw.update(kw) |
| 162 | return Function(self.f, *new_args, **new_kw) |
| 163 | |
| 164 | # TODO(tmb) The `of` method may be renamed to `function`. |
| 165 | def funcall(self, x): |
| 166 | args, kw = get_positional(self.args, self.kw) |
| 167 | if debug_: |
| 168 | print("DEBUG:", self.f, x, args, kw) |
| 169 | return self.f(x, *args, **kw) |
| 170 | |
| 171 | |
| 172 | class Composition(Composable): |