Construct an interval with the given minimum and maximum values. They should be provided in the correct order but will be flipped if necessary to result in a valid interval.
exampleInterval =
Interval.with { minValue = -1, maxValue = 5 }Construct a zero-width interval containing a single value.
Interval.singleton 3
--> Interval.with { minValue = 3, maxValue = 3 }Construct an interval containing both of the given intervals.
firstInterval =
Interval.with { minValue = 1, maxValue = 2 }
secondInterval =
Interval.with { minValue = 3, maxValue = 6 }
Interval.hull firstInterval secondInterval
--> Interval.with { minValue = 1, maxValue = 6 }Attempt to construct an interval containing all the values common to both
given intervals. If the intervals do not intersect, returns Nothing.
firstInterval =
Interval.with { minValue = 1, maxValue = 3 }
secondInterval =
Interval.with { minValue = 2, maxValue = 5 }
thirdInterval =
Interval.with { minValue = 4, maxValue = 7 }
Interval.intersection firstInterval secondInterval
--> Just (Interval.with { minValue = 2, maxValue = 3 })
Interval.intersection firstInterval thirdInterval
--> NothingIf the two intervals just touch, a singleton interval will be returned:
Interval.intersection
(Interval.with { minValue = 1, maxValue = 2 })
(Interval.with { minValue = 2, maxValue = 3 })
--> Just (Interval.with { minValue = 2, maxValue = 2})Construct an interval containing all of the intervals in the given list. If
the list is empty, returns Nothing.
Interval.hullOf
[ Interval.singleton 2
, Interval.with { minValue = 3, maxValue = 4 }
]
--> Just (Interval.with { minValue = 2, maxValue = 4 })
Interval.hullOf []
--> NothingGet the minimum and maximum values of an interval as a single record. Useful when combined with destructuring:
{ minValue, maxValue } =
Interval.extrema exampleInterval
--> minValue = -1
--> maxValue = 5Get the minimum value of an interval.
Interval.minValue exampleInterval
--> -1Get the maximum value of an interval.
Interval.maxValue exampleInterval
--> 5Get the midpoint of an interval.
Interval.midpoint exampleInterval
--> 2Get the width of an interval.
Interval.width exampleInterval
--> 6Interpolate an interval from its minimum to its maximum value; a value of 0.0 corresponds to the minimum value of the interval, a value of 0.5 corresponds to its midpoint and a value of 1.0 corresponds to its maximum value. Values less than 0.0 or greater than 1.0 can be used to extrapolate.
Interval.interpolate 0 exampleInterval
--> -1
Interval.interpolate 0.75 exampleInterval
--> 3.5
Interval.interpolate -0.5 exampleInterval
--> -4Get the image of sin(x) applied on the interval.
Interval.with { minValue = -pi / 2, maxValue = pi / 2}
|> Interval.sin
--> Interval.with { minValue = -1, maxValue = 1 }Get the image of cos(x) applied on the interval.
Interval.with { minValue = -pi / 2, maxValue = pi / 2}
|> Interval.cos
--> Interval.with { minValue = 0, maxValue = 1 }Check if an interval contains a given value.
Interval.contains 0 exampleInterval
--> True
Interval.contains 10 exampleInterval
--> FalseThe minimum and maximum values of an interval are considered to be contained in the interval, although if you are relying on this then there is a good chance your code is vulnerable to numerical roundoff!
Interval.contains -1 exampleInterval
--> TrueCheck if two intervals touch or overlap (have any values in common).
Interval.with { minValue = 0, maxValue = 10 }
|> Interval.intersects exampleInterval
--> True
Interval.with { minValue = 10, maxValue = 20 }
|> Interval.intersects exampleInterval
--> FalseIntervals that just touch each other are considered to intersect (this is
consistent with intersection which will return a zero-width interval for the
intersection of two just-touching intervals):
Interval.with { minValue = -3, maxValue = -1 }
|> Interval.intersects exampleInterval
--> TrueAs with contains, though, if you are relying on this then there is a good
chance your code is vulnerable to numerical roundoff!
Check if the second interval is fully contained in the first.
-- Note that here exampleInterval is actually the
-- *second* interval due to how |> works
exampleInterval
|> Interval.isContainedIn
(Interval.with
{ minValue = 0
, maxValue = 10
}
)
--> False
exampleInterval
|> Interval.isContainedIn
(Interval.with
{ minValue = -10
, maxValue = 10
}
)
--> True
Represents a bounded interval with a minimum and maximum value, for example the interval from 3 to 5.