module Cf_seq:sig..end
cf library make
extensive use of this type.
The functions for manipulating static lists in the standard List library
all have equivalents here in this module. Additionally, there are some
convenient functions for converting structures with imperative interfaces
into sequences that permit functional algorithms to be applied.
type'at ='a cell Lazy.t
type 'a cell =
| |
P of |
(* | Element of a sequence | *) |
| |
Z |
(* | End of sequence | *) |
exception Empty
val nil : 'a tval head : 'a t -> 'aEmpty if the sequence
has no elements, i.e. if the sequence is Z.val tail : 'a t -> 'a tEmpty if the sequence has no elements, i.e.
if the sequence is Z.val concat : 'a t -> 'a t -> 'a tconcat a b returns the sequence of all the elements in a followed by
all the elements in b. Adds a constant cost to the evaluation of every
element in the resulting sequence prior to the start of elements from b,
so it may be worth considering the use of a Cf_deque object in place of
a Cf_seq object to avoid cost explosion.val flatten : 'a t t -> 'a tflatten a returns the sequence of all the elements in the sequence of
sequences by concatenating them.val limit : ?x:exn -> int -> 'a t -> 'a tlimit n s returns the sequence of all the elements in s, up to n
elements in number and no more. Raises Invalid_argument if n < 0.
If ?x is provided, then the exception is raised if the sequence is
evaluated past the limit.val shift : int -> 'a t -> 'a tshift n s returns the sequence of all the elements in s after the first
n elements are discarded. Returns the empty sequence if s has fewer
than n elements.val sentinel : exn -> 'a t -> 'a tsentinel x s returns a sequence identical to s except that x is raised
by evaluating to the end. This is intended for use in incremental sequence
processing.val reverse : 'a t -> 'a listreverse s evaluates the entire sequence and composes a list of the
elements in reverse order. Tail recursive.val length : 'a t -> intval unfold : ('b -> ('a * 'b) option) -> 'b -> 'a tunfold f a returns the sequence composed of the results of applying f
according to the following rule: the first application of f is with a
as the argument; if the result is None then the empty sequence is
returned; else, the result is Some (hd, tl) and the sequence returned is
composed of an element hd followed by the sequence produced by looping
through applications of f tl until None is returned to signal the end
of the sequence.
The function is defined as follows:
let rec unfold f s =
match f s with
| Some (hd, tl) -> P (hd, lazy (unfold f tl))
| None -> Z
val unfold2 : ('b -> ('a * 'b) option) -> 'b -> ('a * 'b) tunfold2 f a is like unfold f a above, except that the sequence returned
has elements which are pairs of output values and the input values that
correspond to them.
The function is defined as follows:
let rec unfold2 f s =
match f s with
| Some (hd, tl) -> P ((hd, s), lazy (unfold2 f tl))
| None -> Z
val iterate : ('a -> unit) -> 'a t -> unititerate f s evaluates the entire sequence s, applying f to each
element in order until the end of the sequence is reached. Tail recursive.val predicate : ('a -> bool) -> 'a t -> boolpredicate f s evaluates as much of the sequence s as necessary to
determine that every element satisfy the predicate function f. If any
element produces a false result, then false is returned and the
remainder of the sequence is not evaluated. Otherwise, the entire sequence
is evaluated and true is returned. Tail recursive.val constrain : ('a -> bool) -> 'a t -> 'a tconstrain f s evaluates the sequence s by applying f to each element
while the result is true. The returned sequence is all the elements of
s before the first element for which f returns false. Tail recursive.val search : ('a -> bool) -> 'a t -> intsearch f s evaluates the sequence s until the result of applying f is
true and returns the number of elements applied that resulted in a
false result. Tail recursive.val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'bfold f a s is like List.fold_left and is the result of applying f to
the elements in sequence, i.e. f (... (f (f a b1) b2) ...) bn, where
b1, b2 ... bn are the elements of the sequence. Evaluates the
entire sequence s in a tail recursive loop.val filter : ('a -> bool) -> 'a t -> 'a tfilter f s returns the sequence produced by applying f to every element
of s and taking all the elements for which the result is true. The
sequence returned evaluates s on demand.val map : ('a -> 'b) -> 'a t -> 'b tmap f s returns the sequence produced by transforming every element in
s by applying it to f. The sequence returned evaluates s on demand.val optmap : ('a -> 'b option) -> 'a t -> 'b toptmap f s returns the sequence produced by applying f to every element
of s and taking all the Some a results in order. The sequence returned
evaluates s on demand.val listmap : ('a -> 'b list) -> 'a t -> 'b tlistmap f s returns the sequence produced by applying f to every
element of s and taking all the resulting lists of elements in order.
The sequence returned evaluates s on demand.val seqmap : ('a -> 'b t) -> 'a t -> 'b tseqmap f s returns the sequence produced by applying f to every element
of s and taking all the resulting sequences of elements in order. The
sequence returned evaluates s on demand.val partition : ('a -> bool) -> 'a t -> 'a t * 'a tpartition f s returns two sequences. The first is the sequence of
elements in s for which applying f results in true, and the second
is the sequence of elements for which applying f results in false.val fcmp : ('a -> 'a -> int) -> 'a t -> 'a t -> intfcmp f a b compares two sequences by applying f to the elements of
each sequence in turn until the result is non-zero, or the end of one
or both sequences is reached. If the result of f is non-zero, then
that is the value returned; otherwise, the value returned is an indication
of which sequences have ended. If a ends while b continues, then the
result is 1. If b ends while a continues, then the result is (-1).
If both sequences end at the same place, then 0 is returned.
The function is defined as follows:
let rec fcmp f s0 s1 =
match s0, s1 with
| P (x0, y0), P (x1, y1) ->
let d = f x0 x1 in
if d <> 0 then d else fcmp f (Lazy.force y0) (Lazy.force y1)
| P _, Z -> 1
| Z, P _ -> -1
| Z, Z -> 0
val cmp : 'a t -> 'a t -> intcmp a b is the same as fcmp Pervasives.compare a b.val equal : 'a t -> 'a t -> boolequal a b returns true, if every element in both sequences a and b
are logically equivalent, as with the built-in (=) comparison operator.
Both sequences are evaluated until one of the sequences reaches the end, or
the elements in each are found to be inequivalent.val first : ('a * 'b) t -> 'a tfirst s returns the sequence of elements composed by taking only the
first object in an element pair. Evaluates s on demand.val second : ('a * 'b) t -> 'b tsecond s returns the sequence of elements composed by taking only the
second object in an element pair. Evaluates s on demand.val split : ('a * 'b) t -> 'a t * 'b tsplit s is equivalent to (first s, second s).val combine : 'a t -> 'b t -> ('a * 'b) tcombine a b returns the sequence composed of the pairs of elements
produced by combining each element from a and the corresponding element
from b in a pair (a, b) until all the elements from one or both
sequences are exhausted. The sequences a and b are evaluated on
demand. The resulting sequence is only as long as the shorter of the two
input sequences.val iterate2 : ('a -> 'b -> unit) -> 'a t -> 'b t -> unititerate2 f a b is like iterate f s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end.val predicate2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> boolpredicate2 f a b is like predicate f s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end. If
the sequences are not the same length, then the result is always false.val constrain2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> ('a * 'b) tconstrain2 f a b is like constrain f s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end or
the constrain returns false.val fold2 : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'cfold2 f a s1 s2 is like fold f a s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end.val filter2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> ('a * 'b) tfilter2 f a b is like filter f s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end.val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c tmap2 f a b is like map f s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end.val optmap2 : ('a -> 'b -> 'c option) -> 'a t -> 'b t -> 'c toptmap2 f a b is like optmap f s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end.val listmap2 : ('a -> 'b -> 'c list) -> 'a t -> 'b t -> 'c tlistmap2 f a b is like listmap f s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end.val seqmap2 : ('a -> 'b -> 'c t) -> 'a t -> 'b t -> 'c tseqmap2 f a b is like seqmap f s, except it operates on a pair of
sequences simultaneously, until one or both sequences reaches its end.val of_channel : Pervasives.in_channel -> char tof_channel c returns the sequence of characters produced by reading them
on demand from the channel c. (Note: this means that dueling char t
sequences reading from the same in_channel object may interfere with one
another.) The sequence returned ends when EOF happens on the channel.val of_string : string -> char tof_string s returns the sequence of characters produced by extracting
them on demand from the string s with String.unsafe_get. Since the
contents of strings are mutable, be advised that the character extracted
from the string is determined at the time the position in the sequence is
evaluated, and that subsequent changes to the string will not be reflected
in the sequence.val of_substring : string -> int -> char tof_substring s pos returns the sequence of characters produced by
extracting them on demand from the string s with String.unsafe_get.
Returns Invalid_argument if pos < 0 or pos >= String.length s.
The sequence ends when the end of the string is reached. If a shorter
substring is desired, use the limit function in conjunction.val of_array : 'a array -> 'a tof_array v is like of_string s, except that it operates on an
'a array value instead of a string value.val of_subarray : 'a array -> int -> 'a tof_subarray v pos is like of_substring s pos, except that it operates
on an 'a array value instead of a string value.val of_list : 'a list -> 'a tof_list s converts a 'a list value into a sequence.val of_function : (unit -> 'a) -> 'a tof_function f returns a sequence produced by applying f () repeatedly
until Not_found is raised.val to_channel : char t -> Pervasives.out_channel -> unitto_channel s c evaluates the entire character sequence s and puts each
character produced into the out_channel object in a tail-recursive loop.val to_string : char t -> stringto_string s evaluates the entire character sequence s and composes a
string value containing the characters in order. Tail-recursive.val to_substring : char t -> string -> int -> int -> char tto_substring s str pos len overwrites the substring of str starting at
pos and running for len characters, with the first len characters
from the sequence s. If the sequence is shorter than len characters,
then the rest of the substring is not overwritten. If pos and len do
not describe a valid substring of str, then Invalid_argument is raised.
The unused portion of the character sequence is returned.val to_array : 'a t -> 'a arrayto_array v is like to_string s, except that it constructs an 'a array
value instead of a string value.val to_subarray : 'a t -> 'a array -> int -> int -> 'a tto_subarray s v pos len is like to_substring s str pos len, except that
it overwrites an 'a array value instead of a string value.val to_list : 'a t -> 'a listto_list s is the same as List.rev (reverse s).val to_buffer : char t -> Buffer.t -> unitto_buffer s b is like to_channel s c except that characters are output
to a Buffer object, instead of an out_channel object.val to_function : 'a t -> unit -> 'ato_function s returns a function that evaluates the next value in the
sequence each time it's called. When the sequence completes, End_of_file
is raised.val writeC : 'x -> ('x t, unit) Cf_cmonad.twrite x to compose a continuation monad that puts x into the
sequence produced by evaluation and returns the unit value.val evalC : ('x t, unit) Cf_cmonad.t -> 'x tevalC m to evaluate the continuation monad m to compute the
sequence it encapsulates.val writeSC : 'x -> ('s, 'x t, unit) Cf_scmonad.twriteSC x to compose a state-continuation monad that puts x into
the sequence produced by evaluation and returns the unit value.val evalSC : ('s, 'x t, unit) Cf_scmonad.t -> 's -> 'x tevalSC m s to evaluate the state-continuation monad m with the
initial state s, computing the encapsulated sequence.module S:sig..end
sequence and accumulate functions for the
state monad.
module C:sig..end
sequence and accumulate functions for the
continuation monad.
module SC:sig..end
sequence and accumulate functions for the
state-continuation monad.