I still having a hard time with all this (Julia v0.5).
Comprehension on an Array{Tuple}
is fine
julia> a=[(1,2),(3,4)];
julia> [x for (x,y) in a]
2-element Array{Int64,1}:
but not map
julia> map((x,y)->x, a)
ERROR: MethodError: no method matching (::##3#4)(::Tuple{Int64,Int64})
even though
(x,y) = a[1]
is fine.
Also filter
raises an error on
julia> filter((x,y)->x==3, a)
ERROR: MethodError: no method matching (::##5#6)(::Tuple{Int64,Int64})
because a
is not an associative collection (two arguments are passed to the function in this case: this is specified in the manual). Indeed filter
works here
julia> filter((x,y)->x==3, Dict(a))
Dict{Int64,Int64} with 1 entry:
Then again neither one is valid:
julia> foreach((x,y)->println(x), a)
ERROR: MethodError: no method matching (::##9#10)(::Pair{Int64,Int64})
julia> foreach((x,y)->println(x), Dict(a))
ERROR: MethodError: no method matching (::##11#12)(::Pair{Int64,Int64})
Instead
julia> for (x,y) in a
println(x)
end
is OK.
Then I come across this:
julia> filter((x,y)->begin println(typeof(x)); x[1]==3; end, Dict(a))
Int64
Int64
Dict{Int64,Int64} with 1 entry:
but it should be an error because Int64
has no getindex
.
I am very confused, but there must have been a good reason to have it this way and I cannot see it. How can I picture all this in a more systematic way?
(And all this because of this post.)