Skip to content

Commit 66356e6

Browse files
committed
Belt.Array.sliceToEnd
1 parent 66232a1 commit 66356e6

File tree

5 files changed

+157
-78
lines changed

5 files changed

+157
-78
lines changed

jscomp/others/belt_Array.ml

+9
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ let slice a ~offset ~len =
195195
done ;
196196
result
197197

198+
let sliceToEnd a offset =
199+
let lena = length a in
200+
let ofs = if offset < 0 then max (lena + offset) 0 else offset in
201+
let len = lena - ofs in
202+
let result = makeUninitializedUnsafe len in
203+
for i = 0 to len - 1 do
204+
setUnsafe result i (getUnsafe a (ofs + i))
205+
done;
206+
result
198207

199208
let fill a ~offset ~len v =
200209
if len > 0 then

jscomp/others/belt_Array.mli

+14
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,20 @@ val slice: 'a array -> offset:int -> len:int -> 'a array
257257
]}
258258
*)
259259

260+
val sliceToEnd: 'a array -> int -> 'a array
261+
(** [sliceToEnd xs offset] creates a new array with the elements of [xs] starting at [offset]
262+
263+
[offset] can be negative;and is evaluated as [length xs - offset]
264+
[sliceToEnd xs -1] means get the last element as a singleton array
265+
266+
[sliceToEnd xs 0] will return a copy of the array
267+
268+
@example {[
269+
sliceToEnd [|10;11;12;13;14;15;16|] 2 = [|12;13;14;15;16|];;
270+
sliceToEnd [|10;11;12;13;14;15;16|] (-4) = [|13;14;15;16|];;
271+
]}
272+
*)
273+
260274

261275
external copy : 'a array -> (_ [@bs.as 0]) -> 'a array = "slice" [@@bs.send]
262276
(** [copy a]

0 commit comments

Comments
 (0)