Module Sequence

module Sequence: sig .. end
Transient iterators, that abstract on a finite sequence of elements. They are designed to allow easy transfer (mappings) between data structures, without defining n^2 conversions between the n types.

type +'a t 
Sequence abstract iterator type, representing a finite sequence of values of type 'a.

Build a sequence


val from_iter : (('a -> unit) -> unit) -> 'a t
Build a sequence from a iter function
val empty : 'a t
Empty sequence
val singleton : 'a -> 'a t
Singleton sequence
val repeat : 'a -> 'a t
Infinite sequence of the same element
val iterate : ('a -> 'a) -> 'a -> 'a t
iterate f x is the infinite sequence (x, f(x), f(f(x)), ...)
val forever : (unit -> 'b) -> 'b t
Sequence that calls the given function to produce elements
val cycle : 'a t -> 'a t
Cycle forever through the given sequence. Assume the given sequence can be traversed any amount of times (not transient).

Consume a sequence


val iter : ('a -> unit) -> 'a t -> unit
Consume the sequence, passing all its arguments to the function
val iteri : (int -> 'a -> unit) -> 'a t -> unit
Iterate on elements and their index in the sequence
val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
Fold over elements of the sequence, consuming it
val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b
Fold over elements of the sequence and their index, consuming it
val map : ('a -> 'b) -> 'a t -> 'b t
Map objects of the sequence into other elements, lazily
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
Map objects, along with their index in the sequence
val for_all : ('a -> bool) -> 'a t -> bool
Do all elements satisfy the predicate?
val exists : ('a -> bool) -> 'a t -> bool
Exists there some element satisfying the predicate?
val length : 'a t -> int
How long is the sequence?
val is_empty : 'a t -> bool
Is the sequence empty?

Transform a sequence


val filter : ('a -> bool) -> 'a t -> 'a t
Filter on elements of the sequence
val append : 'a t -> 'a t -> 'a t
Append two sequences
val concat : 'a t t -> 'a t
Concatenate a sequence of sequences into one sequence
val flatMap : ('a -> 'b t) -> 'a t -> 'b t
Monadic bind. It applies the function to every element of the initial sequence, and calls concat.
val intersperse : 'a t -> 'a -> 'a t
Insert the second element between every element of the sequence
val persistent : 'a t -> 'a t
Iterate on the sequence, storing elements in a data structure. The resulting sequence can be iterated on as many times as needed.
val product : 'a t -> 'b t -> ('a * 'b) t
Cartesian product of the sequences. The first one is transformed by calling persistent on it, so that it can be traversed several times (outer loop of the product)
val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a t
unfoldr f b will apply f to b. If it yields Some (x,b') then x is returned and unfoldr recurses with b'.
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a -> 'a
Max element of the sequence, using the given comparison function. A default element has to be provided.
val min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a -> 'a
Min element of the sequence, using the given comparison function
val take : int -> 'a t -> 'a t
Take at most n elements from the sequence
val drop : int -> 'a t -> 'a t
Drop the n first elements of the sequence
val rev : 'a t -> 'a t
Reverse the sequence. O(n) memory and time.

Basic data structures converters


val to_list : 'a t -> 'a list
val to_rev_list : 'a t -> 'a list
Get the list of the reversed sequence (more efficient)
val of_list : 'a list -> 'a t
val to_array : 'a t -> 'a array
Convert to an array. Currently not very efficient because and intermediate list is used.
val of_array : 'a array -> 'a t
val of_array_i : 'a array -> (int * 'a) t
Elements of the array, with their index
val array_slice : 'a array -> int -> int -> 'a t
array_slice a i j Sequence of elements whose indexes range from i to j
val of_stream : 'a Stream.t -> 'a t
Sequence of elements of a stream
val to_stack : 'a Stack.t -> 'a t -> unit
Push elements of the sequence on the stack
val of_stack : 'a Stack.t -> 'a t
Sequence of elements of the stack (same order as Stack.iter)
val to_queue : 'a Queue.t -> 'a t -> unit
Push elements of the sequence into the queue
val of_queue : 'a Queue.t -> 'a t
Sequence of elements contained in the queue, FIFO order
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit
Add elements of the sequence to the hashtable, with Hashtbl.add
val hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit
Add elements of the sequence to the hashtable, with Hashtbl.replace (erases conflicting bindings)
val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.t
Build a hashtable from a sequence of key/value pairs
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) t
Sequence of key/value pairs from the hashtable
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a t
val hashtbl_values : ('a, 'b) Hashtbl.t -> 'b t
val of_str : string -> char t
val to_str : char t -> string
val of_in_channel : Pervasives.in_channel -> char t
val to_buffer : char t -> Buffer.t -> unit
Copy content of the sequence into the buffer
val int_range : start:int -> stop:int -> int t
Iterator on integers in start...stop by steps 1
val of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a t
Convert the given set to a sequence. The set module must be provided.
val to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'b
Convert the sequence to a set, given the proper set module

Functorial conversions between sets and sequences


module Set: sig .. end

Conversion between maps and sequences.


module Map: sig .. end

Infinite sequences of random values


val random_int : int -> int t
val random_bool : bool t
val random_float : float -> float t
val random_array : 'a array -> 'a t
Sequence of choices of an element in the array
val random_list : 'a list -> 'a t

Type-classes


module TypeClass: sig .. end

Pretty printing of sequences


val pp_seq : ?sep:string ->
(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit
Pretty print a sequence of 'a, using the given pretty printer to print each elements. An optional separator string can be provided.