javascriptcoffeescriptapplysplatunroll

Unrolling list into function args?


Given an argument list and a two-arity function:

args = [5, 6]
f = ((y,z)->y*z)

How do I unroll args into function arguments? - E.g. in Python you can do: f(*args).

What I've tried (more JavaScript style I suppose):

Function.call.apply(f, args)
# When that failed, tried:
((y,z) -> y*z).call.apply(null, [5,6])

Solution

  • Use f(args...).

    f = (x, y) -> x + y
    list = [1, 2]
    console.log(f(list...)) # -> 3
    

    You can also mix and match this with regular arguments:

    f = (a, b, c, d) -> a*b + c*d
    list = [2, 3]
    console.log(f(1, list..., 4)) # -> 1*2 + 3*4 == 14