Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 60a007f

Browse files
authored
Sync Belt sources from compiler repo (#5)
1 parent dea1956 commit 60a007f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+767
-1098
lines changed

belt/src/belt_Array.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,11 @@ function swapUnsafe(xs, i, j) {
5858
xs[j] = tmp;
5959
}
6060

61-
function floor_int(f) {
62-
if (f > 2147483647) {
63-
return 2147483647;
64-
} else if (f < -2147483648) {
65-
return -2147483648;
66-
} else {
67-
return floor(f);
68-
}
69-
}
70-
71-
function random_int(min, max) {
72-
return floor_int(Math.random() * (max - min | 0)) + min | 0;
73-
}
74-
7561
function shuffleInPlace(xs) {
7662
let len = xs.length;
63+
let random_int = function (min, max) {
64+
return Math.floor(Math.random() * (max - min | 0)) + min | 0;
65+
};
7766
for(let i = 0; i < len; ++i){
7867
swapUnsafe(xs, i, random_int(i, len));
7968
}

belt/src/belt_Array.res

+5-22
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,15 @@ let swapUnsafe = (xs, i, j) => {
6464
setUnsafe(xs, j, tmp)
6565
}
6666

67-
// TODO? Parts of Js.Math/Js.Int that we need for the shuffle function
68-
module Js_math = {
69-
@val @scope("Math")
70-
external random: unit => float = "random"
71-
external intToFloat: int => float = "%floatofint"
72-
external unsafe_floor_int: float => int = "floor"
73-
74-
let intMax: int = 2147483647
75-
let intMin: int = -2147483648
76-
77-
let floor_int = f =>
78-
if f > intToFloat(intMax) {
79-
intMax
80-
} else if f < intToFloat(intMin) {
81-
intMin
82-
} else {
83-
unsafe_floor_int(f)
84-
}
85-
86-
let random_int = (min, max) => floor_int(random() *. intToFloat(max - min)) + min
87-
}
67+
@val @scope("Math") external random: unit => float = "random"
68+
@val @scope("Math") external floor: float => int = "floor"
69+
external toFloat: int => float = "%floatofint"
8870

8971
let shuffleInPlace = xs => {
9072
let len = length(xs)
73+
let random_int = (min, max) => floor(random() *. toFloat(max - min)) + min
9174
for i in 0 to len - 1 {
92-
swapUnsafe(xs, i, Js_math.random_int(i, len)) /* [i,len) */
75+
swapUnsafe(xs, i, random_int(i, len)) /* [i,len) */
9376
}
9477
}
9578

belt/src/belt_Array.resi

+26-26
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Belt.Array.rangeBy(3, 3, ~step=1) == [3]
205205
*/
206206
let rangeBy: (int, int, ~step: int) => array<int>
207207

208-
let makeByU: (int, (. int) => 'a) => t<'a>
208+
let makeByU: (int, int => 'a) => t<'a>
209209
/**
210210
`makeBy(n, f)` return an empty array when n is negative return an array of size
211211
n populated by `f(i)` start from `0` to `n - 1`.
@@ -220,7 +220,7 @@ Belt.Array.makeBy(5, (i) => i * i) == [0, 1, 4, 9, 16]
220220
*/
221221
let makeBy: (int, int => 'a) => t<'a>
222222

223-
let makeByAndShuffleU: (int, (. int) => 'a) => t<'a>
223+
let makeByAndShuffleU: (int, int => 'a) => t<'a>
224224
/**
225225
Equivalent to `shuffle(makeBy(n, f))`
226226
*/
@@ -238,7 +238,7 @@ Belt.Array.zip([1, 2], [3, 4, 5]) == [(1, 3), (2, 4)]
238238
*/
239239
let zip: (t<'a>, array<'b>) => array<('a, 'b)>
240240

241-
let zipByU: (t<'a>, array<'b>, (. 'a, 'b) => 'c) => array<'c>
241+
let zipByU: (t<'a>, array<'b>, ('a, 'b) => 'c) => array<'c>
242242
/**
243243
`zipBy(xs, ys, f)` create an array by applying `f` to corresponding elements of
244244
`xs` and `ys`. Stops with shorter array.
@@ -392,7 +392,7 @@ Unsafe blit without bounds checking.
392392
*/
393393
let blitUnsafe: (~src: t<'a>, ~srcOffset: int, ~dst: t<'a>, ~dstOffset: int, ~len: int) => unit
394394

395-
let forEachU: (t<'a>, (. 'a) => unit) => unit
395+
let forEachU: (t<'a>, 'a => unit) => unit
396396
/**
397397
`forEach(xs, f)`
398398

@@ -420,7 +420,7 @@ total.contents == 1 + 2 + 3 + 4
420420
*/
421421
let forEach: (t<'a>, 'a => unit) => unit
422422

423-
let mapU: (t<'a>, (. 'a) => 'b) => array<'b>
423+
let mapU: (t<'a>, 'a => 'b) => array<'b>
424424
/**
425425
`map(xs, f)` returns a new array by calling `f` for each element of `xs` from
426426
the beginning to end.
@@ -433,7 +433,7 @@ Belt.Array.map([1, 2], (x) => x + 1) == [3, 4]
433433
*/
434434
let map: (t<'a>, 'a => 'b) => array<'b>
435435

436-
let flatMapU: (t<'a>, (. 'a) => array<'b>) => array<'b>
436+
let flatMapU: (t<'a>, 'a => array<'b>) => array<'b>
437437
/**
438438
`flatMap(xs, f)` returns a new array by calling `f` for each element of `xs` from
439439
the beginning to end, concatenating the results.
@@ -446,7 +446,7 @@ Belt.Array.flatMap([1, 2], x => [x + 10, x + 20]) == [11, 21, 12, 22]
446446
*/
447447
let flatMap: (t<'a>, 'a => array<'b>) => array<'b>
448448

449-
let getByU: (t<'a>, (. 'a) => bool) => option<'a>
449+
let getByU: (t<'a>, 'a => bool) => option<'a>
450450
/**
451451
`getBy(xs, p)` returns `Some(value)` for the first value in `xs` that satisifies
452452
the predicate function `p`; returns `None` if no element satisifies the function.
@@ -460,7 +460,7 @@ Belt.Array.getBy([15, 13, 11], (x) => mod(x, 2) == 0) == None
460460
*/
461461
let getBy: (t<'a>, 'a => bool) => option<'a>
462462

463-
let getIndexByU: (t<'a>, (. 'a) => bool) => option<int>
463+
let getIndexByU: (t<'a>, 'a => bool) => option<int>
464464
/**
465465
`getIndexBy(xs, p)` returns `Some(index)` for the first value in `xs` that
466466
satisifies the predicate function `p`; returns `None` if no element satisifies
@@ -475,13 +475,13 @@ Belt.Array.getIndexBy([15, 13, 11], (x) => mod(x, 2) == 0) == None
475475
*/
476476
let getIndexBy: (t<'a>, 'a => bool) => option<int>
477477

478-
let keepU: (t<'a>, (. 'a) => bool) => t<'a>
478+
let keepU: (t<'a>, 'a => bool) => t<'a>
479479
/**
480480
`keep(xs, p)` returns a new array that keep all elements satisfy `p`.
481481
*/
482482
let keep: (t<'a>, 'a => bool) => t<'a>
483483

484-
let keepWithIndexU: (t<'a>, (. 'a, int) => bool) => t<'a>
484+
let keepWithIndexU: (t<'a>, ('a, int) => bool) => t<'a>
485485
/**
486486
`keepWithIndex(xs, p)` returns a new array that keep all elements satisfy `p`.
487487

@@ -493,7 +493,7 @@ Belt.Array.keepWithIndex([1, 2, 3], (_x, i) => i == 1) == [2]
493493
*/
494494
let keepWithIndex: (t<'a>, ('a, int) => bool) => t<'a>
495495

496-
let keepMapU: (t<'a>, (. 'a) => option<'b>) => array<'b>
496+
let keepMapU: (t<'a>, 'a => option<'b>) => array<'b>
497497
/**
498498
`keepMap(xs, p)` returns a new array that keep all elements that return a non
499499
None applied `p`.
@@ -513,7 +513,7 @@ Belt.Array.keepMap([1, 2, 3], x =>
513513
*/
514514
let keepMap: (t<'a>, 'a => option<'b>) => array<'b>
515515

516-
let forEachWithIndexU: (t<'a>, (. int, 'a) => unit) => unit
516+
let forEachWithIndexU: (t<'a>, (int, 'a) => unit) => unit
517517
/**
518518
`forEachWithIndex(xs, f)` same as `Belt.Array.forEach`, except that `f` is
519519
supplied two arguments: the index starting from 0 and the element from `xs`.
@@ -538,7 +538,7 @@ total.contents == 0 + 10 + 1 + 11 + 2 + 12 + 3 + 13
538538
*/
539539
let forEachWithIndex: (t<'a>, (int, 'a) => unit) => unit
540540

541-
let mapWithIndexU: (t<'a>, (. int, 'a) => 'b) => array<'b>
541+
let mapWithIndexU: (t<'a>, (int, 'a) => 'b) => array<'b>
542542
/**
543543
`mapWithIndex(xs, f)` applies `f` to each element of `xs`. Function `f` takes
544544
two arguments: the index starting from 0 and the element from `xs`.
@@ -551,7 +551,7 @@ Belt.Array.mapWithIndex([1, 2, 3], (i, x) => i + x) == [0 + 1, 1 + 2, 2 + 3]
551551
*/
552552
let mapWithIndex: (t<'a>, (int, 'a) => 'b) => array<'b>
553553

554-
let partitionU: (t<'a>, (. 'a) => bool) => (t<'a>, t<'a>)
554+
let partitionU: (t<'a>, 'a => bool) => (t<'a>, t<'a>)
555555
/**
556556
`partition(f, a)` split array into tuple of two arrays based on predicate `f`;
557557
first of tuple where predicate cause true, second where predicate cause false
@@ -566,7 +566,7 @@ Belt.Array.partition([1, 2, 3, 4, 5], (x) => mod(x, 2) != 0) == ([1, 3, 5], [2,
566566
*/
567567
let partition: (t<'a>, 'a => bool) => (t<'a>, t<'a>)
568568

569-
let reduceU: (array<'b>, 'a, (. 'a, 'b) => 'a) => 'a
569+
let reduceU: (array<'b>, 'a, ('a, 'b) => 'a) => 'a
570570
/**
571571
`reduce(xs, init, f)` applies `f` to each element of `xs` from beginning to end.
572572
Function `f` has two parameters: the item from the list and an “accumulator”;
@@ -583,7 +583,7 @@ Belt.Array.reduce(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "abcd"
583583
*/
584584
let reduce: (array<'b>, 'a, ('a, 'b) => 'a) => 'a
585585

586-
let reduceReverseU: (array<'b>, 'a, (. 'a, 'b) => 'a) => 'a
586+
let reduceReverseU: (array<'b>, 'a, ('a, 'b) => 'a) => 'a
587587
/**
588588
`reduceReverse(xs, init, f)` works like `Belt.Array.reduce` except that
589589
function `f` is applied to each item of `xs` from the last back to the first.
@@ -596,7 +596,7 @@ Belt.Array.reduceReverse(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "dcba"
596596
*/
597597
let reduceReverse: (array<'b>, 'a, ('a, 'b) => 'a) => 'a
598598

599-
let reduceReverse2U: (t<'a>, array<'b>, 'c, (. 'c, 'a, 'b) => 'c) => 'c
599+
let reduceReverse2U: (t<'a>, array<'b>, 'c, ('c, 'a, 'b) => 'c) => 'c
600600
/**
601601
`reduceReverse2(xs, ys, init, f)` reduces two arrays xs and ys;taking items
602602
starting at `min(length(xs), length(ys))` down to and including zero.
@@ -609,7 +609,7 @@ Belt.Array.reduceReverse2([1, 2, 3], [1, 2], 0, (acc, x, y) => acc + x + y) == 6
609609
*/
610610
let reduceReverse2: (t<'a>, array<'b>, 'c, ('c, 'a, 'b) => 'c) => 'c
611611

612-
let reduceWithIndexU: (t<'a>, 'b, (. 'b, 'a, int) => 'b) => 'b
612+
let reduceWithIndexU: (t<'a>, 'b, ('b, 'a, int) => 'b) => 'b
613613
/**
614614
Applies `f` to each element of `xs` from beginning to end. Function `f` has
615615
three parameters: the item from the array and an “accumulator”, which starts
@@ -624,7 +624,7 @@ Belt.Array.reduceWithIndex([1, 2, 3, 4], 0, (acc, x, i) => acc + x + i) == 16
624624
*/
625625
let reduceWithIndex: (t<'a>, 'b, ('b, 'a, int) => 'b) => 'b
626626

627-
let joinWithU: (t<'a>, string, (. 'a) => string) => string
627+
let joinWithU: (t<'a>, string, 'a => string) => string
628628
/**
629629
`joinWith(xs, sep, toString)`
630630

@@ -643,7 +643,7 @@ Belt.Array.joinWith([1], " ", Js.Int.toString) == "1"
643643
*/
644644
let joinWith: (t<'a>, string, 'a => string) => string
645645

646-
let someU: (t<'a>, (. 'a) => bool) => bool
646+
let someU: (t<'a>, 'a => bool) => bool
647647
/**
648648
`some(xs, p)` returns true if at least one of the elements in `xs` satifies `p`;
649649
where `p` is a predicate: a function taking an element and returning a `bool`.
@@ -658,7 +658,7 @@ Belt.Array.some([(-1), (-3), (-5)], (x) => x > 0) == false
658658
*/
659659
let some: (t<'a>, 'a => bool) => bool
660660

661-
let everyU: (t<'a>, (. 'a) => bool) => bool
661+
let everyU: (t<'a>, 'a => bool) => bool
662662
/**
663663
`every(xs, p)` returns `true` if all elements satisfy `p`; where `p` is a
664664
predicate: a function taking an element and returning a `bool`.
@@ -673,7 +673,7 @@ Belt.Array.every([1, (-3), 5], (x) => x > 0) == false
673673
*/
674674
let every: (t<'a>, 'a => bool) => bool
675675

676-
let every2U: (t<'a>, array<'b>, (. 'a, 'b) => bool) => bool
676+
let every2U: (t<'a>, array<'b>, ('a, 'b) => bool) => bool
677677
/**
678678
`every2(xs, ys, p)` returns true if `p(xi, yi)` is true for all pairs of
679679
elements up to the shorter length (i.e. `min(length(xs), length(ys))`)
@@ -692,7 +692,7 @@ Belt.Array.every2([0, 1], [5, 0], (x, y) => x > y) == false
692692
*/
693693
let every2: (t<'a>, array<'b>, ('a, 'b) => bool) => bool
694694

695-
let some2U: (t<'a>, array<'b>, (. 'a, 'b) => bool) => bool
695+
let some2U: (t<'a>, array<'b>, ('a, 'b) => bool) => bool
696696
/**
697697
`some2(xs, ys, p)` returns true if `p(xi, yi)` is true for any pair of elements
698698
up to the shorter length (i.e. `min(length(xs), length(ys))`)
@@ -709,7 +709,7 @@ Belt.Array.some2([2, 3], [1, 4], (x, y) => x > y) == true
709709
*/
710710
let some2: (t<'a>, array<'b>, ('a, 'b) => bool) => bool
711711

712-
let cmpU: (t<'a>, t<'a>, (. 'a, 'a) => int) => int
712+
let cmpU: (t<'a>, t<'a>, ('a, 'a) => int) => int
713713
/**
714714
`cmp(xs, ys, f)` compared by length if `length(xs) != length(ys)`; returning `-1`
715715
if `length(xs) < length(ys)` or 1 if `length(xs) > length(ys)`. Otherwise
@@ -730,7 +730,7 @@ Belt.Array.cmp([1, 3, 5], [1, 3, 5], (a, b) => compare(a, b)) == 0
730730
*/
731731
let cmp: (t<'a>, t<'a>, ('a, 'a) => int) => int
732732

733-
let eqU: (t<'a>, t<'a>, (. 'a, 'a) => bool) => bool
733+
let eqU: (t<'a>, t<'a>, ('a, 'a) => bool) => bool
734734
/**
735735
`eq(xs, ys)` return `false` if length is not the same otherwise compare items
736736
one by one using `f(xi, yi)`; and return true if all results are true false otherwise
@@ -761,7 +761,7 @@ arr == ["ant", "bee", "cat"]
761761
*/
762762
external truncateToLengthUnsafe: (t<'a>, int) => unit = "length"
763763

764-
let initU: (int, (. int) => 'a) => t<'a>
764+
let initU: (int, int => 'a) => t<'a>
765765
let init: (int, int => 'a) => t<'a>
766766

767767
/**

0 commit comments

Comments
 (0)