Julia v0.4中匿名函数的参数数
我的包裹中包含以下内容:Julia v0.4中匿名函数的参数数,julia,anonymous-function,Julia,Anonymous Function,我的包裹中包含以下内容: """ numparameters(f) Returns the number of parameters of `f` for the method which has the most parameters. """ function numparameters(f) if length(methods(f))>1 warn("Number of methods for f is greater than 1. Choosing linearit
"""
numparameters(f)
Returns the number of parameters of `f` for the method which has the most parameters.
"""
function numparameters(f)
if length(methods(f))>1
warn("Number of methods for f is greater than 1. Choosing linearity based off of method with most parameters")
end
numparm = maximum([length(m.sig.parameters) for m in methods(f)])
if VERSION < v"0.5-"
return numparm
else
return (numparm-1) #-1 in v0.5 since it add f as the first parameter.
end
end
“”“
数值参数(f)
返回参数最多的方法的“f”参数数。
"""
函数数值参数(f)
如果长度(方法(f))>1
警告(“f的方法数量大于1。根据大多数参数的方法选择线性度”)
终止
numparm=最大值([方法(f)中m的长度(m.sig.参数)])
如果版本
这适用于泛型函数,因此也适用于v0.5+上的匿名函数。但是,v0.4上的匿名函数没有方法,因此这会导致错误。我想知道是否有人知道解决方法。在0.4上,这应该适用于匿名函数:
length(Base.uncompressed_ast(f.code).args[1])
这将从AST中提取形式参数列表并获取其长度。然而,这是相当昂贵的,所以你不应该经常这样做。是的,它很少使用,而且只在v0.4中使用。听起来这样行得通。让我试一试。