module Sequence:sig..end
n^2 conversions between the n types. The
implementation relies on the assumption that a sequence can be iterated
on as many times as needed; this choice allows for high performance
of many combinators. However, for transient iterators, the Sequence.persistent
function is provided, storing elements of a transient iterator
in memory; the iterator can then be used several times (See further).
Note that some combinators also return sequences (e.g. group). The
transformation is computed on the fly every time one iterates over
the resulting sequence. If a transformation performs heavy computation,
Sequence.persistent can also be used as intermediate storage.
Most functions are lazy, i.e. they do not actually use their arguments
until their result is iterated on. For instance, if one calls Sequence.map
on a sequence, one gets a new sequence, but nothing else happens until
this new sequence is used (by folding or iterating on it).
If a sequence is built from an iteration function that is repeatable
(i.e. calling it several times always iterates on the same set of
elements, for instance List.iter or Map.iter), then
the resulting Sequence.t object is also repeatable. For one-time iter functions
such as iteration on a file descriptor or a Stream,
the Sequence.persistent function can be used to iterate and store elements in
a memory structure; the result is a sequence that iterates on the elements
of this memory structure, cheaply and repeatably.
type'at =('a -> unit) -> unit
'a. If you give it a function 'a -> unit
it will be applied to every element of the sequence successively.type'asequence ='a t
type('a, 'b)t2 =('a -> 'b -> unit) -> unit
'a and 'b.val from_iter : (('a -> unit) -> unit) -> 'a tval from_fun : (unit -> 'a option) -> 'a tSequence.persistent if needed!val empty : 'a tval singleton : 'a -> 'a tval doubleton : 'a -> 'a -> 'a tval cons : 'a -> 'a t -> 'a tcons x l yields x, then yields from l.
Same as append (singleton x) lval snoc : 'a t -> 'a -> 'a t
val return : 'a -> 'a tSequence.singletonval pure : 'a -> 'a tSequence.singletonval repeat : 'a -> 'a tSequence.take and the likes if you iterate on it.val iterate : ('a -> 'a) -> 'a -> 'a titerate f x is the infinite sequence x, f(x), f(f(x)), ...val forever : (unit -> 'b) -> 'b tSequence.take and Sequence.persistent.val cycle : 'a t -> 'a tSequence.take not to loop
forever.val iter : ('a -> unit) -> 'a t -> unititer f seq is just seq f.val iteri : (int -> 'a -> unit) -> 'a t -> unitval fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'aval foldi : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'aval map : ('a -> 'b) -> 'a t -> 'b tval mapi : (int -> 'a -> 'b) -> 'a t -> 'b tval map_by_2 : ('a -> 'a -> 'a) -> 'a t -> 'a tval for_all : ('a -> bool) -> 'a t -> boolval exists : ('a -> bool) -> 'a t -> boolval mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> booleq : the equality predicate to use (default (=))val find : ('a -> 'b option) -> 'a t -> 'b optionNoneval length : 'a t -> intval is_empty : 'a t -> boolval filter : ('a -> bool) -> 'a t -> 'a tval append : 'a t -> 'a t -> 'a tval concat : 'a t t -> 'a tval flatten : 'a t t -> 'a tSequence.concatval flat_map : ('a -> 'b t) -> 'a t -> 'b tSequence.concat.
Formerly flatMapval flat_map_l : ('a -> 'b list) -> 'a t -> 'b t
val filter_map : ('a -> 'b option) -> 'a t -> 'b tNone elements
Formerly fmapval intersperse : 'a -> 'a t -> 'a tval persistent : 'a t -> 'a tval persistent_lazy : 'a t -> 'a tSequence.persistent. When calling persistent_lazy s,
a new sequence s' is immediately returned (without actually consuming
s) in constant time; the first time s' is iterated on,
it also consumes s and caches its content into a inner data
structure that will back s' for future iterations.
warning: on the first traversal of s', if the traversal
is interrupted prematurely (Sequence.take, etc.) then s' will not be
memorized, and the next call to s' will traverse s again.
val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tval sort_uniq : ?cmp:('a -> 'a -> int) -> 'a t -> 'a tsortval group_succ_by : ?eq:('a -> 'a -> bool) -> 'a t -> 'a list tgroup.val group_by : ?hash:('a -> int) ->
?eq:('a -> 'a -> bool) -> 'a t -> 'a list tval uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a tfun seq -> map List.hd (group seq).val product : 'a t -> 'b t -> ('a * 'b) tproduct a b,
the caller MUST ensure that b can be traversed as many times
as required (several times), possibly by calling Sequence.persistent on it
beforehand.val product2 : 'a t -> 'b t -> ('a, 'b) t2
val join : join_row:('a -> 'b -> 'c option) ->
'a t -> 'b t -> 'c tjoin ~join_row a b combines every element of a with every
element of b using join_row. If join_row returns None, then
the two elements do not combine. Assume that b allows for multiple
iterations.val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a tunfoldr f b will apply f to b. If it
yields Some (x,b') then x is returned
and unfoldr recurses with b'.val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b tval max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionm where m is the maximal
element otherwiseval min : ?lt:('a -> 'a -> bool) -> 'a t -> 'a optionSequence.max for more details.val head : 'a t -> 'a optionNoneval head_exn : 'a t -> 'aInvalid_argument if the sequence is emptyval take : int -> 'a t -> 'a tn elements from the sequence. Works on infinite
sequences.val take_while : ('a -> bool) -> 'a t -> 'a ts if the predicate is false for at
least one element of s.val fold_while : ('a -> 'b -> 'a * [ `Continue | `Stop ]) -> 'a -> 'b t -> 'a('a, `Stop)val drop : int -> 'a t -> 'a tn first elements of the sequence. Lazy.val drop_while : ('a -> bool) -> 'a t -> 'a tSequence.dropval rev : 'a t -> 'a tval empty2 : ('a, 'b) t2
val is_empty2 : ('a, 'b) t2 -> bool
val length2 : ('a, 'b) t2 -> int
val zip : ('a, 'b) t2 -> ('a * 'b) t
val unzip : ('a * 'b) t -> ('a, 'b) t2
val zip_i : 'a t -> (int, 'a) t2val fold2 : ('c -> 'a -> 'b -> 'c) -> 'c -> ('a, 'b) t2 -> 'c
val iter2 : ('a -> 'b -> unit) -> ('a, 'b) t2 -> unit
val map2 : ('a -> 'b -> 'c) -> ('a, 'b) t2 -> 'c t
val map2_2 : ('a -> 'b -> 'c) ->
('a -> 'b -> 'd) -> ('a, 'b) t2 -> ('c, 'd) t2map2_2 f g seq2 maps each x, y of seq2 into f x y, g x yval to_list : 'a t -> 'a listSequence.to_rev_list.val to_rev_list : 'a t -> 'a listSequence.to_list)val of_list : 'a list -> 'a t
val on_list : ('a t -> 'b t) -> 'a list -> 'b liston_list f l is equivalent to to_list @@ f @@ of_list l.val to_opt : 'a t -> 'a option
val to_array : 'a t -> 'a arrayval of_array : 'a array -> 'a t
val of_array_i : 'a array -> (int * 'a) tval of_array2 : 'a array -> (int, 'a) t2
val array_slice : 'a array -> int -> int -> 'a tarray_slice a i j Sequence of elements whose indexes range
from i to jval of_opt : 'a option -> 'a tval of_stream : 'a Stream.t -> 'a tval to_stream : 'a t -> 'a Stream.tval to_stack : 'a Stack.t -> 'a t -> unitval of_stack : 'a Stack.t -> 'a tStack.iter)val to_queue : 'a Queue.t -> 'a t -> unitval of_queue : 'a Queue.t -> 'a tval hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitval hashtbl_replace : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unitval to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.tval to_hashtbl2 : ('a, 'b) t2 -> ('a, 'b) Hashtbl.tval of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) tval of_hashtbl2 : ('a, 'b) Hashtbl.t -> ('a, 'b) t2val 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 concat_str : string t -> string
exception OneShotSequence
val of_in_channel : Pervasives.in_channel -> char tSequence.persistent.OneShotSequence when used more than once.val to_buffer : char t -> Buffer.t -> unitval int_range : start:int -> stop:int -> int tstart...stop by steps 1. Also see
(--) for an infix version.val int_range_dec : start:int -> stop:int -> int tstop...start by steps -1.
See (--^) for an infix versionval int_range_by : step:int -> int -> int -> int tint_range_by ~step i j is the range starting at i, including j,
where the difference between successive elements is step.
use a negative step for a decreasing sequence.Invalid_argument if step=0val bools : bool ttrue and falseval of_set : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a tval to_set : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'btype'agen =unit -> 'a option
type'aklist =unit -> [ `Cons of 'a * 'a klist | `Nil ]
val of_gen : 'a gen -> 'a tval to_gen : 'a t -> 'a genval of_klist : 'a klist -> 'a tval to_klist : 'a t -> 'a klistmodule Set:sig..end
module Map:sig..end
val random_int : int -> int tval random_bool : bool tval random_float : float -> float t
val random_array : 'a array -> 'a tval random_list : 'a list -> 'a tSequence.random_array.val shuffle : 'a t -> 'a tshuffle seq returns a perfect shuffle of seq.
Uses O(length seq) memory and time. Eager.val shuffle_buffer : int -> 'a t -> 'a tshuffle_buffer n seq returns a sequence of element of seq in random
order. The shuffling is *not* uniform. Uses O(n) memory.
The first n elements of the sequence are consumed immediately. The
rest is consumed lazily.
Since 0.7
val sample : int -> 'a t -> 'a arraysample n seq returns k samples of seq, with uniform probability.
It will consume the sequence and use O(n) memory.
It returns an array of size min (length seq) n.
Since 0.7
module Infix:sig..end
include Sequence.Infix
val pp_seq : ?sep:string ->
(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit'a, using the given pretty printer
to print each elements. An optional separator string can be provided.val pp_buf : ?sep:string -> (Buffer.t -> 'a -> unit) -> Buffer.t -> 'a t -> unitval to_string : ?sep:string -> ('a -> string) -> 'a t -> stringVery basic interface to manipulate files as sequence of chunks/lines. The sequences take care of opening and closing files properly; every time one iterates over a sequence, the file is opened/closed again.
Example: copy a file "a" into file "b", removing blank lines:
Sequence.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;
By chunks of 4096 bytes:
Sequence.IO.(chunks_of ~size:4096 "a" |> write_to "b");;
Read the lines of a file into a list:
Sequence.IO.lines "a" |> Sequence.to_list
module IO:sig..end