aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/util.ml27
-rw-r--r--lib/util.mli5
2 files changed, 31 insertions, 1 deletions
diff --git a/lib/util.ml b/lib/util.ml
index 567c8edb6..363e13b1a 100644
--- a/lib/util.ml
+++ b/lib/util.ml
@@ -119,6 +119,14 @@ let list_map_i f =
in
map_i_rec
+let list_map2_i f i l1 l2 =
+ let rec map_i i = function
+ | ([], []) -> []
+ | ((h1::t1), (h2::t2)) -> (f i h1 h2) :: (map_i (succ i) (t1,t2))
+ | (_, _) -> invalid_arg "map2_i"
+ in
+ map_i i (l1,l2)
+
let list_index x =
let rec index_x n = function
| y::l -> if x = y then n else index_x (succ n) l
@@ -233,6 +241,13 @@ let list_except_assoc e =
let list_join_map f l = List.flatten (List.map f l)
+let list_try_find f =
+ let rec try_find_f = function
+ | [] -> failwith "try_find"
+ | h::t -> try f h with Failure _ -> try_find_f t
+ in
+ try_find_f
+
(* Arrays *)
@@ -331,6 +346,18 @@ let array_map2 f v1 v2 =
res
end
+let array_map2_i f v1 v2 =
+ if Array.length v1 <> Array.length v2 then invalid_arg "array_map2";
+ if Array.length v1 == 0 then
+ [| |]
+ else begin
+ let res = Array.create (Array.length v1) (f 0 v1.(0) v2.(0)) in
+ for i = 1 to pred (Array.length v1) do
+ res.(i) <- f i v1.(i) v2.(i)
+ done;
+ res
+ end
+
let array_map3 f v1 v2 v3 =
if Array.length v1 <> Array.length v2 ||
Array.length v1 <> Array.length v3 then invalid_arg "array_map3";
diff --git a/lib/util.mli b/lib/util.mli
index e0743627d..77c87d33f 100644
--- a/lib/util.mli
+++ b/lib/util.mli
@@ -52,6 +52,8 @@ val list_tabulate : (int -> 'a) -> int -> 'a list
val list_assign : 'a list -> int -> 'a -> 'a list
val list_distinct : 'a list -> bool
val list_map_i : (int -> 'a -> 'b) -> int -> 'a list -> 'b list
+val list_map2_i :
+ (int -> 'a -> 'b -> 'c) -> int -> 'a list -> 'b list -> 'c list
val list_index : 'a -> 'a list -> int
val list_iter_i : (int -> 'a -> unit) -> 'a list -> unit
val list_fold_left_i : (int -> 'a -> 'b -> 'a) -> int -> 'a -> 'b list -> 'a
@@ -73,7 +75,7 @@ val list_map_append2 : ('a -> 'b -> 'c list) -> 'a list -> 'b list -> 'c list
val list_share_tails : 'a list -> 'a list -> 'a list * 'a list * 'a list
val list_except_assoc : 'a -> ('a * 'b) list -> ('a * 'b) list
val list_join_map : ('a -> 'b list) -> 'a list -> 'b list
-
+val list_try_find : ('a -> 'b) -> 'a list -> 'b
(*s Arrays. *)
@@ -95,6 +97,7 @@ val array_list_of_tl : 'a array -> 'a list
val array_map_to_list : ('a -> 'b) -> 'a array -> 'b list
val array_chop : int -> 'a array -> 'a array * 'a array
val array_map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array
+val array_map2_i : (int -> 'a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array
val array_map3 :
('a -> 'b -> 'c -> 'd) -> 'a array -> 'b array -> 'c array -> 'd array