From bc8029fc3c5c65c9ad3dd63e0239246a413eed5a Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Tue, 4 Jun 2024 10:02:47 +0200 Subject: [PATCH] Remove Belt & Pervasives deps (#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 --- CHANGELOG.md | 1 + src/Core__Array.mjs | 7 +++++-- src/Core__Array.res | 12 +++++++++++- src/Core__Int.mjs | 11 +++++++++-- src/Core__Int.res | 8 ++++++++ src/Core__List.mjs | 17 ++++++++++++++--- src/Core__List.res | 24 +++++++++++++++++++----- 7 files changed, 67 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e817712..411a841b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - BREAKING: Fixes the type of `RegExp.Result.t` to be `array>` instead of `array`. https://github.com/rescript-association/rescript-core/pull/233. - BREAKING: Adds typed bindings to `Intl`, replacing the options type of `{..}` with records. https://github.com/rescript-association/rescript-core/pull/65 - BREAKING: Align List api with other modules (`List.getBy` -> `List.find` etc.). https://github.com/rescript-association/rescript-core/pull/195 +- Remove some deps to Belt, Pervasives and Js. https://github.com/rescript-association/rescript-core/pull/226/commits - Fix: Expose Intl.Common. https://github.com/rescript-association/rescript-core/pull/197 - Add `Array.join` and deprecate `Array.joinWith`. https://github.com/rescript-association/rescript-core/pull/205 - Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181 diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index 489f569e..a99713d3 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -1,7 +1,6 @@ // Generated by ReScript, PLEASE EDIT WITH CARE import * as Curry from "rescript/lib/es6/curry.js"; -import * as Js_math from "rescript/lib/es6/js_math.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; function make(length, x) { @@ -114,10 +113,14 @@ function swapUnsafe(xs, i, j) { xs[j] = tmp; } +function random_int(min, max) { + return (Math.floor(Math.random() * (max - min | 0)) | 0) + min | 0; +} + function shuffle(xs) { var len = xs.length; for(var i = 0; i < len; ++i){ - swapUnsafe(xs, i, Js_math.random_int(i, len)); + swapUnsafe(xs, i, random_int(i, len)); } } diff --git a/src/Core__Array.res b/src/Core__Array.res index 03b69ea4..788352fa 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -203,10 +203,20 @@ let swapUnsafe = (xs, i, j) => { setUnsafe(xs, j, tmp) } +module M = { + @val external floor: float => float = "Math.floor" + @val external random: unit => float = "Math.random" + external fromFloat: float => int = "%intoffloat" + external toFloat: int => float = "%identity" + + let random_int: (int, int) => int = (min, max) => + floor(random() *. toFloat(max - min))->fromFloat + min +} + let shuffle = xs => { let len = length(xs) for i in 0 to len - 1 { - swapUnsafe(xs, i, Js.Math.random_int(i, len)) /* [i,len) */ + swapUnsafe(xs, i, M.random_int(i, len)) /* [i,len) */ } } diff --git a/src/Core__Int.mjs b/src/Core__Int.mjs index c870441b..2d453d65 100644 --- a/src/Core__Int.mjs +++ b/src/Core__Int.mjs @@ -1,6 +1,5 @@ // Generated by ReScript, PLEASE EDIT WITH CARE -import * as Pervasives from "rescript/lib/es6/pervasives.js"; import * as Core__Array from "./Core__Array.mjs"; function equal(a, b) { @@ -26,6 +25,14 @@ function fromString(radix, x) { } } +function abs(x) { + if (x >= 0) { + return x; + } else { + return -x | 0; + } +} + function rangeWithOptions(start, end, options) { var isInverted = start > end; var n = options.step; @@ -50,7 +57,7 @@ function rangeWithOptions(start, end, options) { } else { var range = isInverted ? start - end | 0 : end - start | 0; var range$1 = options.inclusive === true ? range + 1 | 0 : range; - length = Math.ceil(range$1 / Pervasives.abs(step)) | 0; + length = Math.ceil(range$1 / abs(step)) | 0; } return Core__Array.fromInitializer(length, (function (i) { return start + Math.imul(i, step) | 0; diff --git a/src/Core__Int.res b/src/Core__Int.res index bd107749..4d5e1695 100644 --- a/src/Core__Int.res +++ b/src/Core__Int.res @@ -42,6 +42,14 @@ let fromString = (~radix=?, x) => { external mod: (int, int) => int = "%modint" type rangeOptions = {step?: int, inclusive?: bool} + +let abs = x => + if x >= 0 { + x + } else { + -x + } + let rangeWithOptions = (start, end, options) => { let isInverted = start > end diff --git a/src/Core__List.mjs b/src/Core__List.mjs index 2968405a..5f5e1e0e 100644 --- a/src/Core__List.mjs +++ b/src/Core__List.mjs @@ -1,7 +1,6 @@ // Generated by ReScript, PLEASE EDIT WITH CARE import * as Curry from "rescript/lib/es6/curry.js"; -import * as Belt_Array from "rescript/lib/es6/belt_Array.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; import * as Core__Array from "./Core__Array.mjs"; @@ -817,7 +816,12 @@ function reduceReverse(l, accu, f) { if (len < 1000) { return reduceReverseUnsafeU(l, accu, f$1); } else { - return Belt_Array.reduceReverseU(toArray(l), accu, f$1); + var a = toArray(l); + var r = accu; + for(var i = a.length - 1 | 0; i >= 0; --i){ + r = f$1(r, a[i]); + } + return r; } } @@ -921,7 +925,14 @@ function reduceReverse2(l1, l2, acc, f) { if (len < 1000) { return reduceReverse2UnsafeU(l1, l2, acc, f$1); } else { - return Belt_Array.reduceReverse2U(toArray(l1), toArray(l2), acc, f$1); + var a = toArray(l1); + var b = toArray(l2); + var r = acc; + var len$1 = a.length < b.length ? a.length : b.length; + for(var i = len$1 - 1 | 0; i >= 0; --i){ + r = f$1(r, a[i], b[i]); + } + return r; } } diff --git a/src/Core__List.res b/src/Core__List.res index 94504d1c..9b1e3ebd 100644 --- a/src/Core__List.res +++ b/src/Core__List.res @@ -63,12 +63,26 @@ type t<'a> = list<'a> -// TODO: This module should be inlined eventually, if we end up removing Belt -// from the compiler. module A = { - let makeUninitializedUnsafe = Belt_Array.makeUninitializedUnsafe - let reduceReverseU = Belt_Array.reduceReverseU - let reduceReverse2U = Belt_Array.reduceReverse2U + @new external makeUninitializedUnsafe: int => array<'a> = "Array" + external min: ('a, 'a) => 'a = "%bs_min" + + let reduceReverseU = (a, x, f) => { + let r = ref(x) + for i in Core__Array.length(a) - 1 downto 0 { + r.contents = f(. r.contents, Core__Array.getUnsafe(a, i)) + } + r.contents + } + + let reduceReverse2U = (a, b, x, f) => { + let r = ref(x) + let len = min(Core__Array.length(a), Core__Array.length(b)) + for i in len - 1 downto 0 { + r.contents = f(. r.contents, Core__Array.getUnsafe(a, i), Core__Array.getUnsafe(b, i)) + } + r.contents + } } external mutableCell: ('a, t<'a>) => t<'a> = "#makemutablelist"