package seqes

  1. Overview
  2. Docs

Transformers

A transformer is a function that traverses a sequence, applying a caller-provided function on the sequence's elements, returning a sequence. E.g., map.

A transformer may traverse only a part of the sequence. E.g., drop_while.

Other functions do not necessarily fit the exact description of traversors but have similar characteristic in their use of monads. E.g., init does not consume any sequence.

The type of a transformer mentions two distinct monad types:

  • 'a mon: the monad that the sequence is specialised to (sometimes this type is mentioned implicitly as a component of the 'a t type)
  • 'a callermon: the monad that the caller-provided functions use

These two monad types can be different. The main use for these types being distinct is to provide both plain-traversors (e.g., the plain-map has type ('a -> 'b) -> 'a t -> 'b t) and mon-traversors (e.g., the mon-map has type ('a -> 'b mon) -> 'a t -> 'b t). Plain-traversors are obtained with type 'a callermon := 'a whereas mon-traversors are obtained with type 'a callermon := 'a mon.

There are more advanced use for tiers of monads. See examples/seqlwtres/seqlwtres.ml for an advanced example involving Lwt and result.

Because a transformer returns a new sequence, and because that sequence carries within it the mon monad, there are restrictions on what the caller monad can be. These restrictions are imposed onto you by the functors that produce Transformer modules. Specifically, these functors expect a function to lift one monad into the other.

Because the nature of the transformers impose a restriction on the kind of callermon that can be used, it is always possible to generate traversors for these monads. Consequently, the SEQMON1TRANSFORMERS signature includes the SEQMON1TRAVERSORS signature and the functors that generate transformers also generate traversors.

type 'a callermon

callermon is the type constructor for the monad used in caller-provided functions.

The type is meant to be substituted by the functor that produces modules following this signature.

type 'a mon

mon is the type constructor for the monad used in the sequence.

The type is meant to be substituted by the functor that produces modules of following this signature.

type 'a t

t is the type constructor for the sequence.

The type is meant to be substituted by the functor that produces modules of following this signature.

Any monad that we can use to produce transformers, we can also use to produce traversors. Thus, SEQMON1TRANSFORMERS includes SEQMON1TRAVERSORS and all the functors producing transformer also produce traversors.

val iter : ('a -> unit callermon) -> 'a t -> unit mon
val fold_left : ('a -> 'b -> 'a callermon) -> 'a -> 'b t -> 'a mon
val iteri : (int -> 'a -> unit callermon) -> 'a t -> unit mon
val fold_lefti : ('b -> int -> 'a -> 'b callermon) -> 'b -> 'a t -> 'b mon
val for_all : ('a -> bool callermon) -> 'a t -> bool mon
val exists : ('a -> bool callermon) -> 'a t -> bool mon
val find : ('a -> bool callermon) -> 'a t -> 'a option mon
val find_map : ('a -> 'b option callermon) -> 'a t -> 'b option mon
val iter2 : ('a -> 'b -> unit callermon) -> 'a t -> 'b t -> unit mon
val fold_left2 : ('a -> 'b -> 'c -> 'a callermon) -> 'a -> 'b t -> 'c t -> 'a mon
val for_all2 : ('a -> 'b -> bool callermon) -> 'a t -> 'b t -> bool mon
val exists2 : ('a -> 'b -> bool callermon) -> 'a t -> 'b t -> bool mon
val init : int -> (int -> 'a callermon) -> 'a t
val unfold : ('b -> ('a * 'b) option callermon) -> 'b -> 'a t
val forever : (unit -> 'a callermon) -> 'a t
val iterate : ('a -> 'a callermon) -> 'a -> 'a t
val map : ('a -> 'b callermon) -> 'a t -> 'b t
val mapi : (int -> 'a -> 'b callermon) -> 'a t -> 'b t
val filter : ('a -> bool callermon) -> 'a t -> 'a t
val filter_map : ('a -> 'b option callermon) -> 'a t -> 'b t
val scan : ('b -> 'a -> 'b callermon) -> 'b -> 'a t -> 'b t
val take_while : ('a -> bool callermon) -> 'a t -> 'a t
val drop_while : ('a -> bool callermon) -> 'a t -> 'a t
val group : ('a -> 'a -> bool callermon) -> 'a t -> 'a t t
val map2 : ('a -> 'b -> 'c callermon) -> 'a t -> 'b t -> 'c t
val map_product : ('a -> 'b -> 'c callermon) -> 'a t -> 'b t -> 'c t
val partition_map : ('a -> ('b, 'c) Either.t callermon) -> 'a t -> 'b t * 'c t
val partition : ('a -> bool callermon) -> 'a t -> 'a t * 'a t