package travesty
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=40ada5c475cfeba7d933eec133294d1b5ef5da6ce864c9746b2ce4ce49b5e3a4
md5=dc818d6b232f13edb388d25781cd99a2
doc/travesty/Travesty/T_list/index.html
Module Travesty.T_list
Source
List extensions.
This module contains various extensions for Core's List
module, including adding monadic traversal.
t
is just list
, re-exported to make this module satisfy various container interfaces.
Lists are traversable containers.
include Traversable.S1_container with type 'a t := 'a t
module On_monad
(M : Base.Monad.S) :
Traversable.On_monad1 with type 'a t := 'a t and module M := M
On_monad
implements monadic folding and mapping operators for a given monad M
, including arity-1 specific operators.
module With_errors :
Traversable.On_monad1 with type 'a t := 'a t and module M := Base.Or_error
With_errors
is shorthand for On_monad (Or_error)
.
include Traversable.Generic_container
with type 'a t := 'a t
and type 'a elt := 'a
and module On_monad := On_monad
and module With_errors := With_errors
include Types_intf.Generic with type 'a t := 'a t with type 'a elt := 'a
We can do generic container operations.
include Base.Container.Generic with type 'a t := 'a t and type 'a elt := 'a
We can do non-monadic mapping operations.
include Mappable.Generic with type 'a t := 'a t and type 'a elt := 'a
Generic
refers to the container type as 'a t
, and the element type as 'a elt
; substitute t
/elt
(arity-0) or 'a t
/'a
(arity-1) accordingly below.
include Types_intf.Generic with type 'a t := 'a t with type 'a elt := 'a
fold_map c ~f ~init
folds f
over every t
in c
, threading through an accumulator with initial value init
.
include Mappable.S1_container with type 'a t := 'a t
include Mappable.S1 with type 'a t := 'a t
include Mappable.Generic with type 'a t := 'a t and type 'a elt := 'a
Generic
refers to the container type as 'a t
, and the element type as 'a elt
; substitute t
/elt
(arity-0) or 'a t
/'a
(arity-1) accordingly below.
include Types_intf.Generic with type 'a t := 'a t with type 'a elt := 'a
include Base.Container.S1 with type 'a t := 'a t
val mem : 'a t -> 'a -> equal:('a -> 'a -> bool) -> bool
val length : 'a t -> int
val is_empty : 'a t -> bool
val iter : 'a t -> f:('a -> unit) -> unit
val fold : 'a t -> init:'accum -> f:('accum -> 'a -> 'accum) -> 'accum
val fold_result :
'a t ->
init:'accum ->
f:('accum -> 'a -> ('accum, 'e) Base__.Result.t) ->
('accum, 'e) Base__.Result.t
val fold_until :
'a t ->
init:'accum ->
f:('accum -> 'a -> ('accum, 'final) Base__Container_intf.Continue_or_stop.t) ->
finish:('accum -> 'final) ->
'final
val exists : 'a t -> f:('a -> bool) -> bool
val for_all : 'a t -> f:('a -> bool) -> bool
val count : 'a t -> f:('a -> bool) -> int
val sum :
(module Base__Container_intf.Summable with type t = 'sum) ->
'a t ->
f:('a -> 'sum) ->
'sum
val find : 'a t -> f:('a -> bool) -> 'a option
val find_map : 'a t -> f:('a -> 'b option) -> 'b option
val to_list : 'a t -> 'a list
val to_array : 'a t -> 'a array
val min_elt : 'a t -> compare:('a -> 'a -> int) -> 'a option
val max_elt : 'a t -> compare:('a -> 'a -> int) -> 'a option
include Mappable.Extensions1 with type 'a t := 'a t
Extensions1
includes the container extensions from T_container, as they work with any arity-1 container.
include T_container.Extensions1 with type 'a t := 'a t
include T_container.Generic_extensions
with type 'a t := 'a t
and type 'a elt := 'a
Generic_extensions
refers to the container type as 'a t
, and the element type as 'a elt
; substitute t
/elt
(arity-0) or 'a t
/'a
(arity-1) accordingly below.
include Types_intf.Generic with type 'a t := 'a t with type 'a elt := 'a
Testing for a specific number of elements
The following functions help in checking whether a container has a particular, commonly-required number of elements (zero or one, one, two, and so on).
at_most_one xs
returns Ok None
if xs
is empty; Ok Some(x)
if it contains only x
; and an error otherwise.
Examples:
T_list.at_most_one [] (* ok None *)
at_most_one [1] (* ok (Some 1) *)
at_most_one [1; 2] (* error -- too many *)
one xs
returns Ok x
if xs
contains only x
, and an error otherwise.
Examples:
T_list.one [] (* error -- not enough *)
one [1] (* ok 1 *)
one [1; 2] (* error -- too many *)
two xs
returns Ok (x, y)
if xs
is a list containing only x
and y
in that order, and an error otherwise.
Examples:
T_list.two [] (* error -- not enough *)
two [1] (* error -- not enough *)
two [1; 2] (* ok (1, 2) *)
two [1; 2; 3] (* error -- too many *)
Miscellaneous extensions
Predicate extensions are available on all arity-1 containers, provided that we fix the element type parameter to 'a -> bool
.
include T_container.Generic_predicate_extensions
with type 'a t := ('a -> Base.bool) t
and type 'a item := 'a
any x ~predicates
tests x
against predicates
until one returns true
, in which case it returns true
; or all return false
, in which case it returns false
.
any x ~predicates
tests x
against predicates
until one returns false
, in which case it returns false
; or all return true
, in which case it returns true
.
right_pad ~padding xs
pads every list in xs with padding
, ensuring all lists have equal length.
Example:
right_pad ~padding:6
[ [0; 8; 0; 0] (* [ [ 0; 8; 0; 0; 6 ] *)
; [9; 9; 9] (* ; [ 9; 9; 9; 6; 6 ] *)
; [8; 8; 1; 9; 9] (* ; [ 8; 8; 1; 9; 9 ] *)
; [9; 1; 1; 9] (* ; [ 9; 1; 1; 9; 6 ] *)
; [7; 2; 5] (* ; [ 7; 2; 5; 6; 6 ] *)
; [3] (* ; [ 3; 6; 6; 6; 6 ] *)
] (* ] *)
We can also filter-map over them.
include Filter_mappable.S1 with type 'a t := 'a t
include Filter_mappable.Generic with type 'a t := 'a t and type 'a elt := 'a
Generic
strictly extends Generic_basic
.
include Filter_mappable.Generic_basic
with type 'a t := 'a t
with type 'a elt := 'a
Generic_basic
refers to the container type as 'a t
, and the element type as 'a elt
; substitute t
/elt
(arity-0) or 'a t
/'a
(arity-1) accordingly below.
include Types_intf.Generic with type 'a t := 'a t with type 'a elt := 'a
filter c ~f
checks f
over every t
in c
, discarding any items for which f
returns false
.
exclude c ~f
checks f
over every t
in c
, discarding any items for which f
returns true
.
We can also derive Mappable interfaces from filter-mappable ones, but leave that to a separate functor.