mytools.ml 4.46 KB
Newer Older
charguer's avatar
init  
charguer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17


(**************************************************************)
(** Option manipulation functions *)

let option_map f = function
  | None -> None
  | Some x -> Some (f x)

let option_iter f = function
  | None -> ()
  | Some x -> f x

let unsome = function
  | None -> assert false
  | Some v -> v

charguer's avatar
charguer committed
18
let list_of_option = function
charguer's avatar
init  
charguer committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  | None -> []
  | Some v -> [v]

let option_app d f = function
  | None -> d
  | Some x -> f x

let unsome_safe d = function
  | None -> d
  | Some s -> s

let bool_of_option xo =
   match xo with 
   | None -> false 
   | Some x -> x 


(**************************************************************)
(** List manipulation functions *)

let rec list_make n v =
   if n = 0 then [] else v::(list_make (n-1) v)
  
let list_mapi f l =
  let rec aux i = function
    | [] -> []
    | h::t -> (f i h)::(aux (i+1) t)
    in
  aux 0 l

let list_nat n = (* for n >= 0 *)
  let rec aux i = 
    if i = 0 then [] else (i-1)::(aux (i-1)) in
  List.rev (aux n)
    
let rec list_separ sep = function 
  | [] -> []
  | a::[] -> a::[]
  | a::l -> a::sep::(list_separ sep l)

let rec filter_somes = function
  | [] -> []
  | None::l -> filter_somes l
  | (Some x) :: l -> x :: filter_somes l

let list_unique l =
   let rec aux acc = function
      | [] -> acc
      | a::q -> 
         if List.mem a acc then aux acc q else aux (a::acc) q
      in
   aux [] l

let list_intersect l1 l2 =
   List.filter (fun x -> List.mem x l1) l2

let list_minus l1 l2 =
   List.filter (fun t -> not (List.mem t l2)) l1

let list_concat_map f l =
   List.concat (List.map f l)

let list_assoc_option x l =
   try Some (List.assoc x l)
   with Not_found -> None

let rec assoc_list_map f = function
  | [] -> []
  | (k,v)::l -> (k, f v)::(assoc_list_map f l)

let rec list_remove i l = (* i >= 0 *)
   match l with 
   | [] -> failwith "list_remove invalid index" (* todo: illegal argument *)
   | x::t -> if i = 0 then t else x::(list_remove (i-1) t)

let rec list_replace i v l = (* i >= 0 *)
   match l with 
   | [] -> failwith "list_replace invalid index" (* todo: illegal argument *)
   | x::t -> if i = 0 then v::t else x::(list_replace (i-1) v t)

let list_replace_nth i vs xs =
   list_replace i (List.nth vs i) xs 

let list_ksort cmp l =
  List.sort (fun (k1,_) (k2,_) -> cmp k1 k2) l

let list_index k l =
   let rec aux n = function
      | [] -> raise Not_found
      | x::l -> if x = k then n else aux (n+1) l
      in
   aux 0 l 

(**************************************************************)
(** String manipulation functions *)

let str_cmp (s1:string) (s2:string) =
  if s1 < s2 then -1 else if s1 = s2 then 0 else 1

let str_starts_with p s = 
   let n = String.length p in
      String.length s >= n 
   && String.sub s 0 n = p 

let str_replace char1 char2 s =
   let s2 = String.copy s in
   for i = 0 to pred (String.length s) do
      if s2.[i] = char1 then s2.[i] <- char2;
   done;
   s2

charguer's avatar
charguer committed
130
let str_capitalize_1 s =
charguer's avatar
init  
charguer committed
131 132 133 134 135
   if String.length s <= 0 then s else
   let s' = String.copy s in
   s'.[0] <- Char.uppercase s.[0];
   s'

charguer's avatar
charguer committed
136
let str_capitalize_2 s =
charguer's avatar
init  
charguer committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
   if String.length s < 2 then s else
   let s' = String.copy s in
   s'.[1] <- Char.uppercase s.[1];
   s'


(**************************************************************)
(** File manipulation functions *)

let file_put_contents filename text =
   try 
      let handle = open_out filename in
      output_string handle text;
      close_out handle
   with Sys_error s -> 
     failwith ("Could not write in file: " ^ filename ^ "\n" ^ s)


(**************************************************************)
(** Try-with manipulation functions *)

let gives_not_found f =
   try ignore (f()); false 
   with Not_found -> true


(**************************************************************)
(** Pretty-printing functions *)

let lin0 = ""
let lin1 = "\n"
let lin2 = "\n\n"

let show_list s sep l = 
  String.concat sep (List.map s l)

let show_listp s sep l = 
  if l = [] then "" else
  sep ^ (String.concat sep (List.map s l))

let show_listq s sep l = 
  if l = [] then "" else
  (String.concat sep (List.map s l)) ^ sep

let show_option f ox =
   match ox with
   | None -> ""
   | Some x -> f x

let show_par required s =
  if required then "(" ^ s ^ ")" else s

charguer's avatar
charguer committed
189 190 191
let show_str s =
  s

charguer's avatar
init  
charguer committed
192 193 194 195 196 197 198 199 200 201 202 203

(**************************************************************)
(** Error messages *)

let output s = 
  Printf.printf "%s\n" s

let warning s =
  Printf.printf "### WARNING: %s\n" s

let unsupported s =
   failwith ("Unsupported language construction : " ^ s)
charguer's avatar
charguer committed
204
  (* TODO: report location *)