Stream: helpdesk (published)

Topic: filter dict with anonymous function


view this post on Zulip Max Köhler (Feb 13 2021 at 01:15):

whats wrong with this?

julia> filter(((k,v))->k=="a", testdict)

I know that

julia> filter((kv)->kv[1]=="a", testdict)

works

view this post on Zulip Mason Protter (Feb 13 2021 at 01:23):

You're missing a comma.

((k,v))->k=="a"

is the same as

(k,v)->k=="a",

You instead need to write

filter(((k,v),)->k=="a", testdict)

This is kinda an unfortunate thing with anonymous functions in julia

view this post on Zulip Mason Protter (Feb 13 2021 at 01:25):

This is one area where do blocks are nice, because you need just one layer of parens:

filter(testdict) do (k, v)
    k == "a"
end

view this post on Zulip Max Köhler (Feb 13 2021 at 01:39):

ahhh I see! Thanks for clarifying. I actually like the do block more, the comma is a bit weird


Last updated: Nov 06 2024 at 04:40 UTC