Ranges
Module ranges
import ranges
| Module | ranges |
| Source | ranges/module-docs.zirr, ranges/ranges.zirr |
Set operations over
Range,ClosedRangeandOpenRange.
The three range types themselves live in prelude — they are iterable and countable, so for i <- Range(0, 10) works without importing anything. ranges adds the operations that treat a range as a set of integers.
Like is the union of all three, so every function here accepts any of them, in any combination. Results come back as prelude.ClosedRange, the one form that can describe any non-empty span exactly, wrapped in an prelude.Option because the answer may be empty.
Contents
Unions
Like
`ranges/ranges.zirr:3`
union Like {
Range
ClosedRange
OpenRange
}
Cases
| Case | Interpretation |
|---|---|
Range |
The integers from start up to but excluding end, written [start, end). Range(0, 3) iterates 0, 1, 2. This is the form to reach for by default: it counts end - start values, so it pairs with a length. |
ClosedRange |
The integers from start to end, both included, written [start, end]. ClosedRange(0, 3) iterates 0, 1, 2, 3. It is the only form that can name any non-empty span exactly, which is why ranges returns it. |
OpenRange |
The integers strictly between start and end, both excluded, written (start, end). OpenRange(0, 3) iterates 1, 2. |
Functions
contains
`ranges/ranges.zirr:10`
fn contains(r: Like, value: Int) -> Bool
Returns whether value lies within r.
intersect
`ranges/ranges.zirr:48`
fn intersect(a: Like, b: Like) -> Option
Returns the values a and b have in common, or None if they don’t overlap.
merge
`ranges/ranges.zirr:71`
fn merge(a: Like, b: Like) -> Option
Combines a and b into a single range spanning both, if they overlap or are adjacent (e.g. [1,3] and [4,6] merge into [1,6]). Returns None if there’s a gap between them, since the result wouldn’t be a single contiguous range.
overlap
`ranges/ranges.zirr:43`
fn overlap(a: Like, b: Like) -> Bool
Returns whether a and b share any values.
toClosedRange
`ranges/ranges.zirr:22`
fn toClosedRange(r: Like) -> Option
Returns r’s equivalent closed, inclusive bounds, or None if r contains no integers (e.g. Range(5, 5), or OpenRange(5, 6)).