Python function with multiple arguments

242 views Asked by At

I came across this Codewars question:

Your task is to write a higher order function for chaining together a list of unary functions. In other words, it should return a function that does a left fold on the given functions.

chained([a,b,c,d])(input)

Should yield the same result as

d(c(b(a(input))))

I don't really care what the answer to the problem is, I can access that on the site. What I actually need explained to me is the first function, "chained". I've never seen a function like that with 2 sets of argument in separate parenthesis, so I imagine I'm interpreting it incorrectly.. what does that mean? Thanks for the help

2

There are 2 answers

0
tobias_k On BEST ANSWER

It is not a function with two sets of parameters, but a function that returns another function, executing the functions given as parameters one after the other.

Maybe it's clearer if you separate the line into two lines:

f = chained([a,b,c,d]) # call `chained` with functions as parameters
f(input)               # call result of `chained`, which is another function
0
buran On

As stated in the question chained is a higher order function - it will take one argument - list of functions and will return/yield a function. That function is called by passing one argument - another function, in this case input.