feat(ccarray): add CCArray.map_inplace

This commit is contained in:
Fardale 2022-05-12 11:03:38 +02:00
parent e59cc68c24
commit 8a71b1dcaa
3 changed files with 21 additions and 5 deletions

View file

@ -52,6 +52,16 @@ let get_safe a i =
None (get_safe [|1;2;3|] ~-42) 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 fold = Array.fold_left
let foldi f acc a = let foldi f acc a =

View file

@ -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. (** [get_safe a i] returns [Some a.(i)] if [i] is a valid index.
@since 0.18 *) @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 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)], (** [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]. where [n] is the length of the array [a].

View file

@ -32,17 +32,17 @@ type 'a t = 'a array
[@@@else_] [@@@else_]
include module type of ArrayLabels 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 (** @inline
{{: https://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html} Documentation for the standard Array module}*) {{: 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] [@@@endif]
val empty : 'a t 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 val equal : 'a equal -> 'a t equal
(** [equal eq a1 a2] is [true] if the lengths of [a1] and [a2] are the same (** [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. (** [get_safe a i] returns [Some a.(i)] if [i] is a valid index.
@since 0.18 *) @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 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)], (** [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]. where [n] is the length of the array [a].