From: Eric C. C. <ec...@cm...> - 2003-02-26 16:39:02
|
Here are the functions (primarily extensions to List and String) that I find myself re-using. The set-theoretic and combinatoric ones are probably too simplistic for general use, but I'll throw them out there for discussion. -- Eric C. Cooper e c c @ c m u . e d u ---- (* * Additions to the List module. *) (* Generate the range of integers [a; a+1; ...; b] *) val range : int * int -> int list (* List equivalent of Array.init *) val listi : int -> (int -> 'a) -> 'a list (* List equivalent of Array.mapi *) val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list (* List equivalent of Array.iteri *) val iteri : (int -> 'a -> unit) -> 'a list -> unit (* Reverse assoc *) (* can also add rassq, mem_rassoc/rassq, remove_rassoc/rassq if needed *) val rassoc : 'b -> ('a * 'b) list -> 'a (* Tail-recursively compute rev (map f list1) @ list2 *) val rev_map_append : ('a -> 'b) -> 'a list -> 'b list -> 'b list (* Split [x1; x2; ...; xN] into [x1; ...; x{n}] and [x{n+1}; ...; xN] *) val split_nth : int -> 'a list -> 'a list * 'a list (* Tail-recursively split [x1; x2; ...; xN] into [x{n}; ...; x1] and [x{n+1}; ...; xN] *) val rev_split_nth : int -> 'a list -> 'a list * 'a list (* Return the last element of a list. *) val last : 'a list -> 'a (* Insert x at position n in list. Position 0 places x at the front; position (length list) places x at the end. *) val insert : 'a -> int -> 'a list -> 'a list (* Remove first occurrence of x, if any, from list. *) val remove_first : 'a -> 'a list -> 'a list (* Remove all occurrences of x from list. *) val remove : 'a -> 'a list -> 'a list (* Remove a subset of elements from a list. *) val remove_subset : 'a list -> 'a list -> 'a list (* Rotate each element of a list to the left, so that the head becomes the last element. *) val rotate_left : 'a list -> 'a list (* Homogeneous version of List.fold_left. Uses the head of the list as the initial value. *) val fold : ('a -> 'a -> 'a) -> 'a list -> 'a (* Test whether the first list is a subset of the second. *) val subset : 'a list -> 'a list -> bool (* Return cartesian product of two lists. *) val product : 'a list -> 'b list -> ('a * 'b) list (* Return all sublists of a list. *) val subsets : 'a list -> 'a list list (* Return all n-element sublists of a list. *) val choose : int -> 'a list -> 'a list list (* Return all sublists with up to n elements. *) val choose_up_to : int -> 'a list -> 'a list list (* Remove duplicates from a sorted list, using a sort-style comparison function that returns 0 for equality. *) val uniq : ('a -> 'a -> int) -> 'a list -> 'a list (* * Additions to the String module. *) (* String equivalents of Array.fold_left and Array.fold_right *) val string_fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a val string_fold_right : (char -> 'a -> 'a) -> 'a -> string -> 'a (* Convert between strings and lists of chars. *) val explode : string -> char list val implode : char list -> string (* Convert between strings and lists of ints in the range 0 .. 255 *) val explode_bytes : string -> int list val implode_bytes : int list -> string (* Like [index_from] but searches at most [len] chars. *) (* can also add bounded_rindex if needed *) val bounded_index : string -> pos:int -> len:int -> char -> int (* * Miscellaneous control structures. *) (* Compute f (g x) *) (* It would be nice to have a standard infix operator for this. *) val compose : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c (* Compute f^n x *) val iterate : ('a -> 'a) -> int -> 'a -> 'a (* Invoke (f ()) n times, for side effects. *) val repeat : int -> (unit -> unit) -> unit (* Apply f to 0 .. n-1, for side effects. *) val dotimes : int -> (int -> unit) -> unit |