package seqes
Library
Module
Module type
Parameter
Class
Class type
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.
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.
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.
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.
See Stdlib.Seq.iter
See Stdlib.Seq.iteri
See Stdlib.Seq.find
See Stdlib.Seq.iter2
See Stdlib.Seq.init
See Stdlib.Seq.map
See Stdlib.Seq.mapi
See Stdlib.Seq.scan
See Stdlib.Seq.group
See Stdlib.Seq.map2