Why is that```d = Dict("a"=>1, "b"=>2, "c"=>3)filter!(d) do (k, v)

Why is that

d = Dict("a"=>1, "b"=>2, "c"=>3)
filter!(d) do (k, v)
    iseven(v)
end

is happy, but

d = Dict("a"=>1, "b"=>2, "c"=>3)
filter!((k,v) -> iseven(v), d)

is sad and throws ERROR: MethodError: no method matching (::var"#114#115")(::Pair{String,Int64})? How am I doing this wrong?

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

1 Like

Quoting the answer from Frederik Ekre below:

You need filter(((k,v),) -> iseven(v), d)

And more in-depth Q/A replies:

Q: I had tried ((k,v)) but I didn’t realize I needed to turn it into a tuple.
A: ((k,v)) is just (k,v) just like (1) is just 1 .
A: filter gives you a single pair that you need to destruct, so you need to create a function that takes a single argument
Q: But why don’t I run into this with the do form?
A: do (k, v) is a single argument function while do k, v is a two argument function

EDIT: This issue is relevant destructuring inconsistency with anonymous functions · Issue #36595 · JuliaLang/julia · GitHub

2 Likes