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.
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.
Sourceval findi : (int ->'a-> bool)->'a list-> int * 'a
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.
Sourceval unique : ?cmp:('a->'a-> bool)->'a list->'a list
unique cmp l returns the list l without any duplicate element. Default comparator ( = ) is used if no comparison function specified.
Sourceval filter_map : ('a->'b option)->'a list->'b list
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.
Sourceval split_nth : int ->'a list->'a list * 'a list
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.
Sourceval filteri : (int ->'a-> bool)->'a list->'a list
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.
Sourceval fold_left_map : ('a->'b->'a * 'c)->'a->'b list->'a * 'c list
fold_left_map is a combination of fold_left and map that threads an accumulator through calls to f
drop n l returns l without the first n elements, or the empty list if l have less than n elements.
Sourceval takewhile : ('a-> bool)->'a list->'a list
takewhile f xs returns the first elements of list xs which satisfy the predicate f.
Sourceval dropwhile : ('a-> bool)->'a list->'a list
dropwhile f xs returns the list xs with the first elements satisfying the predicate f dropped.
Enum functions
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.
nth l n returns the n-th element of the list l or raise Invalid_index is the index is outside of l bounds.
Sourceval sort : ?cmp:('a->'a-> int)->'a list->'a list
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.
Sourceval map2 : ('a->'b->'c)->'a list->'b list->'c list
Sourceval rev_map2 : ('a->'b->'c)->'a list->'b list->'c list
Sourceval iter2 : ('a->'b-> unit)->'a list->'b list-> unit
Sourceval combine : 'a list->'b list->('a * 'b) list
Improved functions
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.
Sourceval remove_assoc : 'a->('a * 'b) list->('a * 'b) list
Sourceval remove_assq : 'a->('a * 'b) list->('a * 'b) list
Sourceval split : ('a * 'b) list->'a list * 'b list
The following functions were already tail-recursive in the List module but were using List.rev calls. The new implementations have better performances.
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.
Sourceval compare : ('a->'a-> int)->'a list->'a list-> int
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
the empty list [] is strictly smaller than non-empty lists
Note: the cmp function will be called even if the lists have different lengths.