Skip to content

Commit

Permalink
Range interface for Intervals (#76)
Browse files Browse the repository at this point in the history
* Range interface for Intervals

* Fixing error for unsupported keyword syntax

* Fixing error for unsupported keyword
step and length

* Fix error constructing range
with older Julia versions

* New version 0.5.3 for range support
on closed intervals
  • Loading branch information
myrddin89 authored Mar 15, 2021
1 parent 76a5a2a commit 4e5e2e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "IntervalSets"
uuid = "8197267c-284f-5f27-9208-e0e47529a953"
version = "0.5.2"
version = "0.5.3"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
21 changes: 21 additions & 0 deletions src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,27 @@ UnitRange{T}(i::TypedEndpointsInterval{:closed,:closed,I}) where {T<:Integer,I<:
UnitRange(i::TypedEndpointsInterval{:closed,:closed,I}) where {I<:Integer} = UnitRange{I}(i)
range(i::TypedEndpointsInterval{:closed,:closed,I}) where {I<:Integer} = UnitRange{I}(i)

"""
range(i::ClosedInterval; step, length)
range(i::ClosedInterval, len::Integer)
Constructs a range of a specified step or length.
"""
range(i::TypedEndpointsInterval{:closed,:closed}; step=nothing, length=nothing) =
range(leftendpoint(i); stop=rightendpoint(i), step=step, length=length)
range(i::TypedEndpointsInterval{:closed,:closed}, len::Integer) = range(i; length=len)

"""
range(i::Interval{:closed,:open}; length)
range(i::Interval{:closed,:open}, len::Integer)
Constructs a range of a specified length with `step=width(i)/length`.
"""
range(i::TypedEndpointsInterval{:closed,:open}; length::Integer) =
range(leftendpoint(i); step=width(i)/length, length=length)
range(i::TypedEndpointsInterval{:closed,:open}, len::Integer) = range(i; length=len)


"""
duration(iv)
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,14 @@ struct IncompleteInterval <: AbstractInterval{Int} end
@test Base.Slice(1..5) == Base.Slice{UnitRange{Int}}(1..5) == Base.Slice(1:5)
end

@testset "range" begin
@test range(0..1, 10) == range(0; stop=1, length=10)
@test range(0..1; length=10) == range(0; stop=1, length=10)
@test range(0..1; step=1/10) == range(0; stop=1, step=1/10)
@test range(Interval{:closed,:open}(0..1), 10) == range(0; step=1/10, length=10)
@test range(Interval{:closed,:open}(0..1); length=10) == range(0; step=1/10, length=10)
end

@testset "IteratorSize" begin
@test Base.IteratorSize(ClosedInterval) == Base.SizeUnknown()
end
Expand Down

2 comments on commit 4e5e2e9

@dlfivefifty
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/31986

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.3 -m "<description of version>" 4e5e2e9cf308cf8693dc0f663627b61cc5d44fa6
git push origin v0.5.3

Please sign in to comment.