Skip to content

Commit

Permalink
Provide range methods for OpenInterval and `Interval{:open,:close…
Browse files Browse the repository at this point in the history
…d}` (#193)

* Provide `range` methods for `OpenInterval` and `Interval{:open,:closed}`

Methods already exist for ClosedInterval and Interval{:closed,:open};
these new methods complete the set of range extensions for each interval
type.

* CI: test Julia LTS instead of 1.6

Julia LTS is now at 1.10.5
  • Loading branch information
Sagnac authored Jan 1, 2025
1 parent 9ee030a commit 56a55ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- 'lts'
- '1'
- 'nightly'
os:
Expand Down
31 changes: 31 additions & 0 deletions src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ range(i::TypedEndpointsInterval{:closed,:closed}, length::Integer) = range(i; le
"""
range(i::Interval{:closed,:open}; length)
range(i::Interval{:closed,:open}, length::Integer)
range(i::Interval{:open,:closed}; length)
range(i::Interval{:open,:closed}, length::Integer)
Constructs a range of a specified length with `step=width(i)/length`.
Expand All @@ -396,6 +398,9 @@ Constructs a range of a specified length with `step=width(i)/length`.
julia> range(iv"[1, 2)", 7) # Does not contain right endpoint
1.0:0.14285714285714285:1.8571428571428572
julia> range(iv"(1, 2]", 7) # Does not contain left endpoint
1.1428571428571428:0.14285714285714285:2.0
julia> range(1, 2, 8)
1.0:0.14285714285714285:2.0
```
Expand All @@ -404,6 +409,32 @@ range(i::TypedEndpointsInterval{:closed,:open}; length::Integer) =
range(leftendpoint(i); step=width(i)/length, length=length)
range(i::TypedEndpointsInterval{:closed,:open}, length::Integer) = range(i; length=length)

range(i::TypedEndpointsInterval{:open,:closed}; length::Integer) =
range(; stop = rightendpoint(i), step = width(i)/length, length)
range(i::TypedEndpointsInterval{:open,:closed}, length::Integer) = range(i; length)

"""
range(i::OpenInterval; length)
range(i::OpenInterval, length::Integer)
Constructs a range of a specified length with `step = width(i) / (length + 1)`.
# Examples
```jldoctest
julia> range(iv"(1, 4)", 5) # Does not contain the endpoints
1.5:0.5:3.5
julia> range(1, 4, 7)
1.0:0.5:4.0
```
"""
function range(i::TypedEndpointsInterval{:open,:open}; length::Integer)
step = width(i) / (length + 1)
range(leftendpoint(i) + step; step, length)
end

range(i::TypedEndpointsInterval{:open,:open}, length::Integer) = range(i; length)

"""
clamp(t, i::ClosedInterval)
Expand Down
4 changes: 4 additions & 0 deletions test/base_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ end
@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)
@test range(Interval{:open,:closed}(0..1), 10) == range(; stop=1, step=1/10, length=10)
@test range(Interval{:open,:closed}(0..1); length=10) == range(1/10; step=1/10, length=10)
@test range(OpenInterval(0..1), 7) == range(; stop=7/8, step=1/8, length=7)
@test range(OpenInterval(0..1); length=7) == range(1/8; step=1/8, length=7)
end

@testset "clamp" begin
Expand Down

0 comments on commit 56a55ca

Please sign in to comment.