【发布时间】:2014-04-05 06:26:59
【问题描述】:
得到一个接受三个参数的函数。
f(a, b, c) = # do stuff
还有一个返回元组的函数。
g() = (1, 2, 3)
如何将元组作为函数参数传递?
f(g()) # ERROR
【问题讨论】:
标签: julia
得到一个接受三个参数的函数。
f(a, b, c) = # do stuff
还有一个返回元组的函数。
g() = (1, 2, 3)
如何将元组作为函数参数传递?
f(g()) # ERROR
【问题讨论】:
标签: julia
使用 Nanashi 的例子,线索是调用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))
这表明这会将元组(1, 2, 3) 作为f 的输入而无需解包。要解压,请使用省略号。
julia> f(g()...)
6
Julia 手册中的相关部分在这里:http://julia.readthedocs.org/en/latest/manual/functions/#varargs-functions
【讨论】:
f(g()...) 而不是 apply,我认为第一个在 Julia 中更惯用。
使用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
如果这有帮助,请告诉我们。
【讨论】: