Skip to content

Commit

Permalink
Fix findfirst with decreasing step range
Browse files Browse the repository at this point in the history
  • Loading branch information
laborg committed Nov 11, 2019
1 parent 6eebbbe commit bd26f67
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,8 @@ findfirst(testf::Function, A::Union{AbstractArray, AbstractString}) =
findnext(testf, A, first(keys(A)))

function findfirst(p::Union{Fix2{typeof(isequal),T},Fix2{typeof(==),T}}, r::StepRange{T,S}) where {T,S}
first(r) <= p.x <= last(r) || return nothing
isempty(r) && return nothing
minimum(r) <= p.x <= maximum(r) || return nothing
d = convert(S, p.x - first(r))
iszero(d % step(r)) || return nothing
return d ÷ step(r) + 1
Expand Down
1 change: 1 addition & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ end
@test findfirst(==(7), 1:2:10) == 4
@test findfirst(==(10), 1:2:10) == nothing
@test findfirst(==(11), 1:2:10) == nothing
@test findfirst(==(-7), 1:-1:-10) == 9
end
@testset "reverse" begin
@test reverse(reverse(1:10)) == 1:10
Expand Down

0 comments on commit bd26f67

Please sign in to comment.