I found weird behavior of tuple mapping on variables. I need to get indices of elements by a rule.
This code is working fine too:
names = ["aa", "ab", "ca"]
filter((i) -> startswith(i[2], "a"), collect(enumerate(names)))
I specified one variable for the tuple and, next I’m taking value by index - i[2]. And it is not good.
But the code:
names = ["aa", "ab", "ca"]
filter((i, name) -> startswith(name, "a"), collect(enumerate(names)))
doesn’t work. I’m getting error:
ERROR: MethodError: no method matching (::getfield(Main, Symbol("##143#144")))(::Tuple{Int64,Symbol})
Closest candidates are:
#143(::Any, ::Any) at none:1
Stacktrace:
[1] mapfilter(::getfield(Main, Symbol("##143#144")), ::typeof(push!), ::Array{Tuple{Int64,Symbol},1}, ::Array{Tuple{Int64,Symbol},1}) at ./abstractset.jl:336
[2] filter(::Function, ::Array{Tuple{Int64,Symbol},1}) at ./array.jl:2352
[3] top-level scope at none:0
At the same time, following code works fine:
names = ["aa", "ab", "ca"]
@show arr = collect(enumerate(names))
(i, name) = arr[1]
@show i
@show name
I’m getting:
arr = collect(enumerate(names)) = Tuple{Int64,String}[(1, "aa"), (2, "ab"), (3, "ca")]
i = 1
name = "aa"
Is it a bug or I’m doing something wrong?
Julia 1.0.3