module List: CCList
type'a
sequence =('a -> unit) -> unit
type'a
gen =unit -> 'a option
type'a
klist =unit -> [ `Cons of 'a * 'a klist | `Nil ]
type'a
printer =Format.formatter -> 'a -> unit
type'a
random_gen =Random.State.t -> 'a
include List
type'a
t ='a list
val empty : 'a t
val is_empty : 'a t -> bool
is_empty l
returns true
iff l = []
val map : ('a -> 'b) -> 'a t -> 'b t
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
map
with reversed argumentsval cons : 'a -> 'a t -> 'a t
cons x l
is x::l
val append : 'a t -> 'a t -> 'a t
val cons_maybe : 'a option -> 'a t -> 'a t
cons_maybe (Some x) l
is x :: l
cons_maybe None l
is l
val (@) : 'a t -> 'a t -> 'a t
val filter : ('a -> bool) -> 'a t -> 'a t
List.filter
val fold_right : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
fold_right
val fold_while : ('a -> 'b -> 'a * [ `Continue | `Stop ]) -> 'a -> 'b t -> 'a
('a, `Stop)
is
indicated by the accumulatorval fold_map : ('acc -> 'a -> 'acc * 'b) -> 'acc -> 'a list -> 'acc * 'b list
fold_map f acc l
is a fold_left
-like function, but it also maps the
list to another list.val scan_left : ('acc -> 'a -> 'acc) -> 'acc -> 'a list -> 'acc list
scan_left f acc l
returns the list [acc; f acc x0; f (f acc x0) x1; …]
where x0
, x1
, etc. are the elements of l
val fold_map2 : ('acc -> 'a -> 'b -> 'acc * 'c) ->
'acc -> 'a list -> 'b list -> 'acc * 'c list
fold_map2
is to fold_map
what List.map2
is to List.map
.Invalid_argument
if the lists do not have the same lengthval fold_filter_map : ('acc -> 'a -> 'acc * 'b option) -> 'acc -> 'a list -> 'acc * 'b list
fold_filter_map f acc l
is a fold_left
-like function, but also
generates a list of output in a way similar to CCList.filter_map
val fold_flat_map : ('acc -> 'a -> 'acc * 'b list) -> 'acc -> 'a list -> 'acc * 'b list
fold_flat_map f acc l
is a fold_left
-like function, but it also maps the
list to a list of lists that is then flatten
'd..val init : int -> (int -> 'a) -> 'a t
Array.init
val combine : 'a list -> 'b list -> ('a * 'b) list
List.combine
but tail-recursive.Invalid_argument
if the lists have distinct lengths.val combine_gen : 'a list -> 'b list -> ('a * 'b) gen
CCList.combine
.
Unlike CCList.combine
, it does not fail if the lists have different
lengths;
instead, the output has as many pairs as the smallest input list.val split : ('a * 'b) t -> 'a t * 'b t
List.split
.val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
val flatten : 'a t t -> 'a t
val product : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
val fold_product : ('c -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c
val cartesian_product : 'a t t -> 'a t t
# cartesian_product [[1;2];[3];[4;5;6]] =
[[1;3;4];[1;3;5];[1;3;6];[2;3;4];[2;3;5];[2;3;6]];;
# cartesian_product [[1;2];[];[4;5;6]] = [];;
# cartesian_product [[1;2];[3];[4];[5];[6]] =
[[1;3;4;5;6];[2;3;4;5;6]];;
invariant: cartesian_product l = map_product id l
.val map_product_l : ('a -> 'b list) -> 'a list -> 'b list list
map_product_l f l
maps each element of l
to a list of
objects of type 'b
using f
.
We obtain [l1;l2;…;ln]
where length l=n
and li : 'b list
.
Then, it returns all the ways of picking exactly one element per li
.val diagonal : 'a t -> ('a * 'a) t
list_diagonal l
will
return the list of List.nth i l, List.nth j l
if i < j
.val partition_map : ('a -> [< `Drop | `Left of 'b | `Right of 'c ]) ->
'a list -> 'b list * 'c list
partition_map f l
maps f
on l
and gather results in lists:f x = `Left y
, adds y
to the first listf x = `Right z
, adds z
to the second listf x = `Drop
, ignores x
val sublists_of_len : ?last:('a list -> 'a list option) ->
?offset:int -> int -> 'a list -> 'a list list
sublists_of_len n l
returns sub-lists of l
that have length n
.
By default, these sub-lists are non overlapping:
sublists_of_len 2 [1;2;3;4;5;6]
returns [1;2]; [3;4]; [5;6]
Examples:
sublists_of_len 2 [1;2;3;4;5;6] = [[1;2]; [3;4]; [5;6]]
sublists_of_len 2 ~offset:3 [1;2;3;4;5;6] = [1;2];[4;5]
sublists_of_len 3 ~last:CCOpt.return [1;2;3;4] = [1;2;3];[4]
sublists_of_len 2 [1;2;3;4;5] = [[1;2]; [3;4]]
Invalid_argument
if offset <= 0
or n <= 0
last
: if provided and the last group of elements g
is such
that length g < n
, last g
is called. If last g = Some g'
,
g'
is appended; otherwise g
is dropped.
If last = CCOpt.return
, it will simply keep the last group.
By default, last = fun _ -> None
, i.e. the last group is dropped if shorter than n
.offset
: the number of elements skipped between two consecutive
sub-lists. By default it is n
. If offset < n
, the sub-lists
will overlap; if offset > n
, some elements will not appear at all.val pure : 'a -> 'a t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
val (<$>) : ('a -> 'b) -> 'a t -> 'b t
val return : 'a -> 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
val take : int -> 'a t -> 'a t
n
first elements, drop the restval drop : int -> 'a t -> 'a t
n
first elements, keep the restval hd_tl : 'a t -> 'a * 'a t
hd_tl (x :: l)
returns hd, l
.Failure
if the list is emptyval take_drop : int -> 'a t -> 'a t * 'a t
take_drop n l
returns l1, l2
such that l1 @ l2 = l
and
length l1 = min (length l) n
val take_while : ('a -> bool) -> 'a t -> 'a t
val drop_while : ('a -> bool) -> 'a t -> 'a t
val take_drop_while : ('a -> bool) -> 'a t -> 'a t * 'a t
take_drop_while p l = take_while p l, drop_while p l
val last : int -> 'a t -> 'a t
last n l
takes the last n
elements of l
(or less if
l
doesn't have that many elementsval head_opt : 'a t -> 'a option
val last_opt : 'a t -> 'a option
val find_pred : ('a -> bool) -> 'a t -> 'a option
find_pred p l
finds the first element of l
that satisfies p
,
or returns None
if no element satisfies p
val find_pred_exn : ('a -> bool) -> 'a t -> 'a
val find_map : ('a -> 'b option) -> 'a t -> 'b option
find_map f l
traverses l
, applying f
to each element. If for
some element x
, f x = Some y
, then Some y
is returned. Otherwise
the call returns None
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
find_idx p x
returns Some (i,x)
where x
is the i
-th element of l
,
and p x
holds. Otherwise returns None
val remove : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t
remove ~x l
removes every instance of x
from l
. Tailrec.eq
: equality functionval filter_map : ('a -> 'b option) -> 'a t -> 'b t
val keep_some : 'a option t -> 'a t
filter_some l
retains only elements of the form Some x
.
Same as filter_map CCFun.id
val keep_ok : ('a, 'b) Result.result t -> 'a t
filter_some l
retains only elements of the form Some x
.
Same as filter_map CCFun.id
val all_some : 'a option t -> 'a t option
all_some l
returns Some l'
if all elements of l
are of the form Some x
,
or None
otherwise.val all_ok : ('a, 'err) Result.result t -> ('a t, 'err) Result.result
all_ok l
returns Ok l'
if all elements of l
are of the form Ok x
,
or Error e
otherwise (with the first error met).val sorted_merge : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list
val sorted_merge_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
sorted_merge_uniq l1 l2
merges the sorted lists l1
and l2
and
removes duplicatesval is_sorted : ?cmp:('a -> 'a -> int) -> 'a list -> bool
is_sorted l
returns true
iff l
is sorted (according to given order)cmp
: the comparison function (default Pervasives.compare
)val sorted_insert : ?cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a list
sorted_insert x l
inserts x
into l
such that, if l
was sorted,
then sorted_insert x l
is sorted too.uniq
: if true and x
is already in sorted position in l
, then
x
is not duplicated. Default false
(x
will be inserted in any case).val uniq_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list
uniq_succ l
removes duplicate elements that occur one next to the other.
Examples:
uniq_succ [1;2;1] = [1;2;1]
uniq_succ [1;1;2] = [1;2]
val group_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list list
group_succ ~eq l
groups together consecutive elements that are equal
according to eq
val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
val iteri : (int -> 'a -> unit) -> 'a t -> unit
val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b
val get_at_idx : int -> 'a t -> 'a option
val get_at_idx_exn : int -> 'a t -> 'a
Not_found
if the index is invalidval set_at_idx : int -> 'a -> 'a t -> 'a t
val insert_at_idx : int -> 'a -> 'a t -> 'a t
val remove_at_idx : int -> 'a t -> 'a t
Those operations maintain the invariant that the list does not
contain duplicates (if it already satisfies it)
val add_nodup : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t
add_nodup x set
adds x
to set
if it was not already present. Linear time.val remove_one : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t
remove_one x set
removes one occurrence of x
from set
. Linear time.val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool
val subset : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t
CCList.sort_uniq
val union : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t
val inter : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t
val range_by : step:int -> int -> int -> int t
range_by ~step i j
iterates on integers from i
to j
included,
where the difference between successive elements is step
.
use a negative step
for a decreasing list.Invalid_argument
if step=0
val range : int -> int -> int t
range i j
iterates on integers from i
to j
included . It works
both for decreasing and increasing rangesval range' : int -> int -> int t
val (--) : int -> int -> int t
range
val (--^) : int -> int -> int t
range'
val replicate : int -> 'a -> 'a t
n
timesval repeat : int -> 'a t -> 'a t
n
timesmodule Assoc:sig
..end
module Ref:sig
..end
module type MONAD =sig
..end
module Traverse(
M
:
MONAD
)
:sig
..end
val random : 'a random_gen -> 'a t random_gen
val random_non_empty : 'a random_gen -> 'a t random_gen
val random_len : int -> 'a random_gen -> 'a t random_gen
val random_choose : 'a t -> 'a random_gen
Not_found
if the list is emptyval random_sequence : 'a random_gen t -> 'a t random_gen
val to_seq : 'a t -> 'a sequence
val of_seq : 'a sequence -> 'a t
val to_gen : 'a t -> 'a gen
val of_gen : 'a gen -> 'a t
val to_klist : 'a t -> 'a klist
val of_klist : 'a klist -> 'a t
open CCList.Infix
to access the infix operators
without cluttering the scope too much.module Infix:sig
..end
val pp : ?start:string ->
?stop:string ->
?sep:string -> 'a printer -> 'a t printer