@@ -25,37 +25,184 @@ A subtype of `AbstractInterval{T}` represents an interval subset of type `T`, th
2525"""
2626abstract type AbstractInterval{T} <: Domain{T} end
2727
28+ """
29+ endpoints(d::AI) where AI<:AbstractInterval
30+
31+ A tuple containing the left and right endpoints of the interval.
2832
29- " A tuple containing the left and right endpoints of the interval."
33+ # Examples
34+ ```jldoctest
35+ julia> endpoints(iv"[1,2]")
36+ (1, 2)
37+
38+ julia> endpoints(iv"(2,1)")
39+ (2, 1)
40+ ```
41+ """
3042endpoints (d:: AI ) where AI<: AbstractInterval = error (" Override endpoints(::$(AI) )" )
3143
32- " The left endpoint of the interval."
44+ """
45+ leftendpoint(d::AbstractInterval)
46+
47+ The left endpoint of the interval.
48+
49+ # Examples
50+ ```jldoctest
51+ julia> leftendpoint(iv"[1,2]")
52+ 1
53+
54+ julia> leftendpoint(iv"(2,1)")
55+ 2
56+ ```
57+ """
3358leftendpoint (d:: AbstractInterval ) = endpoints (d)[1 ]
3459
35- " The right endpoint of the interval."
60+ """
61+ rightendpoint(d::AbstractInterval)
62+
63+ The right endpoint of the interval.
64+
65+ # Examples
66+ ```jldoctest
67+ julia> rightendpoint(iv"[1,2]")
68+ 2
69+
70+ julia> rightendpoint(iv"(2,1)")
71+ 1
72+ ```
73+ """
3674rightendpoint (d:: AbstractInterval ) = endpoints (d)[2 ]
3775
38- " A tuple of `Bool`'s encoding whether the left/right endpoints are closed."
76+ """
77+ closedendpoints(d::AI) where AI<:AbstractInterval
78+
79+ A tuple of `Bool`'s encoding whether the left/right endpoints are closed.
80+
81+ # Examples
82+ ```jldoctest
83+ julia> closedendpoints(iv"[1,2]")
84+ (true, true)
85+
86+ julia> closedendpoints(iv"(1,2]")
87+ (false, true)
88+
89+ julia> closedendpoints(iv"[2,1)")
90+ (true, false)
91+
92+ julia> closedendpoints(iv"(2,1)")
93+ (false, false)
94+ ```
95+ """
3996closedendpoints (d:: AI ) where AI<: AbstractInterval = error (" Override closedendpoints(::$(AI) )" )
4097
41- " Is the interval closed at the left endpoint?"
98+ """
99+ isleftclosed(d::AbstractInterval)
100+
101+ Is the interval closed at the left endpoint?
102+
103+ # Examples
104+ ```jldoctest
105+ julia> isleftclosed(iv"[1,2]")
106+ true
107+
108+ julia> isleftclosed(iv"(2,1)")
109+ false
110+ ```
111+ """
42112isleftclosed (d:: AbstractInterval ) = closedendpoints (d)[1 ]
43113
44- " Is the interval closed at the right endpoint?"
114+ """
115+ isrightclosed(d::AbstractInterval)
116+
117+ Is the interval closed at the right endpoint?
118+
119+ # Examples
120+ ```jldoctest
121+ julia> isrightclosed(iv"[1,2]")
122+ true
123+
124+ julia> isrightclosed(iv"(2,1)")
125+ false
126+ ```
127+ """
45128isrightclosed (d:: AbstractInterval ) = closedendpoints (d)[2 ]
46129
47130# open_left and open_right are implemented in terms of closed_* above, so those
48131# are the only ones that should be implemented for specific intervals
49- " Is the interval open at the left endpoint?"
132+ """
133+ isleftopen(d::AbstractInterval)
134+
135+ Is the interval open at the left endpoint?
136+
137+ # Examples
138+ ```jldoctest
139+ julia> isleftopen(iv"[1,2]")
140+ false
141+
142+ julia> isleftopen(iv"(2,1)")
143+ true
144+ ```
145+ """
50146isleftopen (d:: AbstractInterval ) = ! isleftclosed (d)
51147
52- " Is the interval open at the right endpoint?"
148+ """
149+ isrightopen(d::AbstractInterval)
150+
151+ Is the interval open at the right endpoint?
152+
153+ # Examples
154+ ```jldoctest
155+ julia> isrightopen(iv"[1,2]")
156+ false
157+
158+ julia> isrightopen(iv"(2,1)")
159+ true
160+ ```
161+ """
53162isrightopen (d:: AbstractInterval ) = ! isrightclosed (d)
54163
55- # Only closed if closed at both endpoints, and similar for open
164+ """
165+ isclosedset(d::AbstractInterval)
166+
167+ Is the interval closed set?
168+
169+ # Examples
170+ ```jldoctest
171+ julia> isclosedset(iv"[1,2]")
172+ true
173+
174+ julia> isclosedset(iv"(1,2]")
175+ false
176+
177+ julia> isclosedset(iv"[1,2)")
178+ false
179+
180+ julia> isclosedset(iv"(1,2)")
181+ false
182+ ```
183+ """
56184isclosedset (d:: AbstractInterval ) = isleftclosed (d) && isrightclosed (d)
57185
58- " Is the interval open?"
186+ """
187+ isopenset(d::AbstractInterval)
188+
189+ Is the interval open set?
190+
191+ # Examples
192+ ```jldoctest
193+ julia> isopenset(iv"[1,2]")
194+ false
195+
196+ julia> isopenset(iv"(1,2]")
197+ false
198+
199+ julia> isopenset(iv"[1,2)")
200+ false
201+
202+ julia> isopenset(iv"(1,2)")
203+ true
204+ ```
205+ """
59206isopenset (d:: AbstractInterval ) = isleftopen (d) && isrightopen (d)
60207
61208eltype (:: Type{AbstractInterval{T}} ) where {T} = T
@@ -94,6 +241,15 @@ mean(d::AbstractInterval) = (leftendpoint(d) + rightendpoint(d))/2
94241
95242Calculate the width (max-min) of interval `iv`. Note that for integers
96243`l` and `r`, `width(l..r) = length(l:r) - 1`.
244+
245+ # Examples
246+ ```jldoctest
247+ julia> width(2..7)
248+ 5
249+
250+ julia> length(2:7)
251+ 6
252+ ```
97253"""
98254function width (A:: AbstractInterval )
99255 _width = rightendpoint (A) - leftendpoint (A)
@@ -206,28 +362,61 @@ range(i::TypedEndpointsInterval{:closed,:closed,I}) where {I<:Integer} = UnitRan
206362
207363"""
208364 range(i::ClosedInterval; step, length)
209- range(i::ClosedInterval, len ::Integer)
365+ range(i::ClosedInterval, length ::Integer)
210366
211367Constructs a range of a specified step or length.
368+
369+ # Examples
370+ ```jldoctest
371+ julia> range(1..2, 8)
372+ 1.0:0.14285714285714285:2.0
373+
374+ julia> range(1, 2, 8)
375+ 1.0:0.14285714285714285:2.0
376+
377+ julia> range(1..2, step=0.2)
378+ 1.0:0.2:2.0
379+
380+ julia> range(1, 2, step=0.2)
381+ 1.0:0.2:2.0
382+ ```
212383"""
213384range (i:: TypedEndpointsInterval{:closed,:closed} ; step= nothing , length= nothing ) =
214385 range (leftendpoint (i); stop= rightendpoint (i), step= step, length= length)
215- range (i:: TypedEndpointsInterval{:closed,:closed} , len :: Integer ) = range (i; length= len )
386+ range (i:: TypedEndpointsInterval{:closed,:closed} , length :: Integer ) = range (i; length= length )
216387
217388"""
218389 range(i::Interval{:closed,:open}; length)
219- range(i::Interval{:closed,:open}, len ::Integer)
390+ range(i::Interval{:closed,:open}, length ::Integer)
220391
221392Constructs a range of a specified length with `step=width(i)/length`.
393+
394+ # Examples
395+ ```jldoctest
396+ julia> range(iv"[1, 2)", 7) # Does not contain right endpoint
397+ 1.0:0.14285714285714285:1.8571428571428572
398+
399+ julia> range(1, 2, 8)
400+ 1.0:0.14285714285714285:2.0
401+ ```
222402"""
223403range (i:: TypedEndpointsInterval{:closed,:open} ; length:: Integer ) =
224404 range (leftendpoint (i); step= width (i)/ length, length= length)
225- range (i:: TypedEndpointsInterval{:closed,:open} , len :: Integer ) = range (i; length= len )
405+ range (i:: TypedEndpointsInterval{:closed,:open} , length :: Integer ) = range (i; length= length )
226406
227407"""
228408 clamp(t, i::ClosedInterval)
229409
230410Clamp the scalar `t` such that the result is in the interval `i`.
411+
412+ # Examples
413+ ```jldoctest
414+ julia> clamp(1.2, 1..2)
415+ 1.2
416+
417+ julia> clamp(2.2, 1..2)
418+ 2.0
419+ ```
231420"""
232421clamp (t, i:: TypedEndpointsInterval{:closed,:closed} ) =
233422 clamp (t, leftendpoint (i), rightendpoint (i))
0 commit comments