Skip to content

Commit ffd4291

Browse files
authored
Merge branch 'main' into feat/option/additions
2 parents d4208d8 + 23ace1f commit ffd4291

9 files changed

+732
-81
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
- Change `Float.parseFloat` signature. Now accepts only string. https://github.com/rescript-association/rescript-core/pull/54
2020
- Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Nullable`. https://github.com/rescript-association/rescript-core/pull/67
2121
- Add `getExn`, `getUnsafe`, `getWithDefault`, `map`, `mapWithDefault` and `flatMap` to `Null`. https://github.com/rescript-association/rescript-core/pull/73
22+
- Add `make`, `fromInitializer`, `findMap`, `keepSome`, `reduceRight` and `reduceRightWithIndex`. https://github.com/rescript-association/rescript-core/pull/49
23+
- Remove `reduceReverse` in favor of `reduceRight`. https://github.com/rescript-association/rescript-core/pull/49
24+
- Fixed type signatures of `reduce` and `reduceWithIndex`. https://github.com/rescript-association/rescript-core/pull/49
2225
- Add `flat`, `expect`, and `or` to `Option`, deprecate `orElse`. https://github.com/rescript-association/rescript-core/pull/57
2326

2427
### Documentation

Diff for: package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
"clean": "rescript clean",
66
"build": "rescript",
77
"watch": "rescript build -w",
8-
"test": "node test/PromiseTest.mjs && node test/TempTests.mjs"
8+
"test": "node test/TestSuite.mjs && node test/TempTests.mjs"
99
},
10-
"keywords": [
11-
"rescript"
12-
],
10+
"keywords": ["rescript"],
1311
"homepage": "https://github.com/rescript-association/rescript-core",
1412
"author": "ReScript Team",
1513
"license": "MIT",

Diff for: src/Core__Array.mjs

+56-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,26 @@
33
import * as Curry from "rescript/lib/es6/curry.js";
44
import * as Js_math from "rescript/lib/es6/js_math.js";
55
import * as Caml_option from "rescript/lib/es6/caml_option.js";
6-
import * as Caml_splice_call from "rescript/lib/es6/caml_splice_call.js";
6+
7+
function make(length, x) {
8+
if (length <= 0) {
9+
return [];
10+
}
11+
var arr = new Array(length);
12+
arr.fill(x);
13+
return arr;
14+
}
15+
16+
function fromInitializer(length, f) {
17+
if (length <= 0) {
18+
return [];
19+
}
20+
var arr = new Array(length);
21+
for(var i = 0; i < length; ++i){
22+
arr[i] = Curry._1(f, i);
23+
}
24+
return arr;
25+
}
726

827
function indexOfOpt(arr, item) {
928
var index = arr.indexOf(item);
@@ -27,31 +46,20 @@ function sort(arr, cmp) {
2746
return result;
2847
}
2948

30-
function reduce(a, x, f) {
31-
var f$1 = Curry.__2(f);
32-
var r = x;
33-
for(var i = 0 ,i_finish = a.length; i < i_finish; ++i){
34-
r = f$1(r, a[i]);
35-
}
36-
return r;
49+
function reduce(arr, init, f) {
50+
return arr.reduce(f, init);
3751
}
3852

39-
function reduceWithIndex(a, x, f) {
40-
var f$1 = Curry.__3(f);
41-
var r = x;
42-
for(var i = 0 ,i_finish = a.length; i < i_finish; ++i){
43-
r = f$1(r, a[i], i);
44-
}
45-
return r;
53+
function reduceWithIndex(arr, init, f) {
54+
return arr.reduce(f, init);
4655
}
4756

48-
function reduceReverse(a, x, f) {
49-
var f$1 = Curry.__2(f);
50-
var r = x;
51-
for(var i = a.length - 1 | 0; i >= 0; --i){
52-
r = f$1(r, a[i]);
53-
}
54-
return r;
57+
function reduceRight(arr, init, f) {
58+
return arr.reduceRight(f, init);
59+
}
60+
61+
function reduceRightWithIndex(arr, init, f) {
62+
return arr.reduceRight(f, init);
5563
}
5664

5765
function findIndexOpt(array, finder) {
@@ -108,22 +116,44 @@ function filterMap(a, f) {
108116
return r;
109117
}
110118

111-
function flatMap(a, f) {
112-
return Caml_splice_call.spliceObjApply([], "concat", [a.map(f)]);
119+
function keepSome(__x) {
120+
return filterMap(__x, (function (x) {
121+
return x;
122+
}));
123+
}
124+
125+
function findMap(arr, f) {
126+
var _i = 0;
127+
while(true) {
128+
var i = _i;
129+
if (i === arr.length) {
130+
return ;
131+
}
132+
var r = Curry._1(f, arr[i]);
133+
if (r !== undefined) {
134+
return r;
135+
}
136+
_i = i + 1 | 0;
137+
continue ;
138+
};
113139
}
114140

115141
export {
142+
make ,
143+
fromInitializer ,
116144
sort ,
117145
indexOfOpt ,
118146
lastIndexOfOpt ,
119147
reduce ,
120-
reduceReverse ,
121148
reduceWithIndex ,
149+
reduceRight ,
150+
reduceRightWithIndex ,
122151
findIndexOpt ,
123152
reverse ,
124153
filterMap ,
154+
keepSome ,
125155
shuffle ,
126156
shuffleInPlace ,
127-
flatMap ,
157+
findMap ,
128158
}
129159
/* No side effect */

Diff for: src/Core__Array.res

+55-39
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@new external makeUninitializedUnsafe: int => array<'a> = "Array"
2+
@set external truncateToLengthUnsafe: (array<'a>, int) => unit = "length"
13
external getUnsafe: (array<'a>, int) => 'a = "%array_unsafe_get"
24
external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set"
35

@@ -11,6 +13,32 @@ external fromArrayLikeWithMap: (Js.Array2.array_like<'a>, 'a => 'b) => array<'b>
1113
@val external fromIterator: Core__Iterator.t<'a> => array<'a> = "Array.from"
1214
@val external fromIteratorWithMap: (Core__Iterator.t<'a>, 'a => 'b) => array<'b> = "Array.from"
1315

16+
@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill"
17+
18+
@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
19+
20+
@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
21+
22+
let make = (~length, x) =>
23+
if length <= 0 {
24+
[]
25+
} else {
26+
let arr = makeUninitializedUnsafe(length)
27+
arr->fillAllInPlace(x)
28+
arr
29+
}
30+
31+
let fromInitializer = (~length, f) =>
32+
if length <= 0 {
33+
[]
34+
} else {
35+
let arr = makeUninitializedUnsafe(length)
36+
for i in 0 to length - 1 {
37+
arr->setUnsafe(i, f(i))
38+
}
39+
arr
40+
}
41+
1442
@val external isArray: 'a => bool = "Array.isArray"
1543

1644
@get external length: array<'a> => int = "length"
@@ -23,12 +51,6 @@ external copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a> =
2351
@send
2452
external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<'a> = "copyWithin"
2553

26-
@send external fillAllInPlace: (array<'a>, 'a) => unit = "fill"
27-
28-
@send external fillInPlaceToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
29-
30-
@send external fillInPlace: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
31-
3254
@send external pop: array<'a> => option<'a> = "pop"
3355

3456
@send external push: (array<'a>, 'a) => unit = "push"
@@ -105,35 +127,16 @@ let sort = (arr, cmp) => {
105127
@send external map: (array<'a>, 'a => 'b) => array<'b> = "map"
106128
@send external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map"
107129

108-
let reduceU = (a, x, f) => {
109-
let r = ref(x)
110-
for i in 0 to length(a) - 1 {
111-
r.contents = f(. r.contents, getUnsafe(a, i))
112-
}
113-
r.contents
114-
}
115-
116-
let reduce = (a, x, f) => reduceU(a, x, (. a, b) => f(a, b))
117-
118-
let reduceWithIndexU = (a, x, f) => {
119-
let r = ref(x)
120-
for i in 0 to length(a) - 1 {
121-
r.contents = f(. r.contents, getUnsafe(a, i), i)
122-
}
123-
r.contents
124-
}
125-
126-
let reduceWithIndex = (a, x, f) => reduceWithIndexU(a, x, (. a, b, c) => f(a, b, c))
127-
128-
let reduceReverseU = (a, x, f) => {
129-
let r = ref(x)
130-
for i in length(a) - 1 downto 0 {
131-
r.contents = f(. r.contents, getUnsafe(a, i))
132-
}
133-
r.contents
134-
}
135-
136-
let reduceReverse = (a, x, f) => reduceReverseU(a, x, (. a, b) => f(a, b))
130+
@send external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce"
131+
let reduce = (arr, init, f) => reduce(arr, f, init)
132+
@send external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce"
133+
let reduceWithIndex = (arr, init, f) => reduceWithIndex(arr, f, init)
134+
@send
135+
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"
136+
let reduceRight = (arr, init, f) => reduceRight(arr, f, init)
137+
@send
138+
external reduceRightWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduceRight"
139+
let reduceRightWithIndex = (arr, init, f) => reduceRightWithIndex(arr, f, init)
137140

138141
@send external some: (array<'a>, 'a => bool) => bool = "some"
139142
@send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some"
@@ -151,8 +154,6 @@ let findIndexOpt = (array: array<'a>, finder: 'a => bool): option<int> =>
151154
| index => Some(index)
152155
}
153156

154-
@new external makeUninitializedUnsafe: int => array<'a> = "Array"
155-
@set external truncateToLengthUnsafe: (array<'a>, int) => unit = "length"
156157
let swapUnsafe = (xs, i, j) => {
157158
let tmp = getUnsafe(xs, i)
158159
setUnsafe(xs, i, getUnsafe(xs, j))
@@ -200,7 +201,22 @@ let filterMapU = (a, f) => {
200201

201202
let filterMap = (a, f) => filterMapU(a, (. a) => f(a))
202203

203-
// TODO: Change this implementation?
204-
let flatMap = (a, f) => []->concatMany(map(a, f))
204+
let keepSome = filterMap(_, x => x)
205+
206+
@send external flatMap: (array<'a>, 'a => array<'b>) => array<'b> = "flatMap"
207+
208+
let findMap = (arr, f) => {
209+
let rec loop = i =>
210+
if i == arr->length {
211+
None
212+
} else {
213+
switch f(getUnsafe(arr, i)) {
214+
| None => loop(i + 1)
215+
| Some(_) as r => r
216+
}
217+
}
218+
219+
loop(0)
220+
}
205221

206222
@send external at: (array<'a>, int) => option<'a> = "at"

0 commit comments

Comments
 (0)