Module Util


module Util: sig  end
Contains miscellaneous functions which have general usefulness. They could theoretically be contained in various other modules which are closed off to expansion - like List or Str, for instance.

val swap : 'a * 'b -> 'b * 'a
swap (a,b) returns (b,a).

type 'a multi = {
   melt : 'a;
   count : int;
}
Defines multi-elements, which are elements associated with an integer count. The count of a multi-element e can be accessed with e.count, while the value can be accessed with e.melt.

val multi : int -> 'a -> 'a multi
Handily create a multi-element.

type 'a weighed = {
   welt : 'a;
   weigh : int;
}
This is pretty much the same as a multi-element, but with a different name.

val weigh : int -> 'a -> 'a weighed
val compare_by_weight : 'a weighed -> 'b weighed -> int
Calls the standard compare function between the weights of two weighted elements.
val apply_weight_and_sort : ('a -> int) -> 'a list -> 'a weighed list
Given a weight function f : 'a -> int and a list l of elements of type 'a, this function creates the corresponding list of weighted elements, obtained by applying the function, and sorts it.
val allpairs : 'a list -> 'b list -> ('a * 'b) list
allpairs l1 l2 calculates the list of all pairs (e1, e2) of elements where e1 is from l1 and e2 is from l2. Not tail recursive for now.
val list_repeat : int -> 'a -> 'a list
list_repeat n x creates a list of length n where all the elements are equal to x.
val pairmap : ('a * 'b -> 'c) -> 'a list -> 'b list -> 'c list
pairmap f l1 l2 calculates the result of applying f to all the pairs (e1, e2) where e1 is from l1 and e2 is from l2.
val choosepairs : 'a list -> ('a * 'a) list
choosepairs l returns a list of all pairs (e1, e2) of elements of l, where e1 is not equal to e2 and all the pairs are distinct regardless of order.
val choosepairmap : ('a * 'a -> 'b) -> 'a list -> 'b list
choosepairmap f l applies f to all pairs (e1, e2) of elements of l as returned by choosepairs l above.
val linter : ('a -> 'b -> bool) -> 'a list -> 'b list -> ('a * 'b) list
linter f l1 l2 calculates the list of pairs (e1, e2) which are such that f e1 e2 is true. Should change this for a tail recursive functional implementation.
val lmultinter : ('a -> 'b -> 'c list) -> 'a list -> 'b list -> (('a * 'b) * 'c) list
A slightly more sophisticated version of linter. lmultinter f l1 l2 should be passed a function f which is such that f e1 e2 is itself a list. The the result will be the concatenation of all the images of f over all pairs of elements from l1 and l2, preserving the source and the value of the application.
exception Skip
This exception is assumed to be thrown by the first parameter of pilemap below.
val pilemap : ('a -> 'b) -> 'a list -> 'b list
pilemap f l is very much like List.map f l except that f may throw the exception Skip, in which case the result of that application is not included in the result.
val all_matches : Str.regexp -> string -> int -> (int * int) list
all_matches r s 0 returns a list of pairs (len, pos) where len is the length of a match of the regular expression r in the string s, and pos is the character position at which it occurs. If there's no match, the list is empty. If there is any match, the list will not be empty. No match will overlay. The regular expression r should be of type Str.regex.
val array_filteri : ('a -> bool) -> 'a array -> int array
array_filteri f a returns the array of indices of elements of a which satisfy the boolean filter f.
val array_fold_lefti : ('a -> int -> 'b -> 'a) -> 'a -> 'b array -> 'a
array_fold_lefti f cumul a computes f (... (f (f cumul 0 a.(0)) 1 a.(1)) ...) (n-1) a.(n-1), where n is the length of the array a.
val array_add : 'a array -> 'a -> 'a array
array_add a e appends the element e at the end of the array a and returns the resulting array.
val removeq : 'a -> 'a list -> 'a list
From List2 in CDK
val remove : 'a -> 'a list -> 'a list
From List2 in CDK
val removeq_first : 'a -> 'a list -> 'a list
From List2 in CDK
val remove_first : 'a -> 'a list -> 'a list
From List2 in CDK
val sym_assoc : 'a -> ('a * 'a) list -> 'a
sym_assoc e l searches the list l for a pair where either the first or the second element matches e, and returns the other member of the pair.
val sym_assoc_elem : 'a -> ('a * 'a) list -> ('a * 'a) * 'a
sym_assoc_elem e l searches the list l for a pair where either the first or the second element matches e, and returns a pair consisting of the matched pair with the other member of the pair.
val fst3 : 'a * 'b * 'c -> 'a
fst3 a b c = a

snd3 a b c = b

val snd3 : 'a * 'b * 'c -> 'b
trd3 a b c = c
val trd3 : 'a * 'b * 'c -> 'c