diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index 87a439d7..ddd13c4c 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -52,6 +52,16 @@ let get_safe a i = None (get_safe [|1;2;3|] ~-42) *) +let map_inplace f a = + Array.iteri (fun i e -> Array.unsafe_set a i (f e)) a + +(*$Q + Q.(array int) (fun a -> \ + let b = map ((+) 1) a in \ + map_inplace ((+) 1) a; \ + b = a) +*) + let fold = Array.fold_left let foldi f acc a = diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index 34f95264..d6b496c6 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -58,6 +58,9 @@ val get_safe : 'a t -> int -> 'a option (** [get_safe a i] returns [Some a.(i)] if [i] is a valid index. @since 0.18 *) +val map_inplace : ('a -> 'a) -> 'a t -> unit +(** [map_inplace f a] replace all elements of [a] by its image by [f]. *) + val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a (** [fold f init a] computes [f (… (f (f init a.(0)) a.(1)) …) a.(n-1)], where [n] is the length of the array [a]. diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index 8be4f679..b9c5b1b6 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -32,17 +32,17 @@ type 'a t = 'a array [@@@else_] include module type of ArrayLabels -(** {{: http://caml.inria.fr/pub/docs/manual-ocaml/libref/ArrayLabels.html} Documentation for the standard ArrayLabels module}*) - -module Floatarray = CCArray.Floatarray -type 'a t = 'a array (** @inline {{: https://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html} Documentation for the standard Array module}*) +module Floatarray = CCArray.Floatarray + +type 'a t = 'a array + [@@@endif] val empty : 'a t -(** [empty] is the empty array, physically equal to [||]. *) +(** [empty] is the empty array, physically equal to [[||]]. *) val equal : 'a equal -> 'a t equal (** [equal eq a1 a2] is [true] if the lengths of [a1] and [a2] are the same @@ -59,6 +59,9 @@ val get_safe : 'a t -> int -> 'a option (** [get_safe a i] returns [Some a.(i)] if [i] is a valid index. @since 0.18 *) +val map_inplace : f:('a -> 'a) -> 'a t -> unit +(** [map_inplace ~f a] replace all elements of [a] by its image by [f]. *) + val fold : f:('a -> 'b -> 'a) -> init:'a -> 'b t -> 'a (** [fold ~f ~init a] computes [f (… (f (f init a.(0)) a.(1)) …) a.(n-1)], where [n] is the length of the array [a].