How to distinguish between "found" and "not found" in `findfirst(dict)`? E.g.:```

How to distinguish between “found” and “not found” in findfirst(dict)? E.g.:

d = Dict(nothing => 1, 2 => 3)
findfirst(==(3), d)  # == 2, ok: d[2] == 3
findfirst(==(1), d)  # == nothing, ok: d[nothing] = 1
findfirst(==(2), d)  # == nothing, but actually not found

For now I do an extra check that f(d[findfirst(f, d)]) == true, but this doesn’t look clean…

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)

I guess you could write your own findfirst method that allows you to specify the “not found” return value.

Maybe something like:

function myfindfirst(pred::Function, A, notfound=nothing)
    for (k, v) in pairs(A)
        pred(v) && return k
    return notfound
d = Dict(nothing=>1, 2=>3)
myfindfirst(==(3), d, :notfound) # 2
myfindfirst(==(1), d, :notfound) # nothing
myfindfirst(==(2), d, :notfound) # :notfound