_ + _ - _
#27
Replies: 3 comments
-
It works by chaining functions. The first function can handle variadic args; the rest expect a single value return from the previous function call. So even if this was supported, it would look like f = (_ + _) - _
f(1, 1)(1) which doesn't seem intuitive. It should probably just raise an error at construction. |
Beta Was this translation helpful? Give feedback.
-
Hmm... So this reminds me of a very similar problem I've been thinking about a lot for function composition (what you're calling "chaining"). So we have And there's also But really these are both special orthogonal cases of a more general tree-shaped thing... consider: lambda a, b, c: f(g(a, b), c)
lambda a, b, c: f(a, g(b, c))
lambda a, b, c: f(foo=g(a, b), c)
lambda a, b, c: f(a, bar=g(b, c))
lambda a, *args, **kwargs: f(identity(a), *args, **kwargs)
lambda b, *args, **kwargs: f(*args, b=identity(b) **kwargs) You see it? Partial application is binding already computed values to specific arguments, and composition is binding functions to arguments. So you see how that suggests a callable that does Or in this case: In other words, we're looking at the idea of a function tree - a generalization of a function pipe/chain. So perhaps Of course in the general case, But In that second overload, we can detect that one side is the result of P.S. I feel like currying as a concept is also potentially useful here, especially the kind of flexibly variadic currying that is possible and common in Python: from toolz.functoolz import curry
f = curry(lambda a, b, c: (a + b) - c)
f(1)(1)(1) == 1
f(1, 1)(1) == 1
f(1)(1, 1) == 1
f(1, 1, 1) == 1 I don't know if that ends up being relevant or not, but those kinds of So maybe there's something in all that about either how |
Beta Was this translation helpful? Give feedback.
-
Converted to a discussion since there's no clear path forward. |
Beta Was this translation helpful? Give feedback.
-
Since
_ + _
works likelambda a, b: a + b
, and_ + _ - 1
works likelambda a, b: a + b - 1
, I would expect_ + _ - _
to work likelambda a, b, c: a + b - c
.But unfortunately it doesn't:
It seems that this is because
_ + _ - _
createsF(<function pipe at 0x7fbcee4eee50>, (<built-in function add>, <built-in function sub>))
, which if I understand right is more likecompose(operator.sub, operator.add)
, so it behaves likelambda a, b: operator.sub(a + b)
:Beta Was this translation helpful? Give feedback.
All reactions