Skip to content

Commit

Permalink
refactor(array/make): use fillAllInPlace instead of manual loop
Browse files Browse the repository at this point in the history
  • Loading branch information
glennsl committed Feb 18, 2023
1 parent 85372d5 commit b4d2003
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/Core__Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ function make(len, x) {
return [];
}
var arr = new Array(len);
for(var i = 0; i < len; ++i){
arr[i] = x;
}
arr.fill(x);
return arr;
}

Expand Down
16 changes: 7 additions & 9 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'a>
@val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from"
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'c) => array<'a> = "Array.from"

@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill"

@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"

@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"

let make = (len, x) =>
if len <= 0 {
[]
} else {
let arr = makeUninitializedUnsafe(len)
for i in 0 to len - 1 {
arr->setUnsafe(i, x)
}
arr->fillAllInPlace(x)
arr
}

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

@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill"

@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"

@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"

@send external pop: array<'a> => option<'a> = "pop"

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

0 comments on commit b4d2003

Please sign in to comment.