2

Hypothetical situation - can a currying function have an unknown number of arguments (kind of like varargs) Eg in Python:

addByCurrying(1)(2)(3)(4)

Should equal 10

addByCurrying(5)(6)

Should equal 11

How could I implement it?

asked Aug 24, 2016 at 2:07
6
  • You can't do that in the most straightforward way. That would mean that addByCurrying(1)(2) is both a number and a function, which isn't possible. Commented Aug 24, 2016 at 2:37
  • 4
    Have a look at Is it possible to have currying and variadic function at the same time? Commented Aug 24, 2016 at 2:39
  • Funny thing I discovered while playing around a bit: (lambda x: x (x)) ((lambda a: lambda b: lambda c: a (b)) (lambda x: x (x))) takes an argument and returns a new instance of itself, meaning you can pass as much arguments as you want. Commented Aug 24, 2016 at 6:54
  • @Tanner Swett could Church Encodings help on that? Commented Aug 24, 2016 at 6:55
  • 2
    Possible duplicate of Is it possible to have currying and variadic function at the same time? Commented Aug 24, 2016 at 9:03

2 Answers 2

3

I recently answered a question on S.O regarding this exact situation. You can't do this with traditional functions in Python.

You can do this by taking advantage of callables though, overloading the __call__ dunder of an int subclass.

In short, return a new instance of your self with the updated value (+ here):

class addByCallable(int):
 def __call__(self, v):
 return type(self)(self + v)

Now, you call it and get this 'form' of currying:

addByCallable(1)(2)(3) # 6

Which is as close as you can get to doing this in Python.

2
  • 1
    Actually this is a great answer! Overloading call is a creative solution that I wouldn't have even thought of. So to do this for another class, I would just define a call function for it, that uses the value of self to return the same class, but also is a function! That's exactly what I was hoping for! Commented Aug 25, 2016 at 0:15
  • "but also is a function" isn't the exact terminology i'd use :-P. It is also callable, that is, it can behave as a function, that's really all we need here. Commented Aug 25, 2016 at 1:53
1

This is not possible since there is no way the function could know if it should return a number or a curried function.

There are various way of "cheating" to achieve some thing somewhat like this, for example you could call with no arguments in order to get the number rather than a function:

addByCurrying(1)(2) --> curried function
addByCurrying(1)(2)() --> the number 3

Which trick is most appropriate depends on what you are trying to achieve.

answered Aug 24, 2016 at 10:18
2
  • Can you define a lambada with an optional parameter? Or one with a default? Commented Aug 24, 2016 at 12:09
  • @vikarjramun: Yes. Commented Aug 24, 2016 at 12:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.