Module Generic_util.Listx

module Listx: Generic_util_list
Useful functions on lists.

val (-<) : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b
val foldr : ('a -> 'b -> 'b) -> 'b -> 'a list -> 'b
foldr c n l = List.fold_right c l n
val cons : 'a -> 'a list -> 'a list
cons is useful when we want partial application of the list constructor.
val from_to : int -> int -> int list
from_to a b = [a; a+1; ... ; b]
val replicate : int -> 'a -> 'a list
replicate n x = [x; x; ...; x] with x repeated n times.
val with_indices : 'a list -> (int * 'a) list
with_indices [x0; x1; ...; xn] = [(0,x0); (1,x1); ... ; (n, xn)]
val find_index : ('a -> bool) -> 'a list -> int
find_index p xs is the index of the first element of xs for which the predicate p is true: p (List.nth xs (find_index p xs)) == true and (n < find_index p xs ==> p (List.nth xs n) == false
Raises Not_found if no element verifies the predicate
val index : 'a -> 'a list -> int
index x xs is the index of the first element of xs equal to x. index x = find_index (fun y -> x = y) List.nth xs (index x xs) = x

Uses Pervasives.(=)
Raises Not_found if x is not an element of the list

val find_opt : ('a -> bool) -> 'a list -> 'a option
findOpt p xs returns Some x if x is the first element of the list xs verifying the predicate p. or None if no element verifies the predicate. This function is total.

take_while p l returns the longest prefix of l whose elements make p true.
val take_while : ('a -> bool) -> 'a list -> 'a list

drop_while p l returns the suffix of l after removing the longest prefix of l whose elements make p true.
take_while p l @ drop_while p l = l

val drop_while : ('a -> bool) -> 'a list -> 'a list

find_some f l applies an option-valued function f to the elements of a list l and returns the first element that is not None or None if no such element exists.
val find_some : ('a -> 'b option) -> 'a list -> 'b option

filter_some f l applies f to every element x of the list l yielding either None which is thrown away, or Some y in which case y is kept.

filter p = filter_some (fun x -> if p x then Some x else None)

val filter_some : ('a -> 'b option) -> 'a list -> 'b list
val set : int -> 'a -> 'a list -> 'a list
set i x xs computes a new list where the i-th element of xs is x, the rest of the list is unchanged. Counting from 0.

If i is greater than the length of the list or if i is negative, then the original list is returned.

val concat_map : ('a -> 'b list) -> 'a list -> 'b list
concat_map f x = concat -< map f
concat_map is the flipped bind operator of the list monad
val monoid : 'a list Monoid.monoid
val monad : App.list' Monad.monad
exception Insert_duplicate
Raised by Generic_util_list.sl_insert when trying to insert an element that is already in the list.
val sl_insert : ('a -> 'a -> bool) -> 'a -> 'a list -> 'a list
sl_insert leq x xs inserts the new element x in the sorted list xs using the pre-order <=.
Raises Insert_duplicate when x is already in xs.
exception Match_list_failure
Raised by Generic_util_list.match_list.
val match_list : ('a -> 'b) -> 'a list -> 'b
match_list tries to apply a function to the first element of a list. If a Match_failure is caught, we proceed with the rest of the list. If no more element is available (empty list), the exception Match_list_failure is raised.
Raises Match_list_failure if the list is empty or if all the elements of the list caused a Match_failure.
val traverse : 'a Applicative.T.applicative ->
('b -> ('c, 'a) App.T.app) ->
'b list -> ('c list, 'a) App.T.app
val sequence : 'a Applicative.T.applicative ->
('b, 'a) App.T.app list -> ('b list, 'a) App.T.app