Reputation: 34236
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
Upvotes: 24
Views: 9799
Reputation: 14179
Answer outdated as of Julia version 0.4, released in 2015:
In modern versions of Julia, use the ...
operator f(g()...)
.
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.
Upvotes: 6
Reputation: 1086
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
Upvotes: 39