-
Notifications
You must be signed in to change notification settings - Fork 18
Additional accessor and convert functions #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
e39e57f
b159390
852227f
130d689
7266148
6cf3009
a270c77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,8 @@ export Bound, | |
HB, | ||
first, | ||
last, | ||
containsfirst, | ||
containslast, | ||
span, | ||
bounds_types, | ||
isclosed, | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -120,6 +120,8 @@ Base.first(interval::Interval) = interval.first | |||||
Base.last(interval::Interval) = interval.last | ||||||
isclosed(interval::AbstractInterval{T,L,R}) where {T,L,R} = L === Closed && R === Closed | ||||||
Base.isopen(interval::AbstractInterval{T,L,R}) where {T,L,R} = L === Open && R === Open | ||||||
containsfirst(interval::AbstractInterval{T,L,R}) where {T,L,R} = L === Closed | ||||||
containslast(interval::AbstractInterval{T,L,R}) where {T,L,R} = R === Closed | ||||||
|
||||||
""" | ||||||
span(interval::AbstractInterval) | ||||||
|
@@ -137,6 +139,10 @@ function Base.convert(::Type{T}, i::Interval{T}) where T | |||||
throw(DomainError(i, "The interval is not closed with coinciding endpoints")) | ||||||
end | ||||||
|
||||||
function Base.convert(::Type{Interval{T,L,R}}, interval::Interval{N,L,R}) where {T,N,L,R} | ||||||
return Interval{L,R}(convert(T, first(interval)), convert(T, last(interval))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
end | ||||||
|
||||||
# Date/DateTime attempt to convert to Int64 instead of falling back to convert(T, ...) | ||||||
Dates.Date(interval::Interval{Date}) = convert(Date, interval) | ||||||
Dates.DateTime(interval::Interval{DateTime}) = convert(DateTime, interval) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,7 @@ isinf(::TimeType) = false | |
@test_throws DomainError convert(Int, Interval{Closed, Open}(10, 10)) | ||
@test convert(Int, Interval{Closed, Closed}(10, 10)) == 10 | ||
@test_throws DomainError convert(Int, Interval{Closed, Closed}(10, 11)) | ||
@test convert(Interval{Float64, Closed, Closed}, Interval(1,2)) == Interval{Float64, Closed, Closed}(1.0,2.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it also be useful to include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would make more sense as a constructor: https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Conversion-vs.-Construction-1. I'd probably leave this out unless you have a use case. |
||
|
||
for T in (Date, DateTime) | ||
dt = T(2013, 2, 13) | ||
|
@@ -94,6 +95,8 @@ isinf(::TimeType) = false | |
@test span(interval) == b - a | ||
@test isclosed(interval) == (L === Closed && R === Closed) | ||
@test isopen(interval) == (L === Open && R === Open) | ||
@test containsfirst(interval) == (L === Closed) | ||
@test containslast(interval) == (R === Closed) | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this functionality is useful but the name needs more consideration. There may be a rename of first/last in the near future: #90