package tezos-lwt-result-stdlib

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
include Bare_sigs.Seq.S

including the OCaml's Stdlib.Seq module to share the Seq.t type (including concrete definition) and to bring the existing functions.

include module type of Seq with type 'a t = 'a Seq.t and type 'a node = 'a Seq.node

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 = 'a Seq.t

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 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.

in-monad, preallocated empty/nil

val empty_e : ('a t, 'trace) result
val empty_s : 'a t Lwt.t
val empty_es : ('a t, 'trace) result Lwt.t
val nil_e : ('a node, 'trace) result
val nil_s : 'a node Lwt.t
val nil_es : ('a node, 'trace) result Lwt.t
val fold_left_e : ('a -> 'b -> ('a, 'trace) result) -> 'a -> 'b t -> ('a, 'trace) result

Similar to fold_left but wraps the traversal in result. The traversal is interrupted if one of the step returns an Error _.

val fold_left_s : ('a -> 'b -> 'a Lwt.t) -> 'a -> 'b t -> 'a Lwt.t

Similar to fold_left but wraps the traversing in Lwt. Each step of the traversal is started after the previous one has resolved. The traversal is interrupted if one of the promise is rejected.

val fold_left_es : ('a -> 'b -> ('a, 'trace) result Lwt.t) -> 'a -> 'b t -> ('a, 'trace) result Lwt.t

Similar to fold_left but wraps the traversing in result Lwt.t. Each step of the traversal is started after the previous one resolved. The traversal is interrupted if one of the step is rejected or is fulfilled with Error _.

val iter_e : ('a -> (unit, 'trace) result) -> 'a t -> (unit, 'trace) result

Similar to iter but wraps the iteration in result. The iteration is interrupted if one of the step returns an Error _.

val iter_s : ('a -> unit Lwt.t) -> 'a t -> unit Lwt.t

Similar to iter but wraps the iteration in Lwt. Each step of the iteration is started after the previous one resolved. The iteration is interrupted if one of the promise is rejected.

val iter_es : ('a -> (unit, 'trace) result Lwt.t) -> 'a t -> (unit, 'trace) result Lwt.t

Similar to iter but wraps the iteration in result Lwt.t. Each step of the iteration is started after the previous one resolved. The iteration is interrupted if one of the promise is rejected of fulfilled with an Error _.

