Intervals.jl icon indicating copy to clipboard operation
Intervals.jl copied to clipboard

Cannot compute intersect of multiple intervals

Open glennmoy opened this issue 3 years ago • 2 comments

Base.intersect can accept multiple arguments, but Intervals.intersect only accepts two. This is limiting if one wants to compute the intersection of multiple intervals at the same time.

MWE

julia> intersect(1..10, 1..3, 1..6)
ERROR: MethodError: no method matching length(::Interval{Int64, Closed, Closed})
Closest candidates are:
  length(::Union{Base.KeySet, Base.ValueIterator}) at abstractdict.jl:58
  length(::Union{LinearAlgebra.Adjoint{T, S}, LinearAlgebra.Transpose{T, S}} where {T, S}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/adjtrans.jl:195
  length(::Union{DataStructures.OrderedRobinDict, DataStructures.RobinDict}) at /Users/glenn/.julia/packages/DataStructures/ixwFs/src/ordered_robin_dict.jl:86
  ...
Stacktrace:
 [1] union!(s::Set{Int64}, itr::Interval{Int64, Closed, Closed})
   @ Base ./abstractset.jl:89
 [2] Set{Int64}(itr::Interval{Int64, Closed, Closed})
   @ Base ./set.jl:10
 [3] _Set(itr::Interval{Int64, Closed, Closed}, #unused#::Base.HasEltype)
   @ Base ./set.jl:23
 [4] Set(itr::Interval{Int64, Closed, Closed})
   @ Base ./set.jl:21
 [5] _shrink(shrinker!::Function, itr::Interval{Int64, Closed, Closed}, itrs::Tuple{Interval{Int64, Closed, Closed}, Interval{Int64, Closed, Closed}})
   @ Base ./array.jl:2607
 [6] intersect(::Interval{Int64, Closed, Closed}, ::Interval{Int64, Closed, Closed}, ::Interval{Int64, Closed, Closed})
   @ Base ./array.jl:2611
 [7] top-level scope
   @ REPL[128]:1

Compared to Base.intersect

julia> intersect(1:10, 1:3, 1:6)
1:3

glennmoy avatar Jun 02 '21 14:06 glennmoy

Work around if you want one:

julia> foldl(intersect, [1..10, 1..3, 1..6])
Interval{Int64, Closed, Closed}(1, 3)

omus avatar Jun 02 '21 14:06 omus

thanks! this came up during a POC discussion so it's not blocking but I might try my hand at the fix during the week if I get the chance.

glennmoy avatar Jun 02 '21 14:06 glennmoy