I don't know if this was previously discussed and dismissed but ...

Inspired by some recent discussion in the "comparing inexact values" thread plus some temporal discussion and some older thoughts ...

I was thinking that Perl 6 ought to have a generic interval type that is conceptually like Range, in that it is defined using a pair of values of an ordered type and includes all the values between those, but unlike Range that type is not expected to have discrete consecutive values that can be iterated over.

I'm thinking of a Range-alike that one could use with Rat|Num or Instant etc, and not just Int etc. There would be operators to test membership of a value in the interval, and set-like operators to compare or combine intervals, such as is_inside, is_subset, is_overlap, union, intersection, etc. Such an interval would be what you use for inexact matching and would be the result of a ± infix operator or % postfix operator. Also, as Range has a .. constructor, this other type should have something.

Now, ideally the type would also support discontinuous ranges, or maybe it would be best to have 2 types, one that is a single continuous range and the other which is a set of the first and supports discontinuous ranges. So, union|intersection etc would generally only work with the discontinuous version but the other operators could work with the continuous ones. In any event I only had closed-ended intervals in mind, and not open-ended ones.

I'm thinking that at least the simpler continuous version of the range should be in core since it is generic in the way that Range is generic, and it is simple, and could be widely usable in common contexts such as fuzzy matching of numerics and instants. The noncontinuous version is less important, though quite useful.

Barring a better name coming along, I suggest calling the type "Interval", and it would be immutable.

As for determination of where ranges overlap or not, eg whether a matching start and end point counts as an overlap or just an adjacency, this could be customizable (eg, an overlap if the ordered type is discrete like Int, and an adjacency if it is continuous like Rat|Num) unless there are general rules best applied generally. I cite the "comparing inexact values" for prior discussion of these matters, that don't necessarily need repeating.

-- Darren Duncan

Reply via email to