Skip to content

Commit 1c652f8

Browse files
authored
Merge pull request #17 from JuliaMath/teh/range
Support range(::ClosedInterval{<:Integer}) -> UnitRange
2 parents b4c9ac8 + ad35645 commit 1c652f8

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ os:
55
- osx
66
julia:
77
- 0.5
8+
- 0.6
89
- nightly
910
notifications:
1011
email: false

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ environment:
22
matrix:
33
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
44
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
5+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
6+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
57
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
68
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
79

src/IntervalSets.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ module IntervalSets
55
# package code goes here
66

77
using Base: @pure
8-
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum
8+
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash, union, intersect, minimum, maximum, range
9+
if isdefined(Main, :)
10+
import Base:
11+
end
912

1013
using Compat
1114

@@ -27,9 +30,6 @@ function convert{I<:AbstractInterval}(::Type{I}, r::Range)
2730
T = eltype(I)
2831
I(convert(T, minimum(r)), convert(T, maximum(r)))
2932
end
30-
function convert{R<:AbstractUnitRange,I<:Integer}(::Type{R}, i::AbstractInterval{I})
31-
R(minimum(i), maximum(i))
32-
end
3333

3434
ordered{T}(a::T, b::T) = ifelse(a < b, (a, b), (b, a))
3535
ordered(a, b) = ordered(promote(a, b)...)

src/closed.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,9 @@ function width{T}(A::ClosedInterval{T})
7676
_width = A.right - A.left
7777
max(zero(_width), _width) # this works when T is a Date
7878
end
79+
80+
function convert{R<:AbstractUnitRange,I<:Integer}(::Type{R}, i::ClosedInterval{I})
81+
R(minimum(i), maximum(i))
82+
end
83+
84+
range{I<:Integer}(i::ClosedInterval{I}) = convert(UnitRange{I}, i)

test/runtests.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ using Base.Test
1010
@test_throws ArgumentError :a .. "b"
1111
I = 0..3
1212
@test string(I) == "0..3"
13-
@test convert(UnitRange, I) === 0:3
14-
@test convert(UnitRange{Int16}, I) === Int16(0):Int16(3)
13+
@test @inferred(convert(UnitRange, I)) === 0:3
14+
@test @inferred(range(I)) === 0:3
15+
@test @inferred(convert(UnitRange{Int16}, I)) === Int16(0):Int16(3)
1516
J = 3..2
1617
K = 5..4
1718
L = 3 ± 2
18-
M = ClosedInterval(2, 5.0)
19+
M = @inferred(ClosedInterval(2, 5.0))
1920
@test string(M) == "2.0..5.0"
20-
N = ClosedInterval(UInt8(255), 300)
21-
O = CartesianIndex(1, 2, 3, 4) ± 2
21+
N = @inferred(ClosedInterval(UInt8(255), 300))
22+
O = @inferred(CartesianIndex(1, 2, 3, 4) ± 2)
2223
@test O == (-1..3, 0..4, 1..5, 2..6)
2324

2425
@test eltype(I) == Int
2526
@test eltype(M) == Float64
26-
@test convert(ClosedInterval{Float64}, I) === 0.0..3.0
27-
@test convert(ClosedInterval{Float64}, 0:3) === 0.0..3.0
27+
@test @inferred(convert(ClosedInterval{Float64}, I)) === 0.0..3.0
28+
@test @inferred(convert(ClosedInterval{Float64}, 0:3)) === 0.0..3.0
2829
@test !(convert(ClosedInterval{Float64}, I) === 0..3)
2930
@test ClosedInterval{Float64}(1,3) === 1.0..3.0
3031
@test ClosedInterval(0.5..2.5) === 0.5..2.5
@@ -49,8 +50,8 @@ using Base.Test
4950
@test 2 in I
5051
@test 1..2 in 0.5..2.5
5152

52-
@test I L == ClosedInterval(0, 5)
53-
@test I L == ClosedInterval(1, 3)
53+
@test @inferred(I L) == ClosedInterval(0, 5)
54+
@test @inferred(I L) == ClosedInterval(1, 3)
5455
@test isempty(J K)
5556
@test isempty((2..5) (7..10))
5657
@test isempty((1..10) (7..2))

0 commit comments

Comments
 (0)