val iter_p : ('a -> unit Lwt.t) -> 'a t -> unit Lwt.t

Similar to iter but wraps the iteration in Lwt. All the steps of the iteration are started concurrently. The promise iter_p f s is resolved only once all the promises of the iteration are. At this point it is either fulfilled if all promises are, or rejected if at least one of them is.

val map_e : ('a -> ('b, 'trace) result) -> 'a t -> ('b t, 'trace) result

Similar to map but wraps the transformation in result. The traversal is interrupted if any of the application returns an Error _.

Note that, unlike map, map_e is not lazy: it applies the transformation immediately to all the elements of the sequence (unless it is interrupted by an Error _) and does not terminate on infinite sequences (again, unless interrupted). Moreover map_e is not tail-recursive.

val map_s : ('a -> 'b Lwt.t) -> 'a t -> 'b t Lwt.t

Similar to map but wraps the transformation in Lwt. Each transformation is done sequentially, only starting once the previous one has resolved. The traversal is interrupted if any of the promise is rejected.

Note that, unlike map, map_s is not lazy: it applies the transformation eagerly to all the elements of the sequence (unless interrupted by a rejection) and does not terminate on infinite sequences (again, unless interrupted). Moreover map_s is not tail-recursive.

val map_es : ('a -> ('b, 'trace) result Lwt.t) -> 'a t -> ('b t, 'trace) result Lwt.t

Similar to map but wraps the transformation in result Lwt.t. Each transformation is done sequentially, only starting once the previous one has resolved. The traversal is interrupted if any of the promise is rejected or fulfilled with an Error _.

Note that, unlike map, map_es is not lazy: it applies the transformation eagerly to all the elements of the sequence (unless interrupted by rejection or an Error _) and does not terminate on infinite sequences (again, unless interrupted). Moreover map_es is not tail-recursive.

val map_p : ('a -> 'b Lwt.t) -> 'a t -> 'b t Lwt.t

Similar to map but wraps the transformation in Lwt. All the transformations are done concurrently. The promise map_p f s resolves once all the promises of the traversal resolve. At this point it is fulfilled if all the promises are, and it is rejected if any of them are.

Note that, unlike map, map_p is not lazy: it applies the transformation eagerly to all the elements of the sequence and does not terminate on infinite sequences. Moreover map_p is not tail-recursive.

val filter_e : ('a -> (bool, 'trace) result) -> 'a t -> ('a t, 'trace) result

Similar to filter but wraps the transformation in result. Note that, unlike filter, filter_e is not lazy: it applies the transformation immediately and does not terminate on infinite sequences. Moreover filter_e is not tail-recursive.

val filter_s : ('a -> bool Lwt.t) -> 'a t -> 'a t Lwt.t

Similar to filter but wraps the transformation in Lwt.t. Each test of the predicate is done sequentially, only starting once the previous one has resolved. Note that, unlike filter, filter_s is not lazy: it applies the transformation immediately and does not terminate on infinite sequences. Moreover filter_s is not tail-recursive.

val filter_es : ('a -> (bool, 'trace) result Lwt.t) -> 'a t -> ('a t, 'trace) result Lwt.t

Similar to filter but wraps the transformation in result Lwt.t. Each test of the predicate is done sequentially, only starting once the previous one has resolved. Note that, unlike filter, filter_es is not lazy: it applies the transformation immediately and does not terminate on infinite sequences. Moreover filter_es is not tail-recursive.

val filter_map_e : ('a -> ('b option, 'trace) result) -> 'a t -> ('b t, 'trace) result

Similar to filter_map but within result. Not lazy and not tail-recursive.

val filter_map_s : ('a -> 'b option Lwt.t) -> 'a t -> 'b t Lwt.t

Similar to filter_map but within Lwt.t. Not lazy and not tail-recursive.

val filter_map_es : ('a -> ('b option, 'trace) result Lwt.t) -> 'a t -> ('b t, 'trace) result Lwt.t

Similar to filter_map but within result Lwt.t. Not lazy and not tail-recursive.

val find : ('a -> bool) -> 'a t -> 'a option

find f t is Some x where x is the first item in t such that f x. It is None if there are no such element. It does not terminate if the sequence is infinite and the predicate is always false.

val find_e : ('a -> (bool, 'trace) result) -> 'a t -> ('a option, 'trace) result

find_e f t is similar to find but wraps the search within result. Specifically, find_e f t is either

  • Ok (Some x) if forall y before x f y = Ok false and f x = Ok true,
  • Error e if there exists x such that forall y before x f y = Ok false and f x = Error e,
  • Ok None otherwise and t is finite,
  • an expression that never returns otherwise.
val find_s : ('a -> bool Lwt.t) -> 'a t -> 'a option Lwt.t

find_s f t is similar to find but wrapped within Lwt.t. The search is identical to find_e but each predicate is applied when the previous one has resolved.

val find_es : ('a -> (bool, 'trace) result Lwt.t) -> 'a t -> ('a option, 'trace) result Lwt.t

find_es f t is similar to find but wrapped within result Lwt.t. The search is identical to find_e but each predicate is applied when the previous one has resolved.

val iter_ep : ('a -> (unit, 'error Monad.trace) result Lwt.t) -> 'a t -> (unit, 'error Monad.trace) result Lwt.t

Similar to iter but wraps the iteration in result Lwt.t. All the steps of the iteration are started concurrently. The promise iter_ep resolves once all the promises of the traversal resolve. At this point it either:

  • is rejected if at least one of the promises is, otherwise
  • is fulfilled with Error _ if at least one of the promises is, otherwise
  • is fulfilled with Ok () if all the promises are.
val map_ep : ('a -> ('b, 'error Monad.trace) result Lwt.t) -> 'a t -> ('b t, 'error Monad.trace) result Lwt.t

Similar to map but wraps the transformation in result Lwt. All the transformations are done concurrently. The promise map_ep f s resolves once all the promises of the traversal resolve. At this point it is rejected if any of the promises are, and otherwise it is resolved with Error _ if any of the promises are, and otherwise it is fulfilled (if all the promises are).

Note that, unlike map, map_ep is not lazy: it applies the transformation eagerly to all the elements of the sequence and does not terminate on infinite sequences. Moreover map_ep is not tail-recursive.

OCaml

Innovation. Community. Security.