aboutsummaryrefslogtreecommitdiff
path: root/tools.ml
diff options
context:
space:
mode:
Diffstat (limited to 'tools.ml')
-rwxr-xr-xtools.ml39
1 files changed, 30 insertions, 9 deletions
diff --git a/tools.ml b/tools.ml
index f4befa5..33185ec 100755
--- a/tools.ml
+++ b/tools.ml
@@ -5,6 +5,20 @@ module Option = struct
let map f = function
| Some x -> Some (f x)
| None -> None
+
+ let iter f = function
+ | Some x -> f x
+ | None -> ()
+
+ let bind f = function
+ | None -> None
+ | Some x -> f x
+
+ let default v = function
+ | None -> v
+ | Some x -> x
+
+
end
module String = struct
@@ -125,22 +139,29 @@ module List = struct
UTF8.Buffer.add_string buffer last
end
+ let rec find_map f = begin function
+ | [] -> raise Not_found
+ | hd::tl -> begin match f hd with
+ | Some x -> x
+ | None -> find_map f tl
+ end
+ end
+
let rec findOpt p = begin function
| [] -> None
| x::l ->
- if p x then
- Some(x)
- else
- findOpt p l
+ if p x then
+ Some(x)
+ else
+ findOpt p l
end
- and find2 p = begin function
+ and find_map2 p = begin function
| [] -> raise Not_found
| x::l ->
- begin match findOpt p x with
- | None -> find2 p l
- | Some x -> x
- end
+ begin try find_map p x with
+ Not_found -> find_map2 p l
+ end
end
(** Convert the list [l] as an array *)