Sijun
1
Why does this fail?
julia> findfirst(((x,y))->x==1, [(1,2), (3,4)])
ERROR: MethodError: no method matching (::var"#7#8")(::Tuple{Int64,
Int64})
while these two are fine:
julia> findfirst([(1,2), (3,4)]) do (x, y)
x==1
end
1
function cond((x, y))
x==1
end
julia> findfirst(cond, [(1,2), (3,4)])
1
I think this is since the double parenthesis does not really do much here, so you actually get a function expecting two variables.
What you can do is add an extra comma to show it is supposed to be a tuple of a single argument (that is itself a tuple)
findfirst(((x,y),) -> x == 1, [(1, 2), (3, 4)])
2 Likes
lmiq
3
Note that this is the same problem as:
julia> f = ((x,y)) -> x == 1
#20 (generic function with 1 method)
julia> f((1,1))
ERROR: MethodError: no method matching (::var"#20#21")(::Tuple{Int64, Int64})
Closest candidates are:
(::var"#20#21")(::Any, ::Any) at REPL[17]:1
Stacktrace:
[1] top-level scope
@ REPL[18]:1
julia> f(1,1)
true
seems to be a parsing edge case.
You can solve that by adding a comma:
julia> findfirst( ((x,y),) -> x == 1 , [(1,2), (3,4)])
1
2 Likes
Sijun
4
Thank you for the tip! Yes it seems like an edge case.