r-k-b/elm-interval/2.1.0

NOTE: the latest version is 3.0.0

Interval

A representation of numeric intervals (also known as ranges.)

Types

An interval over the reals. May be over either the Ordinary Reals (-∞, +∞) or the Extended Reals [-∞, +∞]. Bounded x y will always satisfy x < y. (x == y is either degenerate or empty)

Opaque type.

type Bound

Represents an upper or lower, closed or open endpoint of an Interval. This encompasses the "endpoints" of unbounded intervals when the bound value is either of the Infinity values in the floating point spec.

Opaque type.

Constructors

interval is the primary constructor; the others are just for convenience.

Constructs an Interval from two Bounds.

degenerate : Float -> Interval

A degenerate Interval.

An empty Interval.

Convenience function for a left-bounded Interval (from some n to +∞)

Convenience function for a right-bounded Interval (from -∞ to some n)

An unbounded Interval over the Extended Reals. [-∞, +∞]

Endpoint (Bound) constructors

excludes : Float -> Bound

An exclusive endpoint of an interval.

includes : Float -> Bound

An inclusive endpoint of an interval.

Operations on Intervals

The convex hull of two intervals. This is similar to union in that it includes all the points of the component intervals, and for non-overlapping intervals, the points between them.

The intersection of two intervals. If the intervals overlap, this is the common part. If not, this is the empty interval.

Return a String representation of an Interval.

Returns the largest open interval contained within a.

-- `interior([x, y]) == (x, y)`
interior (interval (includes 0) (includes 2)) == interval (excludes 0) (excludes 2)

Returns the smallest closed interval containing a.

-- `closure((x, y)) == [x, y]`
interior (interval (excludes 0) (excludes 2)) == interval (includes 0) (includes 2)

Subtract interval b from interval a, returning a list of the parts of a that did not intersect with b.

E.g.:

  • [1, 3) - (1, 2] = [ {1}, (2, 3) ]
upperBoundValue : Interval -> Maybe Float

Extract the value of the upper bound of an Interval.

lowerBoundValue : Interval -> Maybe Float

Extract the value of the lower bound of an Interval.

Tests on Intervals

Are these two intervals adjoins? I.e., do they share an upper-lower or lower-upper bound, exactly one of which is closed, and do not intersect each other?

let
    a = interval (includes 1) (excludes 3)  -- [1, 3)
    b = interval (includes 2) (includes 4)  -- [3, 4]
    c = interval (excludes 3) (includes 4)  -- (3, 4]
    d = interval (includes 2) (includes 3)  -- [2, 3]
in
    [ adjoins a b = True
    , adjoins a c = False
    , adjoins a d = False
    ]

Do these two intervals intersect?

let
    a = interval (includes 1) (excludes 3)
    b = interval (includes 2) (includes 4)
    c = interval (includes 3) (includes 4)
in
    [ intersects a b = True
    , intersects a c = False
    ]
intersectsPoint : Interval -> Float -> Bool

Does this interval contain the given point?

let
    a = interval (includes 1) (excludes 3)
in
    [ intersectsPoint a 0 = False
    , intersectsPoint a 1 = True
    , intersectsPoint a 3 = False
    ]

Does this interval have finite bounds?

Is this a degenerate (point-valued) interval?

isEmpty : Interval -> Bool

Is this an empty interval?

Does this interval have a finite lower bound, and an infinite upper bound?

Does this interval have a finite upper bound, and an infinite lower bound?

Is the lower bound of this interval open?

Is the upper bound of this interval open?

Related reading