Skip to content

Commit 21f9cd3

Browse files
Merge pull request #56 from chriselrod/sizedrangeinterface
Added sized range interface.
2 parents 702ec33 + fdae083 commit 21f9cd3

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ information is adequately contained in the type for standard tools to work. In
9494
these cases, `restructure` gives a way to convert for example an `Array` into
9595
a matching `ArrayPartition`.
9696

97+
## known_first(::Type{T})
98+
99+
If `first` of instances of type `T` are known at compile time, return that first
100+
element. Otherwise, return `nothing`. For example, `known_first(Base.OneTo{Int})`
101+
returns `one(Int)`.
102+
103+
## known_last(::Type{T})
104+
105+
If `last` of instances of type `T` are known at compile time, return that
106+
last element. Otherwise, return `nothing`.
107+
108+
## known_step(::Type{T})
109+
110+
If `step` of instances of type `T` are known at compile time, return that step.
111+
Otherwise, returns `nothing`. For example, `known_step(UnitRange{Int})` returns
112+
`one(Int)`.
113+
97114
# List of things to add
98115

99116
- https://github.com/JuliaLang/julia/issues/22216

src/ArrayInterface.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,38 @@ function restructure(x::Array,y)
491491
reshape(convert(Array,y),size(x)...)
492492
end
493493

494+
"""
495+
known_first(::Type{T})
496+
497+
If `first` of an instance of type `T` is known at compile time, return it.
498+
Otherwise, return `nothing`.
499+
500+
@test isnothing(known_first(typeof(1:4)))
501+
@test isone(known_first(typeof(Base.OneTo(4))))
502+
"""
503+
known_first(::Any) = nothing
504+
known_first(::Type{Base.OneTo{T}}) where {T} = one(T)
505+
"""
506+
known_last(::Type{T})
507+
508+
If `last` of an instance of type `T` is known at compile time, return it.
509+
Otherwise, return `nothing`.
510+
511+
@test isnothing(known_last(typeof(1:4)))
512+
"""
513+
known_last(::Any) = nothing
514+
"""
515+
known_step(::Type{T})
516+
517+
If `step` of an instance of type `T` is known at compile time, return it.
518+
Otherwise, return `nothing`.
519+
520+
@test isnothing(known_step(typeof(1:0.2:4)))
521+
@test isone(known_step(typeof(1:4)))
522+
"""
523+
known_step(::Any) = nothing
524+
known_step(::Type{<:AbstractUnitRange{T}}) where {T} = one(T)
525+
494526
function __init__()
495527

496528
@require SuiteSparse="4607b0f0-06f3-5cda-b6b1-a6196a1729e9" begin

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,14 @@ using ArrayInterface: parent_type
172172
@test parent_type(Symmetric(x)) <: typeof(x)
173173
@test parent_type(UpperTriangular(x)) <: typeof(x)
174174
end
175+
176+
@testset "Range Interface" begin
177+
@test isnothing(ArrayInterface.known_first(typeof(1:4)))
178+
@test isone(ArrayInterface.known_first(typeof(Base.OneTo(4))))
179+
180+
@test isnothing(ArrayInterface.known_last(typeof(1:4)))
181+
182+
@test isnothing(ArrayInterface.known_step(typeof(1:0.2:4)))
183+
@test isone(ArrayInterface.known_step(typeof(1:4)))
184+
end
185+

0 commit comments

Comments
 (0)