Skip to content

Remove Belt & Pervasives deps #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Next version

- Remove some deps to Belt, Pervasives and Js. https://github.com/rescript-association/rescript-core/pull/226/commits

## 1.3.0

This version requires ReScript `>=11.1.0-rc.6`.
Expand Down
7 changes: 5 additions & 2 deletions src/Core__Array.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

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 @@ -113,10 +112,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,7 +1,6 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Core__Array from "./Core__Array.mjs";
import * as PervasivesU from "rescript/lib/es6/pervasivesU.js";

function equal(a, b) {
return a === b;
Expand All @@ -26,6 +25,14 @@ function fromString(x, radix) {
}
}

function abs(x) {
if (x >= 0) {
return x;
} else {
return -x | 0;
}
}

function range(start, end, optionsOpt) {
var options = optionsOpt !== undefined ? optionsOpt : ({});
var isInverted = start > end;
Expand All @@ -51,7 +58,7 @@ function range(start, end, optionsOpt) {
} else {
var range$1 = isInverted ? start - end | 0 : end - start | 0;
var range$2 = options.inclusive === true ? range$1 + 1 | 0 : range$1;
length = Math.ceil(range$2 / PervasivesU.abs(step)) | 0;
length = Math.ceil(range$2 / abs(step)) | 0;
}
return Core__Array.fromInitializer(length, (function (i) {
return start + Math.imul(i, step) | 0;
Expand Down
7 changes: 7 additions & 0 deletions src/Core__Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ external mod: (int, int) => int = "%modint"

type rangeOptions = {step?: int, inclusive?: bool}

let abs = x =>
if x >= 0 {
x
} else {
-x
}

let range = (start, end, ~options: rangeOptions={}) => {
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,6 +1,5 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

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 @@ -794,7 +793,12 @@ function reduceReverse(l, acc, f) {
if (len < 1000) {
return reduceReverseUnsafe(l, acc, f);
} else {
return Belt_Array.reduceReverseU(toArray(l), acc, f);
var a = toArray(l);
var r = acc;
for(var i = a.length - 1 | 0; i >= 0; --i){
r = f(r, a[i]);
}
return r;
}
}

Expand Down Expand Up @@ -888,7 +892,14 @@ function reduceReverse2(l1, l2, acc, f) {
if (len < 1000) {
return reduceReverse2Unsafe(l1, l2, acc, f);
} else {
return Belt_Array.reduceReverse2U(toArray(l1), toArray(l2), acc, f);
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(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
Loading