Module ListExtra.Extra (.ml)


module Extra: sig .. end
Extra definitions.


Generalization



Head and tail generalisation
val head : ?n:int -> 'a list -> 'a list
Similar to the standard List.hd, but retrieve the list of first elements (by default n=1 as in List.hd). Thus, the result is a list.
val tail : ?i:int -> 'a list -> 'a list
Similar to the standard List.tl, but the tail is extracted from the given index (by default i=1 as in List.tl)

Set operations


val substract : 'a list -> 'a list -> 'a list
Substract the second argument from the first
val subset : 'a list -> 'a list -> bool
subset a b check if a is a subset of b, i.e. if all elements of a belong to b.
val eqset : 'a list -> 'a list -> bool
eqset a b check if a and b represent the same set of values.
val intersection : 'a list -> 'a list -> 'a list
Intersection of list: AvB=A\(A\B) .
val foreach : 'a list -> ('a -> unit) -> unit
Shortcut for List.iter with arguments in the opposite order: before the list, then the action to perfom.
val uniq : 'a list -> 'a list
Returns a list with no duplicates. For large lists we suggest to use Hashset.uniq instead.

Indexes


val range : int -> int -> int list
range a b returns the list [a; (a+1); .. ; (b-1); b] containing all the values between the given limits (included) .
val interval : int -> int -> int list
Alias for range.
val indexes : 'a list -> int list
The list of indexes of a list. The first index is 0 as usually.
val asFunction : int list -> int -> int
Consider a list as a function from indexes to its content. The function is the identity outside the indexes of the list.

Selecting by indexes
val select : 'a list -> int list -> 'a list
Considering a list as a record and select some fields (indexes). Example:

# select ["aaa";"bbb";"ccc"] [1;2;0;1];; 
  : string list = ["bbb"; "ccc"; "aaa"; "bbb"]


Removing by indexes
val rmindex : 'a list -> int -> 'a list
Remove the element with the given index.

Searching for indexes
val indexSuchThat : ('a -> bool) -> 'a list -> int option
Heuristic searching for the index of an element satisfying a property.
val indexOf : 'a -> 'a list -> int option
Heuristic searching the first index of an element in a list
val firstIndexOf : 'a -> 'a list -> int option
Alias for indexOf.
val lastIndexOf : 'a -> 'a list -> int option
Heuristic searching the last index of an element in a list

Permutations


val shuffle : 'a list -> 'a list
Returns a permutation of the list.
val permute : (int -> int) -> 'a list -> 'a list
List permutation. The first argument is the function f that represents the permutation (we suppose that this function will be a bijection w.r.t. the set of indexes of the given list). In other words permute f l is the list [(f 0) ; (f 1) ; (f 2) ; ... ] .
val shuffler : 'a list -> int -> int
Returns a random permutation function for the given list.
val shuffleIndexes : 'a list -> int list
Returns a random list of indexes for the given list.

Folding


val big : ('a -> 'a -> 'a) -> 'a list -> 'a
The folding of lists is simply a List.fold_left specialization:

This function is adequate for most common cases. Use the module Big when maximum generality is requested.

Common foldings
val max : 'a list -> 'a
The polymorphic maximum of a list.
val min : 'a list -> 'a
The polymorphic minimum of a list.

List of lists


val transpose : 'a list list -> 'a list list
Transpose the matrix (list of lists). Example:
# List.transpose [[1;2;3]; [4;5;6]; [7;8;9]];;
  : int list list = [[1; 4; 7]; [2; 5; 8]; [3; 6; 9]]