base
-
library base
-
module Base
-
module Applicative
-
module type Applicative_infix
-
module type Applicative_infix2
-
module type Applicative_infix3
-
module type Basic
-
module type Basic2
-
module type Basic2_using_map2
-
module type Basic3
-
module type Basic3_using_map2
-
module type Basic_using_map2
-
module Compose
-
argument 1-F
-
module Applicative_infix
-
-
argument 2-G
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module type Let_syntax
-
module Let_syntax
-
module Let_syntax
-
-
module Open_on_rhs_intf
-
-
module type Let_syntax2
-
module Let_syntax
-
module Let_syntax
-
-
module Open_on_rhs_intf
-
-
module type Let_syntax3
-
module Let_syntax
-
module Let_syntax
-
-
module Open_on_rhs_intf
-
-
module Make
-
argument 1-X
-
module Applicative_infix
-
-
module Make2
-
argument 1-X
-
module Applicative_infix
-
-
module Make2_using_map2
-
argument 1-X
-
module Applicative_infix
-
-
module Make3
-
argument 1-X
-
module Applicative_infix
-
-
module Make3_using_map2
-
argument 1-X
-
module Applicative_infix
-
-
module Make_let_syntax
-
argument 1-X
-
argument 2-Intf
-
module Let_syntax
-
module Let_syntax
-
-
-
module Make_let_syntax2
-
argument 1-X
-
argument 2-Intf
-
module Let_syntax
-
module Let_syntax
-
-
-
module Make_let_syntax3
-
argument 1-X
-
argument 2-Intf
-
module Let_syntax
-
module Let_syntax
-
-
-
module Make_using_map2
-
argument 1-X
-
module Applicative_infix
-
-
module Of_monad
-
argument 1-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Applicative_infix
-
-
module Of_monad2
-
argument 1-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Applicative_infix
-
-
module Pair
-
argument 1-F
-
module Applicative_infix
-
-
argument 2-G
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module type S
-
module Applicative_infix
-
-
module type S2
-
module Applicative_infix
-
-
module S2_to_S
-
argument 1-X
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module S2_to_S3
-
argument 1-X
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module type S3
-
module Applicative_infix
-
-
module S3_to_S2
-
argument 1-X
-
module Applicative_infix
-
-
module Applicative_infix
-
-
module S_to_S2
-
argument 1-X
-
module Applicative_infix
-
-
module Applicative_infix
-
-
-
module Array
-
module Avltree
-
module Binary_search
-
module Binary_searchable
-
module type Indexable
-
module type Indexable1
-
module type S
-
module type S1
-
module Which_target_by_key
-
module Which_target_by_segment
-
-
module Blit
-
module Make
-
argument 1-Sequence
-
-
module Make1
-
argument 1-Sequence
-
-
module Make1_generic
-
argument 1-Sequence
-
-
module Make_distinct
-
module Make_to_string
-
argument 1-T
-
argument 2-To_bytes
-
-
module type S
-
module type S1
-
module type S1_distinct
-
module type S_distinct
-
module type S_to_string
-
module type Sequence
-
module type Sequence1
-
-
module Bool
-
module Non_short_circuiting
-
-
module Bytes
-
module From_string
-
module To_string
-
-
module Comparable
-
module Make_using_comparator
-
argument 1-T
-
-
module Polymorphic_compare
-
argument 1-T
-
-
module type S
-
module type With_compare
-
module Comparisons
-
module Container
-
module Continue_or_stop
-
module type Generic
-
module type Generic_phantom
-
module type S0
-
module type S0_phantom
-
module type S1
-
module type S1_phantom
-
module type S1_phantom_invariant
-
module type Summable
-
-
module Continue_or_stop
-
module Either
-
module First
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type Focused
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Second
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
-
module Error
-
module Internal_repr
-
-
module Exn
-
module Export
-
module Field
-
module Fn
-
module Formatter
-
module Hash_set
-
module Hashtbl
-
module type Accessors
-
module type Equal_m
-
module type For_deriving
-
module type Equal_m
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module type Sexp_of_m
-
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module Merge_into_action
-
module type Multi
-
module Poly
-
module type S_poly
-
module type S_without_submodules
-
module type Sexp_of_m
-
-
module Identifiable
-
module type Arg
-
module type Arg_with_comparator
-
module Make_using_comparator
-
argument 1-M
-
-
module type S
-
-
module Info
-
module Internal_repr
-
module type S
-
module Internal_repr
-
-
-
module Int
-
module Hex
-
module type Int_without_module_types
-
module O
-
module type Operators
-
module type Operators_unbounded
-
module type Round
-
module type S_unbounded
-
-
module Int63
-
module Hex
-
module O
-
module Overflow_exn
-
-
module Lazy
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
module T_unforcing
-
-
module Linked_queue
-
module List
-
module Assoc
-
module Cartesian_product
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
module Or_unequal_lengths
-
-
module Map
-
module type Accessors1
-
module type Accessors2
-
module type Accessors3
-
module type Accessors3_with_comparator
-
module type Accessors_generic
-
module type Compare_m
-
module Continue_or_stop
-
module type Creators1
-
module type Creators2
-
module type Creators3_with_comparator
-
module type Creators_and_accessors1
-
module type Creators_and_accessors2
-
module type Creators_and_accessors3_with_comparator
-
module type Creators_and_accessors_generic
-
module type Creators_generic
-
module type Equal_m
-
module Finished_or_unfinished
-
module type For_deriving
-
module type Compare_m
-
module type Equal_m
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module type Sexp_of_m
-
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module Merge_element
-
module Or_duplicate
-
module Poly
-
module type S_poly
-
module type Sexp_of_m
-
module Symmetric_diff_element
-
module Using_comparator
-
module Empty_without_value_restriction
-
argument 1-K
-
-
module Tree
-
module Build_increasing
-
-
-
module With_comparator
-
module With_first_class_module
-
module Without_comparator
-
-
module Maybe_bound
-
module Monad
-
module type Basic
-
module type Basic2
-
module type Basic3
-
module type Basic_indexed
-
module Ident
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type Infix
-
module type Infix2
-
module type Infix3
-
module type Infix_indexed
-
module Make
-
argument 1-X
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Make2
-
argument 1-X
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Make3
-
argument 1-X
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Make_indexed
-
argument 1-X
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Of_monad
-
argument 1-Monad
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
argument 2-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Of_monad2
-
argument 1-Monad
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
argument 2-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Of_monad3
-
argument 1-Monad
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
argument 2-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Of_monad_indexed
-
argument 1-Monad
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
argument 2-M
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S2
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S3
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S_indexed
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module type S_without_syntax
-
module Monad_infix
-
-
module type Syntax
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
-
module type Syntax2
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
-
module type Syntax3
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
-
module type Syntax_indexed
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
-
-
module Nothing
-
module Option
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Option_array
-
module Or_error
-
module Applicative_infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Ordered_collection_common
-
module Private
-
-
module Poly
-
module Popcount
-
module Pretty_printer
-
module Register_pp
-
argument 1-M
-
-
module type S
-
module Printf
-
module Result
-
module Error
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Export
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Sequence
-
module Expert
-
module Generator
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
-
module Infix
-
module Let_syntax
-
module Let_syntax
-
module Open_on_rhs
-
-
-
module Monad_infix
-
module Step
-
-
module Set
-
module type Accessors0
-
module Named
-
-
module type Accessors1
-
module Named
-
-
module type Accessors2
-
module Named
-
-
module type Accessors2_with_comparator
-
module Named
-
-
module type Accessors_generic
-
module Named
-
-
module type Compare_m
-
module type Creators0
-
module type Creators1
-
module type Creators2
-
module type Creators2_with_comparator
-
module type Creators_and_accessors0
-
module Named
-
-
module type Creators_and_accessors1
-
module Named
-
-
module type Creators_and_accessors2
-
module Named
-
-
module type Creators_and_accessors2_with_comparator
-
module Named
-
-
module type Creators_generic
-
module type Elt_plain
-
module type Equal_m
-
module type For_deriving
-
module type Compare_m
-
module type Equal_m
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module type Sexp_of_m
-
-
module type M_of_sexp
-
module type M_sexp_grammar
-
module Merge_to_sequence_element
-
module Named
-
module type Sexp_of_m
-
module Using_comparator
-
module Empty_without_value_restriction
-
argument 1-Elt
-
-
module Named
-
-
module With_comparator
-
module With_first_class_module
-
module Without_comparator
-
-
module Sexpable
-
module Of_sexpable
-
argument 1-Sexpable
-
argument 2-M
-
-
module Of_sexpable1
-
argument 1-Sexpable
-
argument 2-M
-
-
module Of_sexpable2
-
argument 1-Sexpable
-
argument 2-M
-
-
module Of_sexpable3
-
argument 1-Sexpable
-
argument 2-M
-
-
module Of_stringable
-
argument 1-M
-
-
module type S
-
module type S1
-
module type S2
-
module type S3
-
-
module Sign
-
module Sign_or_nan
-
module Source_code_position
-
module Staged
-
module String
-
module Caseless
-
module Escaping
-
module Search_pattern
-
-
module Stringable
-
module type S
-
-
module Sys
-
module type T1
-
module type T2
-
module type T3
-
module Type_equal
-
module type Injective
-
module type Injective2
-
module Uchar
-
module Uniform_array
-
module Unit
-
module Variant
-
module With_return
-
module Word_size
-
-
-
library base.base_internalhash_types
-
module Base_internalhash_types
-
-
library base.caml
-
module Caml
-
module In_channel
-
module Out_channel
-
-
-
library base.md5
-
module Md5_lib
-
-
library base.shadow_stdlib
-
module Shadow_stdlib
-
module In_channel
-
module Out_channel
-
-
val sexp_of_t : ( 'a -> Sexplib0.Sexp.t ) -> 'a t -> Sexplib0.Sexp.t
type 'a sequence = 'a t
include Indexed_container.S1 with type 'a t := 'a t
include Container.S1 with type 'a t := 'a t
val mem : 'a t -> 'a -> equal:( 'a -> 'a -> bool ) -> bool
Checks whether the provided element is there, using equal
.
val length : 'a t -> int
val is_empty : 'a t -> bool
val iter : 'a t -> f:( 'a -> unit ) -> unit
val fold : 'a t -> init:'accum -> f:( 'accum -> 'a -> 'accum ) -> 'accum
fold t ~init ~f
returns f (... f (f (f init e1) e2) e3 ...) en
, where e1..en
are the elements of t
val fold_result :
'a t ->
init:'accum ->
f:( 'accum -> 'a -> ( 'accum, 'e ) Result.t ) ->
( 'accum, 'e ) Result.t
fold_result t ~init ~f
is a short-circuiting version of fold
that runs in the Result
monad. If f
returns an Error _
, that value is returned without any additional invocations of f
.
val fold_until :
'a t ->
init:'accum ->
f:( 'accum -> 'a -> ( 'accum, 'final ) Container.Continue_or_stop.t ) ->
finish:( 'accum -> 'final ) ->
'final
fold_until t ~init ~f ~finish
is a short-circuiting version of fold
. If f
returns Stop _
the computation ceases and results in that value. If f
returns Continue _
, the fold will proceed. If f
never returns Stop _
, the final result is computed by finish
.
Example:
type maybe_negative =
| Found_negative of int
| All_nonnegative of { sum : int }
(** [first_neg_or_sum list] returns the first negative number in [list], if any,
otherwise returns the sum of the list. *)
let first_neg_or_sum =
List.fold_until ~init:0
~f:(fun sum x ->
if x < 0
then Stop (Found_negative x)
else Continue (sum + x))
~finish:(fun sum -> All_nonnegative { sum })
;;
let x = first_neg_or_sum [1; 2; 3; 4; 5]
val x : maybe_negative = All_nonnegative {sum = 15}
let y = first_neg_or_sum [1; 2; -3; 4; 5]
val y : maybe_negative = Found_negative -3
val exists : 'a t -> f:( 'a -> bool ) -> bool
Returns true
if and only if there exists an element for which the provided function evaluates to true
. This is a short-circuiting operation.
val for_all : 'a t -> f:( 'a -> bool ) -> bool
Returns true
if and only if the provided function evaluates to true
for all elements. This is a short-circuiting operation.
val count : 'a t -> f:( 'a -> bool ) -> int
Returns the number of elements for which the provided function evaluates to true.
val sum :
(module Container.Summable with type t = 'sum) ->
'a t ->
f:( 'a -> 'sum ) ->
'sum
Returns the sum of f i
for all i
in the container.
val find : 'a t -> f:( 'a -> bool ) -> 'a option
Returns as an option
the first element for which f
evaluates to true.
val find_map : 'a t -> f:( 'a -> 'b option ) -> 'b option
Returns the first evaluation of f
that returns Some
, and returns None
if there is no such element.
val to_list : 'a t -> 'a list
val to_array : 'a t -> 'a array
val min_elt : 'a t -> compare:( 'a -> 'a -> int ) -> 'a option
Returns a minimum (resp maximum) element from the collection using the provided compare
function, or None
if the collection is empty. In case of a tie, the first element encountered while traversing the collection is returned. The implementation uses fold
so it has the same complexity as fold
.
val max_elt : 'a t -> compare:( 'a -> 'a -> int ) -> 'a option
These are all like their equivalents in Container
except that an index starting at 0 is added as the first argument to f
.
val foldi : 'a t -> init:_ -> f:( int -> _ -> 'a -> _ ) -> _
val iteri : 'a t -> f:( int -> 'a -> unit ) -> unit
val existsi : 'a t -> f:( int -> 'a -> bool ) -> bool
val counti : 'a t -> f:( int -> 'a -> bool ) -> int
val findi : 'a t -> f:( int -> 'a -> bool ) -> (int * 'a) option
val find_mapi : 'a t -> f:( int -> 'a -> 'b option ) -> 'b option
include Monad.S with type 'a t := 'a t
t >>= f
returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t
to yield a value v
, and then runs the computation returned by f v
.
module Monad_infix : sig ... end
val return : 'a -> 'a t
return v
returns the (trivial) computation that returns v.
ignore_m t
is map t ~f:(fun _ -> ())
. ignore_m
used to be called ignore
, but we decided that was a bad name, because it shadowed the widely used Caml.ignore
. Some monads still do let ignore = ignore_m
for historical reasons.
module Let_syntax : sig ... end
These are convenient to have in scope when programming with a monad:
val empty : _ t
empty
is a sequence with no elements.
next
returns the next element of a sequence and the next tail if the sequence is not finished.
module Step : sig ... end
A Step
describes the next step of the sequence construction. Done
indicates the sequence is finished. Skip
indicates the sequence continues with another state without producing the next element yet. Yield
outputs an element and introduces a new state.
unfold_step ~init ~f
constructs a sequence by giving an initial state init
and a function f
explaining how to continue the next step from a given state.
val unfold : init:'s -> f:( 's -> ('a * 's) option ) -> 'a t
unfold ~init f
is a simplified version of unfold_step
that does not allow Skip
.
unfold_with t ~init ~f
folds a state through the sequence t
to create a new sequence
val unfold_with_and_finish :
'a t ->
init:'s_a ->
running_step:( 's_a -> 'a -> ( 'b, 's_a ) Step.t ) ->
inner_finished:( 's_a -> 's_b ) ->
finishing_step:( 's_b -> ( 'b, 's_b ) Step.t ) ->
'b t
unfold_with_and_finish t ~init ~running_step ~inner_finished ~finishing_step
folds a state through t
to create a new sequence (like unfold_with t ~init
~f:running_step
), and then continues the new sequence by unfolding the final state (like unfold_step ~init:(inner_finished final_state) ~f:finishing_step
).
val nth : 'a t -> int -> 'a option
Returns the nth element.
val nth_exn : 'a t -> int -> 'a
folding_map
is a version of map
that threads an accumulator through calls to f
.
If t1
and t2
are each sorted without duplicates, merge_deduped_and_sorted t1 t2
~compare
merges t1
and t2
into a sorted sequence without duplicates. Whenever identical elements are found in both t1
and t2
, the one from t1
is used and the one from t2
is discarded. The behavior is undefined if the inputs aren't sorted or contain duplicates.
If t1
and t2
are each sorted, merge_sorted t1 t2 ~compare
merges t1
and t2
into a sorted sequence. Whenever identical elements are found in both t1
and t2
, the one from t1
is used first. The behavior is undefined if the inputs aren't sorted.
module Merge_with_duplicates_element : sig ... end
val merge_with_duplicates :
'a t ->
'b t ->
compare:( 'a -> 'b -> int ) ->
( 'a, 'b ) Merge_with_duplicates_element.t t
merge_with_duplicates_element t1 t2 ~compare
is like merge
, except that for each element it indicates which input(s) the element comes from, using Merge_with_duplicates_element
.
val hd : 'a t -> 'a option
val hd_exn : 'a t -> 'a
tl t
and tl_eagerly_exn t
immediately evaluates the first element of t
and returns the unevaluated tail.
val find_exn : 'a t -> f:( 'a -> bool ) -> 'a
find_exn t ~f
returns the first element of t
that satisfies f
. It raises if there is no such element.
val for_alli : 'a t -> f:( int -> 'a -> bool ) -> bool
Like for_all
, but passes the index as an argument.
append t1 t2
first produces the elements of t1
, then produces the elements of t2
.
concat tt
produces the elements of each inner sequence sequentially. If any inner sequences are infinite, elements of subsequent inner sequences will not be reached.
concat_mapi t ~f
is like concat_map, but passes the index as an argument.
interleave tt
produces each element of the inner sequences of tt
eventually, even if any or all of the inner sequences are infinite. The elements of each inner sequence are produced in order with respect to that inner sequence. The manner of interleaving among the separate inner sequences is deterministic but unspecified.
round_robin list
is like interleave (of_list list)
, except that the manner of interleaving among the inner sequences is guaranteed to be round-robin. The input sequences may be of different lengths; an empty sequence is dropped from subsequent rounds of interleaving.
Transforms a pair of sequences into a sequence of pairs. The length of the returned sequence is the length of the shorter input. The remaining elements of the longer input are discarded.
WARNING: Unlike List.zip
, this will not error out if the two input sequences are of different lengths, because zip
may have already returned some elements by the time this becomes apparent.
zip_full
is like zip
, but if one sequence ends before the other, then it keeps producing elements from the other sequence until it has ended as well.
val reduce_exn : 'a t -> f:( 'a -> 'a -> 'a ) -> 'a
reduce_exn f [a1; ...; an]
is f (... (f (f a1 a2) a3) ...) an
. It fails on the empty sequence.
val reduce : 'a t -> f:( 'a -> 'a -> 'a ) -> 'a option
group l ~break
returns a sequence of lists (i.e., groups) whose concatenation is equal to the original sequence. Each group is broken where break
returns true on a pair of successive elements.
Example:
group ~break:(<>) (of_list ['M';'i';'s';'s';'i';'s';'s';'i';'p';'p';'i']) ->
of_list [['M'];['i'];['s';'s'];['i'];['s';'s'];['i'];['p';'p'];['i']]
val find_consecutive_duplicate :
'a t ->
equal:( 'a -> 'a -> bool ) ->
('a * 'a) option
find_consecutive_duplicate t ~equal
returns the first pair of consecutive elements (a1, a2)
in t
such that equal a1 a2
. They are returned in the same order as they appear in t
.
The same sequence with consecutive duplicates removed. The relative order of the other elements is unaffected.
val range :
?stride:int ->
?start:[ `inclusive | `exclusive ] ->
?stop:[ `inclusive | `exclusive ] ->
int ->
int ->
int t
range ?stride ?start ?stop start_i stop_i
is the sequence of integers from start_i
to stop_i
, stepping by stride
. If stride
< 0 then we need start_i
> stop_i
for the result to be nonempty (or start_i
>= stop_i
in the case where both bounds are inclusive).
val init : int -> f:( int -> 'a ) -> 'a t
init n ~f
is [(f 0); (f 1); ...; (f (n-1))]
. It is an error if n < 0
.
filter_map t ~f
produce mapped elements of t
which are not None
.
filter_mapi
is just like filter_map
, but it also passes in the index of each element to f
.
filter_opt t
produces the elements of t
which are not None
. filter_opt t
= filter_map t ~f:Fn.id
.
sub t ~pos ~len
is the len
-element subsequence of t
, starting at pos
. If the sequence is shorter than pos + len
, it returns t[pos] ... t[l-1]
, where l
is the length of the sequence.
drop t n
produces all elements of t
except the first n
elements. If there are fewer than n
elements in t
, there is no error; the resulting sequence simply produces no elements. Usually you will probably want to use drop_eagerly
because it can be significantly cheaper.
drop_eagerly t n
immediately consumes the first n
elements of t
and returns the unevaluated tail of t
.
take_while t ~f
produces the longest prefix of t
for which f
applied to each element is true
.
drop_while t ~f
produces the suffix of t
beginning with the first element of t
for which f
is false
. Usually you will probably want to use drop_while_option
because it can be significantly cheaper.
drop_while_option t ~f
immediately consumes the elements from t
until the predicate f
fails and returns the first element that failed along with the unevaluated tail of t
. The first element is returned separately because the alternatives would mean forcing the consumer to evaluate the first element again (if the previous state of the sequence is returned) or take on extra cost for each element (if the element is added to the final state of the sequence using shift_right
).
split_n t n
immediately consumes the first n
elements of t
and returns the consumed prefix, as a list, along with the unevaluated tail of t
.
chunks_exn t n
produces lists of elements of t
, up to n
elements at a time. The last list may contain fewer than n
elements. No list contains zero elements. If n
is not positive, it raises.
shift_right_with_list t l
produces the elements of l
, then produces the elements of t
. It is better to call shift_right_with_list
with a list of size n than shift_right
n times; the former will require O(1) work per element produced and the latter O(n) work per element produced.
module Infix : sig ... end
Returns a sequence with all possible pairs. The stepper function of the second sequence passed as argument may be applied to the same state multiple times, so be careful using cartesian_product
with expensive or side-effecting functions. If the second sequence is infinite, some values in the first sequence may not be reached.
Returns a sequence that eventually reaches every possible pair of elements of the inputs, even if either or both are infinite. The step function of both inputs may be applied to the same state repeatedly, so be careful using interleaved_cartesian_product
with expensive or side-effecting functions.
intersperse xs ~sep
produces sep
between adjacent elements of xs
, e.g., intersperse [1;2;3] ~sep:0 = [1;0;2;0;3]
.
val cycle_list_exn : 'a list -> 'a t
cycle_list_exn xs
repeats the elements of xs
forever. If xs
is empty, it raises.
val repeat : 'a -> 'a t
repeat a
repeats a
forever.
val singleton : 'a -> 'a t
singleton a
produces a
exactly once.
val delayed_fold :
'a t ->
init:'s ->
f:( 's -> 'a -> k:( 's -> 'r ) -> 'r ) ->
finish:( 's -> 'r ) ->
'r
delayed_fold
allows to do an on-demand fold, while maintaining a state.
It is possible to exit early by not calling k
in f
. It is also possible to call k
multiple times. This results in the rest of the sequence being folded over multiple times, independently.
Note that delayed_fold
, when targeting JavaScript, can result in stack overflow as JavaScript doesn't generally have tail call optimization.
val fold_m :
bind:( 'acc_m -> f:( 'acc -> 'acc_m ) -> 'acc_m ) ->
return:( 'acc -> 'acc_m ) ->
'elt t ->
init:'acc ->
f:( 'acc -> 'elt -> 'acc_m ) ->
'acc_m
fold_m
is a monad-friendly version of fold
. Supply it with the monad's return
and bind
, and it will chain them through the computation.
val iter_m :
bind:( 'unit_m -> f:( unit -> 'unit_m ) -> 'unit_m ) ->
return:( unit -> 'unit_m ) ->
'elt t ->
f:( 'elt -> 'unit_m ) ->
'unit_m
iter_m
is a monad-friendly version of iter
. Supply it with the monad's return
and bind
, and it will chain them through the computation.
val to_list_rev : 'a t -> 'a list
to_list_rev t
returns a list of the elements of t
, in reverse order. It is faster than to_list
.
val of_list : 'a list -> 'a t
of_lazy t_lazy
produces a sequence that forces t_lazy
the first time it needs to compute an element.
memoize t
produces each element of t
, but also memoizes them so that if you consume the same element multiple times it is only computed once. It's a non-eager version of force_eagerly
.
force_eagerly t
precomputes the sequence. It is behaviorally equivalent to of_list
(to_list t)
, but may at some point have a more efficient implementation. It's an eager version of memoize
.
val bounded_length : _ t -> at_most:int -> [ `Is of int | `Greater ]
bounded_length ~at_most t
returns `Is len
if len = length t <= at_most
, and otherwise returns `Greater
. Walks through only as much of the sequence as necessary. Always returns `Greater
if at_most < 0
.
val length_is_bounded_by : ?min:int -> ?max:int -> _ t -> bool
length_is_bounded_by ~min ~max t
returns true if min <= length t
and length t <=
max
When min
or max
are not provided, the check for that bound is omitted. Walks through only as much of the sequence as necessary.
val of_seq : 'a Caml.Seq.t -> 'a t
val to_seq : 'a t -> 'a Caml.Seq.t
Generator
is a monadic interface to generate sequences in a direct style, similar to Python's generators.
Here are some examples:
open Generator
let rec traverse_list = function
| [] -> return ()
| x :: xs -> yield x >>= fun () -> traverse_list xs
let traverse_option = function
| None -> return ()
| Some x -> yield x
let traverse_array arr =
let n = Array.length arr in
let rec loop i =
if i >= n then return () else yield arr.(i) >>= fun () -> loop (i + 1)
in
loop 0
let rec traverse_bst = function
| Node.Empty -> return ()
| Node.Branch (left, value, right) ->
traverse_bst left >>= fun () ->
yield value >>= fun () ->
traverse_bst right
let sequence_of_list x = Generator.run (traverse_list x)
let sequence_of_option x = Generator.run (traverse_option x)
let sequence_of_array x = Generator.run (traverse_array x)
let sequence_of_bst x = Generator.run (traverse_bst x)
module Generator : sig ... end
module Expert : sig ... end
The functions in Expert
expose internal structure which is normally meant to be hidden. For example, at least when f
is purely functional, it is not intended for client code to distinguish between