22
votes

Got a function that takes three arguments.

f(a, b, c) = # do stuff

And another function that returns a tuple.

g() = (1, 2, 3)

How do I pass the tuple as function arguments?

f(g()) # ERROR
2

2 Answers

35
votes

Using Nanashi's example, the clue is the error when you call f(g())

julia> g() = (1, 2, 3)
g (generic function with 1 method)

julia> f(a, b, c) = +(a, b, c)
f (generic function with 1 method)

julia> g()
(1,2,3)

julia> f(g())
ERROR: no method f((Int64,Int64,Int64))

This indicates that this gives the tuple (1, 2, 3) as the input to f without unpacking it. To unpack it use an ellipsis.

julia> f(g()...)
6

The relevant section in the Julia manual is here: http://julia.readthedocs.org/en/latest/manual/functions/#varargs-functions

5
votes

Use apply.

julia> g() = (1,2,3)
g (generic function with 1 method)

julia> f(a,b,c) = +(a,b,c)
f (generic function with 1 method)

julia> apply(f,g())
6

Let us know if this helps.