Skip to content

Commit 45e1e78

Browse files
committed
Remove dependency on js_math.
1 parent 6022c0a commit 45e1e78

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

jscomp/others/belt_Array.res

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,16 @@ let swapUnsafe = (xs, i, j) => {
6464
setUnsafe(xs, j, tmp)
6565
}
6666

67+
68+
@val @scope("Math") external random : unit => float = "random"
69+
@val @scope("Math") external floor : float => int = "floor"
70+
external toFloat: int => float = "%floatofint"
71+
6772
let shuffleInPlace = xs => {
6873
let len = length(xs)
74+
let random_int = (min, max) => floor(random() *. toFloat(max - min)) + min
6975
for i in 0 to len - 1 {
70-
swapUnsafe(xs, i, Js_math.random_int(i, len)) /* [i,len) */
76+
swapUnsafe(xs, i, random_int(i, len)) /* [i,len) */
7177
}
7278
}
7379

lib/es6/belt_Array.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import * as Caml from "./caml.js";
44
import * as Curry from "./curry.js";
5-
import * as Js_math from "./js_math.js";
65
import * as Caml_option from "./caml_option.js";
76

87
function get(arr, i) {
@@ -61,8 +60,11 @@ function swapUnsafe(xs, i, j) {
6160

6261
function shuffleInPlace(xs) {
6362
let len = xs.length;
63+
let random_int = function (min, max) {
64+
return Math.floor(Math.random() * (max - min | 0)) + min | 0;
65+
};
6466
for(let i = 0; i < len; ++i){
65-
swapUnsafe(xs, i, Js_math.random_int(i, len));
67+
swapUnsafe(xs, i, random_int(i, len));
6668
}
6769
}
6870

lib/js/belt_Array.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
let Caml = require("./caml.js");
44
let Curry = require("./curry.js");
5-
let Js_math = require("./js_math.js");
65
let Caml_option = require("./caml_option.js");
76

87
function get(arr, i) {
@@ -61,8 +60,11 @@ function swapUnsafe(xs, i, j) {
6160

6261
function shuffleInPlace(xs) {
6362
let len = xs.length;
63+
let random_int = function (min, max) {
64+
return Math.floor(Math.random() * (max - min | 0)) + min | 0;
65+
};
6466
for(let i = 0; i < len; ++i){
65-
swapUnsafe(xs, i, Js_math.random_int(i, len));
67+
swapUnsafe(xs, i, random_int(i, len));
6668
}
6769
}
6870

0 commit comments

Comments
 (0)