From df64b07140949a325668f2afe0da0a58f55898e4 Mon Sep 17 00:00:00 2001 From: chrismamo1 Date: Sun, 9 Aug 2015 18:12:21 -0500 Subject: [PATCH] added an Array.iter2 function to the standard library in order to bring the Array library up to speed with the List library --- stdlib/array.ml | 5 +++++ stdlib/array.mli | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/stdlib/array.ml b/stdlib/array.ml index 13d0bfdcd..cc419893a 100644 --- a/stdlib/array.ml +++ b/stdlib/array.ml @@ -77,6 +77,11 @@ let blit a1 ofs1 a2 ofs2 len = let iter f a = for i = 0 to length a - 1 do f(unsafe_get a i) done +let iter f a b = + if length a <> length b then raise (Invalid_argument "arrays must have the same length") + else + for i = 0 to length a - 1 do f (unsafe_get a i) (unsafe_get b i) done + let map f a = let l = length a in if l = 0 then [||] else begin diff --git a/stdlib/array.mli b/stdlib/array.mli index 0398509d0..ddd27395c 100644 --- a/stdlib/array.mli +++ b/stdlib/array.mli @@ -127,13 +127,18 @@ val iter : ('a -> unit) -> 'a array -> unit the elements of [a]. It is equivalent to [f a.(0); f a.(1); ...; f a.(Array.length a - 1); ()]. *) +val iter2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> unit +(** [Array.iter2 f a b] applies function [f] to all the elements of [a] + and [b]. + Raise [Invalid_argument] if the arrays are not the same size. *) + val map : ('a -> 'b) -> 'a array -> 'b array (** [Array.map f a] applies function [f] to all the elements of [a], and builds an array with the results returned by [f]: [[| f a.(0); f a.(1); ...; f a.(Array.length a - 1) |]]. *) val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array -(** [Array.map f a b] applies function [f] to all the elements of [a] +(** [Array.map2 f a b] applies function [f] to all the elements of [a] and [b], and builds an array with the results returned by [f]: [[| f a.(0) b.(0); f a.(1) b.(1); ...; f a.(Array.length a - 1) b.(Array.length b - 1)|]]. Raise [Invalid_argument] if the arrays are not the same size. *)