r-k-b / elm-interval / Interval

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

Types


type Interval

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 alias Bound =
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.

Deprecated, use Bound.Bound directly.

Constructors

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

interval : Bound -> Bound -> Interval

Constructs an Interval from two Bounds.

If either of the bounds is NaN the Interval will be empty.

degenerate : Basics.Float -> Interval

A degenerate Interval.

empty : Interval

An empty Interval.

lowerBounded : Bound -> Interval

Convenience function for a lower-bounded Interval (from some n to +∞ included).

upperBounded : Bound -> Interval

Convenience function for a upper-bounded Interval (from -∞ included to some n).

unbounded : Interval

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

Operations on Intervals

hull : Interval -> Interval -> Interval

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.

intersection : Interval -> Interval -> Interval

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

intervalToString : Interval -> String

Return a String representation of an Interval.

interior : Interval -> 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)

closure : Interval -> Interval

Returns the smallest closed interval containing a.

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

subtract : Interval -> Interval -> List Interval

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

E.g.:

upperBoundValue : Interval -> Maybe Basics.Float

Extract the value of the upper bound of an Interval.

lowerBoundValue : Interval -> Maybe Basics.Float

Extract the value of the lower bound of an Interval.

Tests on Intervals

adjoins : Interval -> Interval -> Basics.Bool

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
    ]

intersects : Interval -> Interval -> Basics.Bool

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 -> Basics.Float -> Basics.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
    ]

isBounded : Interval -> Basics.Bool

Does this interval have finite bounds?

isDegenerate : Interval -> Basics.Bool

Is this a degenerate (point-valued) interval?

isEmpty : Interval -> Basics.Bool

Is this an empty interval?

isUnbounded : Interval -> Basics.Bool

Is this interval unbounded?

isLowerBounded : Interval -> Basics.Bool

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

isUpperBounded : Interval -> Basics.Bool

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

isLowerInclusive : Interval -> Basics.Bool

Is the lower bound of this interval inclusive?

isUpperInclusive : Interval -> Basics.Bool

Is the upper bound of this interval inclusive?

isLowerExclusive : Interval -> Basics.Bool

Is the lower bound of this interval exclusive?

isUpperExclusive : Interval -> Basics.Bool

Is the upper bound of this interval exclusive?

lowerBound : Interval -> Maybe Bound

Returns the lower (left) bound of the interval, returns Nothing if it's empty.

lowerBound (interval (includes 0) (excludes 1)) == Just (includes 0)

upperBound : Interval -> Maybe Bound

Returns the upper (right) bound of the interval, returns Nothing if it's empty.

upperBound (interval (includes 0) (excludes 1)) == Just (excludes 1)

Arithmetic operations

plus : Interval -> Interval -> Interval

Arithmetically add two intervals. Given two intervals a and b returns an interval that contains all the number that can be obtained by adding a number from the first interval to a number from the second interval.

negate : Interval -> Interval

Arithmetically negate an interval.

minus : Interval -> Interval -> Interval

Arithmetically subtracts two intervals. Given two intervals a and b returns an interval that contains all the number that can be obtained by subtracting a number from the second interval from a number from the first interval.

Deprecated functions

excludes : Basics.Float -> Bound

An exclusive endpoint of an interval.

Deprecated, use Bound.Exclusive directly.

includes : Basics.Float -> Bound

An inclusive endpoint of an interval.

Deprecated, use Bound.Inclusive directly.

leftBounded : Bound -> Interval

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

Deprecated, use lowerBounded.

rightBounded : Bound -> Interval

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

Deprecated, user upperBounded.

isLeftBounded : Interval -> Basics.Bool

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

Deprecated, use isLowerBounded.

isRightBounded : Interval -> Basics.Bool

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

Deprecated, use isUpperBounded.

isLeftOpen : Interval -> Basics.Bool

Is the lower bound of this interval open (exclusive)?

Deprecated, use isLowerExclusive.

isRightOpen : Interval -> Basics.Bool

Is the upper bound of this interval open (exclusive)?

Deprecated, use isUpperExclusive.

Related reading