package lunar

  1. Overview
  2. Docs

Module Time.RangeSource

type time := t
include Range.S with type elt = t

Types

Sourcetype elt = t

The type that describes the elements of a range.

Sourcetype t

The type describing a range.

Sourcetype iterator

A range has no concept of an iterator; it is simply characterized by a first and a last element. An iterator, therefore, is a pair of functions for moving from one elt to another (pred and succ, respectively).

Creating Range

Sourceval make : first:elt -> last:elt -> t

make ~first ~last constructs a range with first as the first element and last as the last element.

You'll notice that a range may not be in ascending order (hence the names first and last).

Facts about Range

Sourceval first_elt : t -> elt

first_elt r returns the first element of the range r.

Sourceval last_elt : t -> elt

last_elt r returns the last element of the range r.

Sourceval is_ascending : t -> bool

is_ascending r returns true if first r <= last r , false otherwise. Singleton ranges are both ascending and descending.

Sourceval is_descending : t -> bool

is_descending r returns true if last r <= first r , false otherwise. Singleton ranges are both ascending and descending.

Sourceval is_singleton : t -> bool

is_singleton returns true if last = first.

Sourceval min_elt : t -> elt

min_elt r returns the smallest elt for the given range r.

Sourceval max_elt : t -> elt

max_elt r returns the greatest elt for the given range r.

Sourceval bounds : t -> elt * elt

bounds r is min_elt r, max_elt r.

Sourceval contains : elt -> t -> bool

contains elt r returns true if the given element elt is included in the range r.

Sourceval mem : elt -> t -> bool

mem is an alias for contains.

Modifying Ranges

Sourceval rev : t -> t

rev r swaps the endpoints of r.

Sourceval sort : t -> t

sort r sorts the range r in ascending order.

Equalities

The concept of comparing ranges is ambiguous, which is why the API only exposes range equality operations.

Sourceval equal : t -> t -> bool

equal a b returns true if a and b have the same first and last elements.

Relations between ranges

Sourceval overlaps : t -> t -> bool

overlaps r1 r2 returns true if the two ranges share at least one common element.

Sourceval disjoint : t -> t -> bool

disjoint r1 r2 returns true if r1 and r2 share no common element.

Sourceval includes : t -> t -> bool

includes parent child returns true if every element of child is contained in parent.

Operation on ranges

Sourceval shift : (elt -> elt) -> t -> t

shift f r moves the entire range forward (or backward) by performing f on first_elt and last_elt.

Sourceval map : (elt -> elt) -> t -> t

map f r is shift f r. (See shift)

Sourceval span : t -> t -> t

span ra rb returns the smallest ascending range that contains both ra and rb. The resulting range will always be ascending.

Sourceval intersection : t -> t -> t option

intersection a b returns the common portion of a and b, or None if they are disjoint.

The result is always ascending.

Sourceval clamp : within:t -> t -> t option

clamp ~within r restricts r to the bounds of within. (An alias of intersection).

Iterators

Ranges only capture endpoints. Therefore, enumerating the possible values of an interval comes into play only later.

Sourceval iterator : pred:(elt -> elt) -> succ:(elt -> elt) -> iterator

iterator ~pred ~succ creates an iterator with a pair of functions pred, succ.

Sourceval linear_iterator : (int -> elt -> elt) -> int -> iterator

linear_iterator f n creates an iterator for a function that takes a quantity as an argument.

The following functions include an include_boundaries parameter. This is primarily because iterators can skip the last element of an iteration. For example, suppose we have a range of dates and decide to iterate over two days. It is possible that the last element will be skipped. The include_boundaries parameter always includes the last element in iterations (and defaults to true).

Sourceval fold_left : ?include_boundaries:bool -> iterator:iterator -> (elt -> 'acc -> 'acc) -> 'acc -> t -> 'acc

fold_left ?include_boundaries ~iterator f acc range folds over the elements of range from the first element to the last element, applying f to each element and an accumulator acc. The order of iteration respects the range direction: if the range is descending, elements are traversed from first to last in descending order.

  • iterator must provide a succ and pred function for moving between elements.
  • include_boundaries (default true) controls whether the last element is included if the iterator would overshoot the end.
  • f is applied to each element and the current accumulator, returning a new accumulator.
Sourceval fold_right : ?include_boundaries:bool -> iterator:iterator -> (elt -> 'acc -> 'acc) -> 'acc -> t -> 'acc

fold_right ?include_boundaries ~iterator f acc range is fold_left but starting from the last element and going back to the first. It's basically fold_left on rev range.

Sourceval length : ?include_boundaries:bool -> iterator:iterator -> t -> int

length ?include_boundaries ~iterator range calculates the size of a range.

Sourceval iter : ?include_boundaries:bool -> iterator:iterator -> (elt -> unit) -> t -> unit

iter ?include_boundaries ~iterator f range apply f on every element of the given range.

Sourceval to_list : ?include_boundaries:bool -> iterator:iterator -> t -> elt list

to_list ?include_boundaries ~iterator range converts the range into a list.

Sourceval to_seq : ?include_boundaries:bool -> iterator:iterator -> t -> elt Seq.t

to_seq ?include_boundaries ~iterator range converts the range into a seq.

Infix Operators

Sourcemodule Infix : sig ... end

Common and useful infix operators.

include module type of Infix
val (=) : t -> t -> bool

v1 = v2 is equal v1 v2.

val (<>) : t -> t -> bool

v1 <> v2 is not (equal v1 v2).

Iterators

Sourceval iterator_second : iterator

Iterator by one second.

Sourceval iterator_minute : iterator

Iterator by one minute.

Sourceval iterator_hour : iterator

Iterator by one hour.

Time range

Sourceval day : t

day returns a range between 00:00:00 and 23:59:59

Sourceval morning : t

morning returns a range between 05:00:00 and 11:59:59

Sourceval afternoon : t

afternoon returns a range between 12:00:00 and 16:59:59

Sourceval evening : t

evening returns a range between 17:00:00 and 20:59:59

Sourceval night : t

night returns a range between 21:00:00 and 04:59:59

Sourceval minute : time -> t

minute t creates a range at the beginning and end of the specified minute.

Sourceval hour : time -> t

hour t creates a range at the beginning and end of the specified hour.