Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1111,15 +1111,15 @@ julia> findprev(A,1)
"""
function findprev(A, start::Integer)
for i = start:-1:1
A[i] != 0 && return i
A[i] != zero(A[i]) && return i
end
return 0
end

"""
findlast(A)

Return the linear index of the last non-zero value in `A` (determined by `A[i]!=0`).
Return the linear index of the last non-zero value in `A` (determined by `A[i]!=zero(A[i])`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a genstdlib run

Returns `0` if there is no non-zero value in `A`.

```jldoctest
Expand Down
3 changes: 3 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ a = [0,1,2,3,0,1,2,3]
@test findprev(isodd, [2,4,5,3,9,2,0], 7) == 5
@test findprev(isodd, [2,4,5,3,9,2,0], 2) == 0

# find with a type where zero(T) != 0
@test findlast(Any[[1], I, 5, 0*I]) == 3

# find with general iterables
s = "julia"
# FIXME once 16269 is resolved
Expand Down