Skip to content

Commit b4d2003

Browse files
committed
refactor(array/make): use fillAllInPlace instead of manual loop
1 parent 85372d5 commit b4d2003

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/Core__Array.mjs

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ function make(len, x) {
99
return [];
1010
}
1111
var arr = new Array(len);
12-
for(var i = 0; i < len; ++i){
13-
arr[i] = x;
14-
}
12+
arr.fill(x);
1513
return arr;
1614
}
1715

src/Core__Array.res

+7-9
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'a>
1313
@val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from"
1414
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'c) => array<'a> = "Array.from"
1515

16+
@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill"
17+
18+
@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
19+
20+
@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
21+
1622
let make = (len, x) =>
1723
if len <= 0 {
1824
[]
1925
} else {
2026
let arr = makeUninitializedUnsafe(len)
21-
for i in 0 to len - 1 {
22-
arr->setUnsafe(i, x)
23-
}
27+
arr->fillAllInPlace(x)
2428
arr
2529
}
2630

@@ -47,12 +51,6 @@ external copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a> =
4751
@send
4852
external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<'a> = "copyWithin"
4953

50-
@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill"
51-
52-
@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
53-
54-
@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
55-
5654
@send external pop: array<'a> => option<'a> = "pop"
5755

5856
@send external push: (array<'a>, 'a) => unit = "push"

0 commit comments

Comments
 (0)