2016-12-10 3 views
0

예를 들어 함수가 포함 된 배열을 만들 수 있습니다.함수를 배열에 추가하는 방법은 무엇입니까?

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 

내가 ulimately 내가 궁극적으로이를 통해지도 할 수 있도록 미리 정의 된 기능을 저장되고 싶지 가치. 예 :

x = 0.0 

for each fn in vec 
    x = x + fn(x)  
end 
+5

['추가'!] (HTTP의 두 번째 매개 변수 : //docs.julialang. org/en/release-0.5/stdlib/collections?? highlight = append # Base.append!)는 콜렉션입니다. 개별 항목에'! ([], a)'를 사용하거나 컬렉션에'(!, [a])'를 추가 할 수 있습니다 – Gomiero

답변

4

append!은 하나의 컬렉션을 다른 그룹에 추가하기위한 것입니다. 컬렉션에 요소를 추가하려는 경우 push!을 찾고 있습니다.

코드는

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). 

대 :

julia>?push! 

search: push! pushdisplay 

push!(collection, items...) -> collection 

Insert one or more items at the end of collection. 

julia> push!([1, 2, 3], 4, 5, 6) 
6-element Array{Int64,1}: 
1 
2 
3 
4 
5 
6 

Use append! to add all the elements of another collection to collection. The result of the preceding example is equivalent to append!([1, 2, 3], [4, 5, 6]). 
관련 문제