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

Iterator interface on IntervalSet

Open ahjulstad opened this issue 1 year ago • 1 comments

Any reason not to implement the iterator interface on the intervalset?

I wanted to do things like

sum( (span(s) for s in u) )

but that doesn't work, I need to explicitly convert like this using

sum( (span(s) for s in convert(Array, u)) )

As the convert function is intended to be called implicitly; it should still be the same data, it feels like u and convert(Array, u) should be very similar, but it doesn't.

The following three lines is all the is needed. Does it make sense to add?

Base.IteratorSize(i::Intervals.IntervalSet) = Base.SizeUnknown()
Base.iterate(i::Intervals.IntervalSet) = Base.iterate(i.items)
Base.iterate(i::Intervals.IntervalSet, state) = Base.iterate(i.items, state)

ahjulstad avatar Oct 17 '23 13:10 ahjulstad

@ahjulstad maybe submitting a PR will get the dev's attention. i'd find such an iterator very useful!

bjarthur avatar Jan 02 '24 23:01 bjarthur