Ranges

Module ranges

import ranges

Set operations over Range, ClosedRange and OpenRange.

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 Represents an open range of integers from start (inclusive) to end (exclusive).
ClosedRange Represents a closed range of integers from start (inclusive) to end (inclusive).
OpenRange Represents an open range of integers strictly between start and end (both exclusive).

Functions

contains

`ranges/ranges.zirr:10`
fn contains(r: Like, value: Int) -> Bool

Returns whether value lies within r.


intersect

`ranges/ranges.zirr:49`
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:74`
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:44`
fn overlap(a: Like, b: Like) -> Bool

Returns whether a and b share any values.


toClosedRange

`ranges/ranges.zirr:23`
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)).