2 (** This module provides extended functions for some datatypes. *)
7 (** [split_nth n l] returns the list of elements of [l] before the [n]th
8 (exclusive, starting at 0) and those after. Raises [Failure
9 "Misc.List.split_nth"] if [n] is negative. *)
12 if n >= 0 then raise (Failure "Misc.List.split_nth")
14 let rec aux i acc = function
15 | [] -> (List.rev acc, [])
16 | l when i = n -> (List.rev acc, l)
17 | e :: l -> aux (i+1) (e :: acc) l in
20 let to_string sep f l =
21 let rec aux = function
24 | e :: l -> (f e) ^ sep ^ (aux l) in
27 let rec fold_left3 f res l1 l2 l3 = match l1, l2, l3 with
29 | e1 :: l1, e2 :: l2, e3 :: l3 -> fold_left3 f (f res e1 e2 e3) l1 l2 l3
30 | _ -> raise (Invalid_argument "Misc.List.fold_left3")
33 let rec aux i acc = function
35 | e :: l -> aux (i+1) (f i e acc) l in
39 let f i e' res = if e = e' then Some i else res in
42 let sub_unordered l1 l2 = List.for_all (fun e -> List.mem e l2) l1
44 let eq_unordered l1 l2 = (sub_unordered l1 l2) && (sub_unordered l2 l1)
46 let compare cmp_a l1 l2 =
47 let f res e1 e2 = match res with
48 | None when cmp_a e1 e2 = 0 -> None
49 | None -> Some (cmp_a e1 e2)
51 let size1 = List.length l1 in
52 let size2 = List.length l2 in
53 if size1 < size2 then -1
55 if size2 < size1 then 1
57 match List.fold_left2 f None l1 l2 with
63 module Option = struct
65 let extract = function
67 | None -> raise (Invalid_argument "Misc.Option.extract")
69 let return (i : 'a) : 'a option = Some i
71 let (>>=) a f = match a with
75 let (>>) a f = match a with
77 | Some a -> Some (f a)
82 module OrdInt = struct type t = int let compare = Pervasives.compare end
83 module Set = Eset.Make (OrdInt)
84 module Map = Emap.Make (OrdInt)
85 module CMap = CompleteMap.Make (struct include Map let keys = None end)
88 module String = struct
89 module Set = Eset.Make (String)
90 module Map = Emap.Make (String)
91 module CMap = CompleteMap.Make (struct include Map let keys = None end)
92 module MSet = Multiset.Make (String)
95 let compare_couple cmp_a cmp_b (a, b) (a', b') =
96 let res_a = cmp_a a a' in
97 if res_a = 0 then cmp_b b b'
100 let string_of_bool = function
106 let rec repeat n f a =
108 else repeat (n-1) f (f a)