mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add CCVector.mapi
This commit is contained in:
parent
968a39b6bc
commit
3804dbff86
2 changed files with 25 additions and 0 deletions
|
|
@ -497,6 +497,26 @@ let map f v =
|
||||||
to_list (map f v) = List.map f l)
|
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 =
|
let map_in_place f v =
|
||||||
iteri
|
iteri
|
||||||
(fun i x -> Array.unsafe_set v.vec i (f x))
|
(fun i x -> Array.unsafe_set v.vec i (f x))
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,11 @@ val iteri : (int -> 'a -> unit) -> ('a,_) t -> unit
|
||||||
val map : ('a -> 'b) -> ('a,_) t -> ('b, 'mut) t
|
val map : ('a -> 'b) -> ('a,_) t -> ('b, 'mut) t
|
||||||
(** Map elements of the vector, yielding a new vector. *)
|
(** 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
|
val map_in_place : ('a -> 'a) -> ('a,_) t -> unit
|
||||||
(** Map elements of the vector in place
|
(** Map elements of the vector in place
|
||||||
@since 2.3 *)
|
@since 2.3 *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue