how to pass tuple as function arguments

2019-03-24 19:06发布

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

标签: julia
2条回答
Luminary・发光体
2楼-- · 2019-03-24 19:22

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

查看更多
一夜七次
3楼-- · 2019-03-24 19:28

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.

查看更多
登录 后发表回答