Library
Module
Module type
Parameter
Class
Class type
Similar to Array.init
, init n f
returns the list containing the results of (f 0),(f 1).... (f (n-1)). Raise Invalid_arg "ExtList.init"
if n < 0. Uses stdlib implementation in OCaml 4.06.0 and newer.
Similar to String.make
, make n x
returns a * list containing n
elements x
.
Returns the first element of the list, or raise Empty_list
if the list is empty (similar to hd
).
Returns the last element of the list, or raise Empty_list
if the list is empty. This function takes linear time.
iteri f l
will call (f 0 a0);(f 1 a1) ... (f n an)
where a0..an
are the elements of the list l
.
mapi f l
will build the list containing (f 0 a0);(f 1 a1) ... (f n an)
where a0..an
are the elements of the list l
.
rfind p l
returns the last element x
of l
such as p x
returns true
or raises Not_found
if such element as not been found.
find_exc p e l
returns the first element of l
such as p x
returns true
or raises e
if such element as not been found.
findi p e l
returns the first element ai
of l
along with its index i
such that p i ai
is true, or raises Not_found
if no such element has been found.
unique cmp l
returns the list l
without any duplicate element. Default comparator ( = ) is used if no comparison function specified.
filter_map f l
call (f a0) (f a1).... (f an)
where a0..an
are the elements of l
. It returns the list of elements bi
such as f ai = Some bi
(when f
returns None
, the corresponding element of l
is discarded).
find_map_exn pred list
finds the first element of list
for which pred element
returns Some r
. It returns r
immediately once found or raises Not_found
if no element matches the predicate. See also filter_map
. This function was called find_map
prior to extlib 1.7.7, but had to be renamed to stay compatible with OCaml 4.10.
same as find_map_exn but returning option
split_nth n l
returns two lists l1
and l2
, l1
containing the first n
elements of l
and l2
the others. Raise Invalid_index
if n
is outside of l
size bounds.
Same as List.filter
, but the predicate is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.
fold_left_map
is a combination of fold_left
and map
that threads an accumulator through calls to f
remove l x
returns the list l
without the first element x
found or returns l
if no element is equal to x
. Elements are compared using ( = ).
remove_if cmp l
is similar to remove
, but with cmp
used instead of ( = ).
remove_all l x
is similar to remove
but removes all elements that are equal to x
and not only the first one.
take n l
returns up to the n
first elements from list l
, if available.
drop n l
returns l
without the first n
elements, or the empty list if l
have less than n
elements.
takewhile f xs
returns the first elements of list xs
which satisfy the predicate f
.
dropwhile f xs
returns the list xs
with the first elements satisfying the predicate f
dropped.
Enumerations are important in ExtLib, they are a good way to work with abstract enumeration of elements, regardless if they are located in a list, an array, or a file.
val enum : 'a list -> 'a Enum.t
Returns an enumeration of the elements of a list.
val of_enum : 'a Enum.t -> 'a list
Build a list from an enumeration.
Some minor modifications have been made to the specification of some functions, especially concerning exceptions raised.
Returns the first element of the list or raise Empty_list
if the list is empty.
Returns the list without its first elements or raise Empty_list
if the list is empty.
nth l n
returns the n-th element of the list l
or raise Invalid_index
is the index is outside of l
bounds.
Sort the list using optional comparator (by default compare
).
The following functions have been improved so all of them are tail-recursive. They have also been modified so they no longer raise Invalid_arg
but Different_list_size
when used on two lists having a different number of elements.
The following functions have the same behavior as the List
module ones but are tail-recursive. That means they will not cause a Stack_overflow
when used on very long list.
The implementation might be a little more slow in bytecode, but compiling in native code will not affect performances.
The following functions were already tail-recursive in the List
module but were using List.rev
calls. The new implementations have better performances.
These functions are already part of the Ocaml standard library and have not been modified. Please refer to the Ocaml Manual for documentation.
Same as List.sort
, but also remove duplicates.
*_seq
functions were introduced in OCaml 4.07.0, and are _not_ implemented in extlib for older OCaml versions
partition_map f l
returns a pair of lists (l1, l2)
such that, for each element x
of the input list l
:
f x
is Left y1
, then y1
is in l1
, andf x
is Right y2
, then y2
is in l2
.The output elements are included in l1
and l2
in the same relative order as the corresponding input elements in l
.
In particular, partition_map (fun x -> if f x then Left x else Right x) l
is equivalent to partition f l
.
This function was introduced in OCaml 4.12.0, and is _not_ implemented in extlib for older OCaml versions
equal eq [a1; ...; an] [b1; ..; bm]
holds when the two input lists have the same length, and for each pair of elements ai
, bi
at the same position we have eq ai bi
.
Note: the eq
function may be called even if the lists have different length. If you know your equality function is costly, you may want to check compare_lengths
first.
compare cmp [a1; ...; an] [b1; ...; bm]
performs a lexicographic comparison of the two input lists, using the same 'a -> 'a -> int
interface as Stdlib.compare
:
a1 :: l1
is smaller than a2 :: l2
(negative result) if a1
is smaller than a2
, or if they are equal (0 result) and l1
is smaller than l2
[]
is strictly smaller than non-empty listsNote: the cmp
function will be called even if the lists have different lengths.
Empty_list
is raised when an operation applied on an empty list is invalid : hd
for example.
Invalid_index
is raised when an indexed access on a list is out of list bounds.