add CCVector.mapi

This commit is contained in:
Fardale 2019-11-17 12:17:13 +01:00 committed by Simon Cruanes
parent 968a39b6bc
commit 3804dbff86
2 changed files with 25 additions and 0 deletions

View file

@ -497,6 +497,26 @@ let map f v =
to_list (map f v) = List.map f l)
*)
let mapi f v =
if array_is_empty_ v
then create ()
else (
let vec = Array.init v.size (fun i -> f i (Array.unsafe_get v.vec i)) in
{ size=v.size; vec; }
)
(*$T mapi
let v = create() in push v 1; push v 2; push v 3; \
to_list (mapi (fun i e -> Printf.sprintf "%i %i" i e) v) = ["0 1"; "1 2"; "2 3"]
*)
(*$QR mapi
Q.(pair (fun2 Observable.int Observable.int small_int) (small_list small_int))
(fun (Q.Fun (_,f),l) ->
let v = of_list l in
to_list (mapi f v) = List.mapi f l)
*)
let map_in_place f v =
iteri
(fun i x -> Array.unsafe_set v.vec i (f x))

View file

@ -141,6 +141,11 @@ val iteri : (int -> 'a -> unit) -> ('a,_) t -> unit
val map : ('a -> 'b) -> ('a,_) t -> ('b, 'mut) t
(** Map elements of the vector, yielding a new vector. *)
val mapi : (int -> 'a -> 'b) -> ('a,_) t -> ('b, 'mut) t
(** [map f v] is just like {!map}, but it also passes in the index
of each element as the first argument to the function [f].
@since NEXT_RELEASE *)
val map_in_place : ('a -> 'a) -> ('a,_) t -> unit
(** Map elements of the vector in place
@since 2.3 *)