Add `find` method to look for a specific element (paralleling `findfirst`, etc.)

Probably the single most common use of the variants of the find method is to search for a specific element in an array. The Base methods findfirst, findlast, findnext, and findprev all conveniently implement this functionality by having methods of the form findfirst(A, v) which check the elements of the array A for equality with the specific element v. But find does not; in order to search an array for the indices of all the appearances of an element v in an array A, we need to use the more cumbersome predicate-specification version find(x -> x == v, A). Should we add a method find(A, v), in order to parallel the other variant methods?

3 Likes

find(A .== v) is the same and less cumbersome

Except it could be less performant when A is large and A .== v is sparse.

That’s right.
But what would be nice here is “broadcast fusing”, making the above as performant as the typical evaluation.
In any case, for better performance, there is also:

myfind(A,v) = collect(i for (i,e) in enumerate(a) if e==v) 

or maybe the same without the collect as sometimes an iterator is what you need.

I have an old PR implementing this, which unfortunatly didn’t get much support. I will rebase it though.

3 Likes