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

See Stdlib.Seq.iter

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

See Stdlib.Seq.fold_left

val iteri : (int -> 'a -> unit callermon) -> 'a t -> unit mon

See Stdlib.Seq.iteri

val fold_lefti : ('b -> int -> 'a -> 'b callermon) -> 'b -> 'a t -> 'b mon

See Stdlib.Seq.fold_lefti

val for_all : ('a -> bool callermon) -> 'a t -> bool mon

See Stdlib.Seq.for_all

val exists : ('a -> bool callermon) -> 'a t -> bool mon

See Stdlib.Seq.exists

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

See Stdlib.Seq.find

val find_map : ('a -> 'b option callermon) -> 'a t -> 'b option mon

See Stdlib.Seq.find_map

val iter2 : ('a -> 'b -> unit callermon) -> 'a t -> 'b t -> unit mon

See Stdlib.Seq.iter2

val fold_left2 : ('a -> 'b -> 'c -> 'a callermon) -> 'a -> 'b t -> 'c t -> 'a mon

See Stdlib.Seq.fold_left2

val for_all2 : ('a -> 'b -> bool callermon) -> 'a t -> 'b t -> bool mon

See Stdlib.Seq.for_all2

val exists2 : ('a -> 'b -> bool callermon) -> 'a t -> 'b t -> bool mon

See Stdlib.Seq.exists2

val init : int -> (int -> 'a callermon) -> 'a t

See Stdlib.Seq.init

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

See Stdlib.Seq.unfold

val forever : (unit -> 'a callermon) -> 'a t

See Stdlib.Seq.forever

val iterate : ('a -> 'a callermon) -> 'a -> 'a t

See Stdlib.Seq.iterate

val map : ('a -> 'b callermon) -> 'a t -> 'b t

See Stdlib.Seq.map

val mapi : (int -> 'a -> 'b callermon) -> 'a t -> 'b t

See Stdlib.Seq.mapi

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

See Stdlib.Seq.filter

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

See Stdlib.Seq.filter_map

val scan : ('b -> 'a -> 'b callermon) -> 'b -> 'a t -> 'b t

See Stdlib.Seq.scan

val take_while : ('a -> bool callermon) -> 'a t -> 'a t

See Stdlib.Seq.take_while

val drop_while : ('a -> bool callermon) -> 'a t -> 'a t

See Stdlib.Seq.drop_while

val group : ('a -> 'a -> bool callermon) -> 'a t -> 'a t t

See Stdlib.Seq.group

val map2 : ('a -> 'b -> 'c callermon) -> 'a t -> 'b t -> 'c t

See Stdlib.Seq.map2

val map_product : ('a -> 'b -> 'c callermon) -> 'a t -> 'b t -> 'c t

See Stdlib.Seq.map_product

val partition_map : ('a -> ('b, 'c) Stdlib.Either.t callermon) -> 'a t -> 'b t * 'c t

See Stdlib.Seq.partition_map

val partition : ('a -> bool callermon) -> 'a t -> 'a t * 'a t

See Stdlib.Seq.partition