package stdcompat

  1. Overview
  2. Docs
include module type of struct include Seq end

Functional Iterators

The type 'a t is a delayed list, i.e. a list where some evaluation is needed to access the next element. This makes it possible to build infinite sequences, to build sequences as we traverse them, and to transform them in a lazy fashion rather than upfront.

  • since 4.07
type 'a t = unit -> 'a node

The type of delayed lists containing elements of type 'a. Note that the concrete list node 'a node is delayed under a closure, not a lazy block, which means it might be recomputed every time we access it.

and +'a node = 'a Seq.node =
  1. | Nil
  2. | Cons of 'a * 'a t

A fully-evaluated list node, either empty or containing an element and a delayed tail.

val empty : 'a t

The empty sequence, containing no elements.

val return : 'a -> 'a t

The singleton sequence containing only the given element.

val cons : 'a -> 'a t -> 'a t

cons x xs is the sequence containing the element x followed by the sequence xs

  • since 4.11
val append : 'a t -> 'a t -> 'a t

append xs ys is the sequence xs followed by the sequence ys

  • since 4.11
val map : ('a -> 'b) -> 'a t -> 'b t

map f seq returns a new sequence whose elements are the elements of seq, transformed by f. This transformation is lazy, it only applies when the result is traversed.

If seq = [1;2;3], then map f seq = [f 1; f 2; f 3].

val filter : ('a -> bool) -> 'a t -> 'a t

Remove from the sequence the elements that do not satisfy the given predicate. This transformation is lazy, it only applies when the result is traversed.

val filter_map : ('a -> 'b option) -> 'a t -> 'b t

Apply the function to every element; if f x = None then x is dropped; if f x = Some y then y is returned. This transformation is lazy, it only applies when the result is traversed.

val flat_map : ('a -> 'b t) -> 'a t -> 'b t

Map each element to a subsequence, then return each element of this sub-sequence in turn. This transformation is lazy, it only applies when the result is traversed.

val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a

Traverse the sequence from left to right, combining each element with the accumulator using the given function. The traversal happens immediately and will not terminate on infinite sequences.

Also see List.fold_left

val iter : ('a -> unit) -> 'a t -> unit

Iterate on the sequence, calling the (imperative) function on every element. The traversal happens immediately and will not terminate on infinite sequences.

val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t

Build a sequence from a step function and an initial value. unfold f u returns empty if f u returns None, or fun () -> Cons (x, unfold f y) if f u returns Some (x, y).

For example, unfold (function [] -> None | h::t -> Some (h,t)) l is equivalent to List.to_seq l.

  • since 4.11
val concat_map : ('a -> 'b t) -> 'a t -> 'b t
val concat : 'a t t -> 'a t
val is_empty : (unit -> 'a node) -> bool
val uncons : (unit -> 'a node) -> ('a * 'a t) option
val length_rec : int -> 'a t -> int
val length : 'a t -> int
val iteri : (int -> 'a -> 'b) -> 'a t -> unit
val fold_lefti : ('a -> int -> 'b -> 'a) -> 'a -> 'b t -> 'a
val for_all : ('a -> bool) -> 'a t -> bool
val exists : ('a -> bool) -> 'a t -> bool
val find : ('a -> bool) -> 'a t -> 'a option
val find_map : ('a -> 'b option) -> 'a t -> 'b option
val iter2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> unit
val fold_left2 : ('a -> 'b -> 'c -> 'a) -> 'a -> 'b t -> 'c t -> 'a
val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
val equal : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
val compare : ('a -> 'b -> int) -> 'a t -> 'b t -> int
val init : int -> (int -> 'a) -> 'a t
val repeat : 'a -> 'a t
val forever : (unit -> 'a) -> 'a t
val cycle : (unit -> 'a node) -> unit -> 'a node
val iterate1 : ('a -> 'a) -> 'a -> 'a t
val iterate : ('a -> 'a) -> 'a -> unit -> 'a node
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
val scan : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a t
val take_rec : int -> 'a t -> 'a t
val take : int -> 'a t -> 'a t
val drop_rec : int -> 'a t -> 'a t
val drop : int -> 'a t -> 'a t
val take_while : ('a -> bool) -> 'a t -> 'a t
val drop_while_rec : ('a -> bool) -> 'a t -> 'a node
val drop_while : ('a -> bool) -> 'a t -> unit -> 'a node
val group : ('a -> 'a -> bool) -> (unit -> 'a node) -> 'a t t
val memoize : 'a t -> 'a t
exception Forced_twice
val once : 'a t -> 'a t
val transpose : (unit -> 'a t node) -> (unit -> 'a node) t
val zip : 'a t -> 'b t -> ('a * 'b) t
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
val interleave : 'a t -> 'a t -> 'a t
val sorted_merge1l : ('a -> 'a -> int) -> 'a node -> 'a -> 'a t -> 'a t -> 'a t
val sorted_merge1r : ('a -> 'a -> int) -> 'a t -> 'a node -> 'a -> 'a t -> 'a t
val sorted_merge1 : ('a -> 'a -> int) -> 'a node -> 'a -> 'a t -> 'a node -> 'a -> 'a t -> 'a node
val sorted_merge : ('a -> 'a -> int) -> (unit -> 'a node) -> (unit -> 'a node) -> unit -> 'a node
val map_product1 : ('a -> 'b -> 'c) -> 'a -> 'a t -> 'b t -> 'c node
val map_product : ('a -> 'b -> 'c) -> (unit -> 'a node) -> 'b t -> unit -> 'c node
val product : (unit -> 'a node) -> 'b t -> unit -> ('a * 'b) node
val unzip : ('a * 'b) t -> 'a t * 'b t
val split : ('a * 'b) t -> 'a t * 'b t
val partition_map : ('a -> ('b, 'c) Stdcompat__init.either) -> 'a t -> 'b t * 'c t
val partition : ('a -> bool) -> 'a t -> 'a t * 'a t
val of_dispenser : (unit -> 'a option) -> 'a t
val to_dispenser : 'a t -> unit -> 'a option
val ints : int -> int t
OCaml

Innovation. Community. Security.