-
Notifications
You must be signed in to change notification settings - Fork 2
Add intersect_spans function
#72
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
Changes from 9 commits
dec62b3
1e6e53d
b8a37f4
c89d6a0
52715f5
934eab0
1c72dbd
9c26ca2
69e0134
0bcfd50
0dd7b07
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,8 @@ using Statistics | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| export TimeSpan, start, stop, istimespan, translate, overlaps, | ||||||||||||||||||||||
| shortest_timespan_containing, duration, index_from_time, | ||||||||||||||||||||||
| time_from_index, merge_spans!, merge_spans, invert_spans | ||||||||||||||||||||||
| time_from_index, merge_spans!, merge_spans, invert_spans, | ||||||||||||||||||||||
| intersect_spans | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const NS_IN_SEC = Dates.value(Nanosecond(Second(1))) # Number of nanoseconds in one second | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -399,6 +400,18 @@ function invert_spans(spans, parent_span) | |||||||||||||||||||||
| return gaps | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| intersect_spans(span_1, span_2) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Returns a timespan that consists of the intersection of two timespans. | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
chetchavat marked this conversation as resolved.
|
||||||||||||||||||||||
| function intersect_spans(span_1, span_2) | ||||||||||||||||||||||
| overlaps(span_1, span_2) || throw(ArgumentError("provided spans must overlap")) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| spans = [span_1, span_2] | ||||||||||||||||||||||
| return TimeSpan(maximum(start, spans), minimum(stop, spans)) | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
Member
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. IMO it's a bit clearer not to express things as array reductions on a two-element array. Also, the other functions in this package that operate on two span arguments call the arguments
Suggested change
Member
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 agree, |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ##### | ||||||||||||||||||||||
| ##### Package extensions (TODO: remove this section once we require Julia 1.9+) | ||||||||||||||||||||||
| ##### | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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 wonder whether "intersect" is really the right verb here. Describing it as the intersection of the two spans makes sense, but it kind of suggests to me that other set terminology would be applicable, but I don't believe there are any such cases. For example, TimeSpans.jl uses
overlaps(a, b)rather than!isdisjoint(a, b), andTimeSpans.contains(a, b)rather thanissubset(b, a). I could imagine that given that the query for "is there a nonempty intersection" isoverlaps, "what is the intersection" would beoverlap. But ifoverlapreturns a span, it's not immediately obvious whetheroverlapsthen is plural or present tense. I have no answers, only quandaries.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.
if it helps any, the original function was called
innerspanThere 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.
this is really the set-theoretic operation though, (other than the error-if-disjoint lol).
my gripe with
intersect_spansis the other part, the_spans: the other_spansfunctions operate on iterables of spans rather than pairs; the pairwiseoverlaps,containsdon't have_spans.I think
TimeSpans.intersectis fine, it has the same semantics as the base function and not exporting it makes the scope clear a lacontainsThere 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'll also add that
Base.containstends to come along with other collection-y methods, but the namespacing makes it pretty clear that it's differentThere 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.
Very, very late to the party but IMO
TimeSpans.containsshould be renamed tocontains_span. (Actually, in basically all code I've written where I've needed that function, I dousing TimeSpans: contains as contains_span, haha.) Having only part of the API namespaced feels inconsistent, and having a package with a function that has the same name as a Base function is really confusing. I strongly preferintersect_spansoverTimeSpans.intersect.