Search code examples
pythonfunctionparentheseschainunary-function

Python function with multiple arguments


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


Solution

  • 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