Legend:
Library
Module
Module type
Parameter
Class
Class type
Library
Module
Module type
Parameter
Class
Class type
The submodule Persistent
, also available under the name P
, offers an implementation of persistent (immutable) sequences.
A sequence s
of type 'a t
is an immutable data structure which represents a mathematical sequence of elements of type 'a
.
val create : 'a -> 'a t
create default
constructs and returns a new empty sequence. The default value default
is used to overwrite array slots that become empty during operations such as clear
, pop
, or split
.
make default n v
constructs and returns a fresh sequence whose length is n
and which consists of n
copies of the value v
. It is equivalent to of_array default (Array.make n v)
.
init default n f
constructs and returns a fresh sequence whose length is n
and whose elements are the values produced by the calls f 0
, f 1
, ... f (n-1)
, in this order. It is equivalent to of_array default (Array.init n f)
.
val default : 'a t -> 'a
default s
returns a value that was supplied as an argument in a call to create
that contributed to the construction of the sequence s
.
val is_empty : 'a t -> bool
is_empty s
returns true
if the sequence s
is empty and false
otherwise. It is equivalent to length s = 0
.
push side s x
constructs and returns a new sequence obtained by pushing the element x
onto the front or back end of the sequence s
. The parameter side
determines which end of the sequence is acted upon.
If the sequence s
is nonempty, then pop side s
returns a pair of the element x
found at the front or back end of the sequence s
and of the sequence s
deprived of x
. The parameter side
determines which end of the sequence is acted upon. If the sequence s
is empty, the exception Empty
is raised.
If the sequence s
is nonempty, then pop_opt side s
returns a pair (Some x, s')
where x
is the element found at the front or back end of the sequence s
and s'
is the sequence s
deprived of x
. The parameter side
determines which end of the sequence is acted upon. If the sequence s
is empty, the pair (None, s)
is returned.
If the sequence s
is nonempty, then peek side s
reads the element x
found at the front or back end of the sequence s
and returns x
. The parameter side
determines which end of the sequence is acted upon. If the sequence s
is empty, the exception Empty
is raised.
If the sequence s
is nonempty, then peek_opt side s
reads the element x
found at the front or back end of the sequence s
and returns Some x
. The parameter side
determines which end of the sequence is acted upon. If the sequence s
is empty, None
is returned.
get s i
returns the element x
located at index i
in the sequence s
. The index i
must be comprised between 0 included and length s
excluded.
set s i x
returns a new sequence obtained by replacing the element located at index i
in the sequence s
with the element x
. The index i
must be comprised between 0 included and length s
excluded. The sequence s
is not affected.
concat s1 s2
returns a new sequence obtained by concatenating the sequences s1
and s2
.
split s i
splits the sequence s
at index i
. It returns two sequences s1
and s2
such that the length of s1
is i
and the concatenation of s1
and s2
is s
. The index i
must be comprised between 0 and length s
, both included.
iter direction f s
applies the function f
in turn to every element x
of the sequence s
. The parameter direction
determines in what order the elements are presented. The function f
is not allowed to modify the sequence s
while iteration is ongoing.
iteri direction f s
applies the function f
in turn to every index i
and matching element x
of the sequence s
. The parameter direction
determines in what order the elements are presented. The function f
is not allowed to modify the sequence s
while iteration is ongoing.
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
fold_left f a s
applies the function f
in turn to each element of the sequence s
, in the forward direction. An accumulator is threaded through the calls to f
. The function f
is not allowed to modify the sequence s
while iteration is ongoing. Subject to this condition, fold_left f a s
is equivalent to List.fold_left f a (to_list s)
.
val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
fold_left f a s
applies the function f
in turn to each element of the sequence s
, in the backward direction. An accumulator is threaded through the calls to f
. The function f
is not allowed to modify the sequence s
while iteration is ongoing. Subject to this condition, fold_right f s a
is equivalent to List.fold_right f (to_list s) a
.
val to_list : 'a t -> 'a list
to_list s
returns a list whose elements are the elements of the sequence s
.
val to_array : 'a t -> 'a array
to_array s
returns a fresh array whose elements are the elements of the sequence s
.
of_array_segment default a head size
creates a new sequence out of the array segment defined by the array a
, the start index head
, and the size size
. The data is copied, so the array a
can still be used afterwards.
val of_array : 'a -> 'a array -> 'a t
of_array default a
creates a new sequence out of the array a
. The data is copied, so the array a
can still be used afterwards.
val format : Format.formatter -> int t -> unit
format
is a printer for sequences of integers. It can be installed in the OCaml toplevel loop by #install_printer format
. It is intended to be used only while debugging the library.
val check : 'a t -> unit
In a release build, this function does nothing. In a development build, it checks that the data structure's internal invariant is satisfied.