Arrays 如何将函数附加到数组?
例如,我可以创建一个包含函数的数组Arrays 如何将函数附加到数组?,arrays,function,julia,Arrays,Function,Julia,例如,我可以创建一个包含函数的数组 julia> a(x) = x + 1 >> a (generic function with 1 method) julia> [a] >> 1-element Array{#a,1}: a 但我似乎无法将函数添加到空数组: julia> append!([],a) >> ERROR: MethodError: no method matching length(::#a) Closes
julia> a(x) = x + 1
>> a (generic function with 1 method)
julia> [a]
>> 1-element Array{#a,1}:
a
但我似乎无法将函数添加到空数组:
julia> append!([],a)
>> ERROR: MethodError: no method matching length(::#a)
Closest candidates are:
length(::SimpleVector) at essentials.jl:168
length(::Base.MethodList) at reflection.jl:256
length(::MethodTable) at reflection.jl:322
...
in _append!(::Array{Any,1}, ::Base.HasLength, ::Function) at .\collections.jl:25
in append!(::Array{Any,1}, ::Function) at .\collections.jl:21
我真正想做的是存储预定义函数,以便最终将它们映射到一个值上。例如:
x = 0.0
for each fn in vec
x = x + fn(x)
end
append代码>用于将一个集合附加到另一个集合。
您正在寻找推送代码>,将元素添加到集合中
您的代码应该是push!([],a)
见文件:
julia>?append!
search: append!
append!(collection, collection2) -> collection.
Add the elements of collection2 to the end of collection.
julia> append!([1],[2,3])
3-element Array{Int64,1}:
1
2
3
julia> append!([1, 2, 3], [4, 5, 6])
6-element Array{Int64,1}:
1
2
3
4
5
6
Use push! to add individual items to collection which are not already themselves in another collection. The result is of the preceding example is equivalent to push!([1, 2, 3], 4, 5, 6).
vs:
append代码>用于将一个集合附加到另一个集合。
您正在寻找推送代码>,将元素添加到集合中
您的代码应该是push!([],a)
见文件:
julia>?append!
search: append!
append!(collection, collection2) -> collection.
Add the elements of collection2 to the end of collection.
julia> append!([1],[2,3])
3-element Array{Int64,1}:
1
2
3
julia> append!([1, 2, 3], [4, 5, 6])
6-element Array{Int64,1}:
1
2
3
4
5
6
Use push! to add individual items to collection which are not already themselves in another collection. The result is of the preceding example is equivalent to push!([1, 2, 3], 4, 5, 6).
vs:
的第二个参数是集合。您可以使用push!([],a)
用于单个项目,或追加!([],[a])
对于集合的第二个参数是集合。您可以使用push!([],a)
用于单个项目,或追加!([],[a])
对于集合