The whole idea of missing vs nothing is that the first one should propagate but not erroring, but then:
a = findfirst(x -> x == 1, [1,0,missing, 1]) # out: 1, ok
b = findfirst(x -> x == 1, [0,0,missing, 1]) # out: error, should be `missing`
c = findfirst(x -> x == 1, [0,0,missing, 0]) # out: error, should be `missing`
Thank you. However both solutions return nothing with [0,0,missing,0], while I think missing should be more appropriate, as we don’t know the 3rd value.
This “manual version” of findfirst works but it is ugly and possibly very slow:
function findfirst_custom(f,x)
for i in 1:length(x)
if ismissing(x[i])
return missing
elseif f(x[i]) == true
return i
end
end
return nothing
end
EDIT: not too slower, at least for these simple cases:
As I understand, missing should propagate to the same type/data (conceptually). findfirst isn’t returning an element of the input array, but the index of an element in the input array.
b = findfirst(x -> x == 1, [0,0,missing, 1]) producing an error has the fewest assumptions about what the correct behavior is (and allows the user to specify their desired behavior/solution, e.g. skipmissing or an isequal predicate). The documented behavior of findfirst is to return a valid index/key or nothing if no element matches the predicate. A result of missing wouldn’t make sense to me, as an index exists or does not exist; there is no such thing as a missing index, IMO.
I disagree on your last sentence : an index exists, it doesn’t or I don’t know if it does exists (or which is it, when missing comes before a value for which I have true in the inner function)