Skip to content

Commit bc8029f

Browse files
fhammerschmidtillusionalsagacity
authored andcommitted
Remove Belt & Pervasives deps (rescript-lang#226)
* Inline Belt.Array deps of Core__List * Get rid of Pervasives deps in Core__Int * Get rid of Js dep in Core__Array * Changelog
1 parent 955b230 commit bc8029f

7 files changed

+67
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- BREAKING: Fixes the type of `RegExp.Result.t` to be `array<option<string>>` instead of `array<string>`. https://github.com/rescript-association/rescript-core/pull/233.
66
- BREAKING: Adds typed bindings to `Intl`, replacing the options type of `{..}` with records. https://github.com/rescript-association/rescript-core/pull/65
77
- BREAKING: Align List api with other modules (`List.getBy` -> `List.find` etc.). https://github.com/rescript-association/rescript-core/pull/195
8+
- Remove some deps to Belt, Pervasives and Js. https://github.com/rescript-association/rescript-core/pull/226/commits
89
- Fix: Expose Intl.Common. https://github.com/rescript-association/rescript-core/pull/197
910
- Add `Array.join` and deprecate `Array.joinWith`. https://github.com/rescript-association/rescript-core/pull/205
1011
- Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181

src/Core__Array.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as Curry from "rescript/lib/es6/curry.js";
4-
import * as Js_math from "rescript/lib/es6/js_math.js";
54
import * as Caml_option from "rescript/lib/es6/caml_option.js";
65

76
function make(length, x) {
@@ -114,10 +113,14 @@ function swapUnsafe(xs, i, j) {
114113
xs[j] = tmp;
115114
}
116115

116+
function random_int(min, max) {
117+
return (Math.floor(Math.random() * (max - min | 0)) | 0) + min | 0;
118+
}
119+
117120
function shuffle(xs) {
118121
var len = xs.length;
119122
for(var i = 0; i < len; ++i){
120-
swapUnsafe(xs, i, Js_math.random_int(i, len));
123+
swapUnsafe(xs, i, random_int(i, len));
121124
}
122125
}
123126

src/Core__Array.res

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,20 @@ let swapUnsafe = (xs, i, j) => {
203203
setUnsafe(xs, j, tmp)
204204
}
205205

206+
module M = {
207+
@val external floor: float => float = "Math.floor"
208+
@val external random: unit => float = "Math.random"
209+
external fromFloat: float => int = "%intoffloat"
210+
external toFloat: int => float = "%identity"
211+
212+
let random_int: (int, int) => int = (min, max) =>
213+
floor(random() *. toFloat(max - min))->fromFloat + min
214+
}
215+
206216
let shuffle = xs => {
207217
let len = length(xs)
208218
for i in 0 to len - 1 {
209-
swapUnsafe(xs, i, Js.Math.random_int(i, len)) /* [i,len) */
219+
swapUnsafe(xs, i, M.random_int(i, len)) /* [i,len) */
210220
}
211221
}
212222

src/Core__Int.mjs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3-
import * as Pervasives from "rescript/lib/es6/pervasives.js";
43
import * as Core__Array from "./Core__Array.mjs";
54

65
function equal(a, b) {
@@ -26,6 +25,14 @@ function fromString(radix, x) {
2625
}
2726
}
2827

28+
function abs(x) {
29+
if (x >= 0) {
30+
return x;
31+
} else {
32+
return -x | 0;
33+
}
34+
}
35+
2936
function rangeWithOptions(start, end, options) {
3037
var isInverted = start > end;
3138
var n = options.step;
@@ -50,7 +57,7 @@ function rangeWithOptions(start, end, options) {
5057
} else {
5158
var range = isInverted ? start - end | 0 : end - start | 0;
5259
var range$1 = options.inclusive === true ? range + 1 | 0 : range;
53-
length = Math.ceil(range$1 / Pervasives.abs(step)) | 0;
60+
length = Math.ceil(range$1 / abs(step)) | 0;
5461
}
5562
return Core__Array.fromInitializer(length, (function (i) {
5663
return start + Math.imul(i, step) | 0;

src/Core__Int.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ let fromString = (~radix=?, x) => {
4242
external mod: (int, int) => int = "%modint"
4343

4444
type rangeOptions = {step?: int, inclusive?: bool}
45+
46+
let abs = x =>
47+
if x >= 0 {
48+
x
49+
} else {
50+
-x
51+
}
52+
4553
let rangeWithOptions = (start, end, options) => {
4654
let isInverted = start > end
4755

src/Core__List.mjs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as Curry from "rescript/lib/es6/curry.js";
4-
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
54
import * as Caml_option from "rescript/lib/es6/caml_option.js";
65
import * as Core__Array from "./Core__Array.mjs";
76

@@ -817,7 +816,12 @@ function reduceReverse(l, accu, f) {
817816
if (len < 1000) {
818817
return reduceReverseUnsafeU(l, accu, f$1);
819818
} else {
820-
return Belt_Array.reduceReverseU(toArray(l), accu, f$1);
819+
var a = toArray(l);
820+
var r = accu;
821+
for(var i = a.length - 1 | 0; i >= 0; --i){
822+
r = f$1(r, a[i]);
823+
}
824+
return r;
821825
}
822826
}
823827

@@ -921,7 +925,14 @@ function reduceReverse2(l1, l2, acc, f) {
921925
if (len < 1000) {
922926
return reduceReverse2UnsafeU(l1, l2, acc, f$1);
923927
} else {
924-
return Belt_Array.reduceReverse2U(toArray(l1), toArray(l2), acc, f$1);
928+
var a = toArray(l1);
929+
var b = toArray(l2);
930+
var r = acc;
931+
var len$1 = a.length < b.length ? a.length : b.length;
932+
for(var i = len$1 - 1 | 0; i >= 0; --i){
933+
r = f$1(r, a[i], b[i]);
934+
}
935+
return r;
925936
}
926937
}
927938

src/Core__List.res

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,26 @@
6363

6464
type t<'a> = list<'a>
6565

66-
// TODO: This module should be inlined eventually, if we end up removing Belt
67-
// from the compiler.
6866
module A = {
69-
let makeUninitializedUnsafe = Belt_Array.makeUninitializedUnsafe
70-
let reduceReverseU = Belt_Array.reduceReverseU
71-
let reduceReverse2U = Belt_Array.reduceReverse2U
67+
@new external makeUninitializedUnsafe: int => array<'a> = "Array"
68+
external min: ('a, 'a) => 'a = "%bs_min"
69+
70+
let reduceReverseU = (a, x, f) => {
71+
let r = ref(x)
72+
for i in Core__Array.length(a) - 1 downto 0 {
73+
r.contents = f(. r.contents, Core__Array.getUnsafe(a, i))
74+
}
75+
r.contents
76+
}
77+
78+
let reduceReverse2U = (a, b, x, f) => {
79+
let r = ref(x)
80+
let len = min(Core__Array.length(a), Core__Array.length(b))
81+
for i in len - 1 downto 0 {
82+
r.contents = f(. r.contents, Core__Array.getUnsafe(a, i), Core__Array.getUnsafe(b, i))
83+
}
84+
r.contents
85+
}
7286
}
7387

7488
external mutableCell: ('a, t<'a>) => t<'a> = "#makemutablelist"

0 commit comments

Comments
 (0)