diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 9c9006ff1076c..d38bda15ab305 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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; dims::Integer=1) + dims == 1 || throw(ArgumentError("dimension $dims out of range (1:$N)")) + return [@inbounds r[i+1] - r[i] for i in firstindex(r):lastindex(r)-1] +end ### from abstractarray.jl diff --git a/test/ranges.jl b/test/ranges.jl index 77f925b5af337..36a803abc0f06 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1607,6 +1607,15 @@ 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) + 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])