Skip to content

Commit

Permalink
Remove Belt & Pervasives deps (rescript-lang#226)
Browse files Browse the repository at this point in the history
* Inline Belt.Array deps of Core__List

* Get rid of Pervasives deps in Core__Int

* Get rid of Js dep in Core__Array

* Changelog
  • Loading branch information
fhammerschmidt authored and illusionalsagacity committed Jun 19, 2024
1 parent 955b230 commit bc8029f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- 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.
- 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
Expand Down
7 changes: 5 additions & 2 deletions src/Core__Array.mjs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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));
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/Core__Int.mjs
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/Core__Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 14 additions & 3 deletions src/Core__List.mjs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down
24 changes: 19 additions & 5 deletions src/Core__List.res
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit bc8029f

Please sign in to comment.