Skip to content

Commit 17fa657

Browse files
authored
Merge pull request #1893 from glennsl/vector-append
Add Js.Vector.append
2 parents f441957 + 7e4c946 commit 17fa657

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

jscomp/others/js_vector.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,7 @@ let mapi f a =
136136
r
137137
end
138138

139+
let append x a =
140+
Js.Array.concat [|x|] a
141+
139142
(* TODO: add [append] *)

jscomp/others/js_vector.mli

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,8 @@ val init : int -> (int -> 'a [@bs]) -> 'a t
8585
@param fn callback
8686
@raise RangeError when [n] is negative *)
8787

88-
(*val append : 'a t -> 'a t -> 'a t *)
89-
(** create a new array, there is no shallow-sharing
90-
between the output and input
91-
*)
88+
val append : 'a -> 'a t -> 'a t
89+
(** [append x a] returns a fresh array with x appended to a *)
9290

9391
external unsafe_get : 'a t -> int -> 'a = "%array_unsafe_get"
9492
external unsafe_set : 'a t -> int -> 'a -> unit = "%array_unsafe_set"
95-
96-

lib/js/js_vector.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ function mapi(f, a) {
143143
}
144144
}
145145

146+
function append(x, a) {
147+
return a.concat(/* array */[x]);
148+
}
149+
146150
exports.filterInPlace = filterInPlace;
147151
exports.empty = empty;
148152
exports.pushBack = pushBack;
@@ -157,4 +161,5 @@ exports.mapi = mapi;
157161
exports.foldLeft = foldLeft;
158162
exports.foldRight = foldRight;
159163
exports.init = init;
164+
exports.append = append;
160165
/* No side effect */

0 commit comments

Comments
 (0)