Skip to content

Commit

Permalink
fix #36116, diff(::AbstractRange) returns an Array (#36117)
Browse files Browse the repository at this point in the history
* fix #36116, diff(::AbstractRange) returns an Array

(cherry picked from commit b49a0d5)
  • Loading branch information
mbauman authored and KristofferC committed Jun 4, 2020
1 parent 3ccc916 commit 88b689b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,10 @@ function diff(a::AbstractArray{T,N}; dims::Integer) where {T,N}

return view(a, r1...) .- view(a, r0...)
end
function diff(r::AbstractRange{T}; dims::Integer=1) where {T}
dims == 1 || throw(ArgumentError("dimension $dims out of range (1:1)"))
return T[@inbounds r[i+1] - r[i] for i in firstindex(r):lastindex(r)-1]
end

### from abstractarray.jl

Expand Down
10 changes: 10 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,16 @@ end
@test collect(r) == ['a','c','e','g']
end

@testset "diff of ranges, #36116" begin
for r in (0:2, 0:1:2, 0.0:1.0:2.0, LinRange(0,2,3))
@test diff(r) == diff(collect(r)) == fill(1, 2)
@test_throws ArgumentError diff(r, dims=2)
end
for r in (0:2:5, 0.1:0.1:2.0, LinRange(0,2,33))
@test diff(r) == diff(collect(r)) == [r[i+1] - r[i] for i in 1:length(r)-1]
end
end

@testset "Return type of indexing with ranges" begin
for T = (Base.OneTo{Int}, UnitRange{Int}, StepRange{Int,Int}, StepRangeLen{Int}, LinRange{Int})
@test eltype(T(1:5)) === eltype(T(1:5)[1:2])
Expand Down

0 comments on commit 88b689b

Please sign in to comment.