added an Array.iter2 function to the standard library in order to bring the Array library up to speed with the List library

master
chrismamo1 2015-08-09 18:12:21 -05:00 committed by Damien Doligez
parent ca5bac12a4
commit df64b07140
2 changed files with 11 additions and 1 deletions

View File

@ -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

View File

@ -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. *)