Skip to content

Commit 3042949

Browse files
committed
refactor(array): use native reduceWithIndex instead of reimplementing it
1 parent a2e2df9 commit 3042949

File tree

5 files changed

+80
-41
lines changed

5 files changed

+80
-41
lines changed

src/Core__Array.mjs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ function sort(arr, cmp) {
4646
return result;
4747
}
4848

49-
function reduceWithIndex(a, x, f) {
50-
var f$1 = Curry.__3(f);
51-
var r = x;
52-
for(var i = 0 ,i_finish = a.length; i < i_finish; ++i){
53-
r = f$1(r, a[i], i);
54-
}
55-
return r;
56-
}
57-
5849
function findIndexOpt(array, finder) {
5950
var index = array.findIndex(finder);
6051
if (index !== -1) {
@@ -137,7 +128,6 @@ export {
137128
sort ,
138129
indexOfOpt ,
139130
lastIndexOfOpt ,
140-
reduceWithIndex ,
141131
findIndexOpt ,
142132
reverse ,
143133
filterMap ,

src/Core__Array.res

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,7 @@ let sort = (arr, cmp) => {
128128
@send external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map"
129129

130130
@send external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce"
131-
132-
let reduceWithIndexU = (a, x, f) => {
133-
let r = ref(x)
134-
for i in 0 to length(a) - 1 {
135-
r.contents = f(. r.contents, getUnsafe(a, i), i)
136-
}
137-
r.contents
138-
}
139-
140-
let reduceWithIndex = (a, x, f) => reduceWithIndexU(a, x, (. a, b, c) => f(a, b, c))
141-
131+
@send external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce"
142132
@send
143133
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"
144134

src/Core__Array.resi

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,28 @@ let lastIndexOfOpt: (array<'a>, 'a) => option<int>
7373
external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce"
7474

7575
/**
76-
`reduceRight(xs, f, init)`
76+
`reduceWithIndex(xs, f, init)`
7777
78-
Works like `Array.reduce`; except that function `f` is applied to each item of `xs` from the last back to the first.
78+
Applies `f` to each element of `xs` from beginning to end. Function `f` has three parameters: the item from the array and an “accumulator”, which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator.
7979
8080
```res example
81-
Array.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba"
81+
Array.reduceWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16
8282
```
8383
*/
8484
@send
85-
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"
85+
external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce"
8686

8787
/**
88-
Applies `f` to each element of `xs` from beginning to end. Function `f` has three parameters: the item from the array and an “accumulator”, which starts with a value of `init` and the index of each element. `reduceWithIndex` returns the final value of the accumulator.
88+
`reduceRight(xs, f, init)`
89+
90+
Works like `Array.reduce`; except that function `f` is applied to each item of `xs` from the last back to the first.
8991
9092
```res example
91-
Array.reduceWithIndex([1, 2, 3, 4], 0, (acc, x, i) => acc + x + i) == 16
93+
Array.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba"
9294
```
9395
*/
94-
let reduceWithIndex: (array<'a>, 'b, ('b, 'a, int) => 'b) => 'b
96+
@send
97+
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"
9598

9699
@send external some: (array<'a>, 'a => bool) => bool = "some"
97100
@send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some"

test/ArrayTests.mjs

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,49 @@ Test.run([
8282
"ArrayTests.res",
8383
18,
8484
13,
85+
30
86+
],
87+
"reduceWithIndex"
88+
], [
89+
1,
90+
2,
91+
3
92+
].reduce((function (acc, v, i) {
93+
return {
94+
hd: v + i | 0,
95+
tl: acc
96+
};
97+
}), /* [] */0), eq, {
98+
hd: 5,
99+
tl: {
100+
hd: 3,
101+
tl: {
102+
hd: 1,
103+
tl: /* [] */0
104+
}
105+
}
106+
});
107+
108+
Test.run([
109+
[
110+
"ArrayTests.res",
111+
24,
112+
13,
113+
38
114+
],
115+
"reduceWithIndex - empty"
116+
], [].reduce((function (acc, v, i) {
117+
return {
118+
hd: v + i | 0,
119+
tl: acc
120+
};
121+
}), /* [] */0), eq, /* [] */0);
122+
123+
Test.run([
124+
[
125+
"ArrayTests.res",
126+
31,
127+
13,
85128
26
86129
],
87130
"reduceRight"
@@ -103,7 +146,7 @@ Test.run([
103146
Test.run([
104147
[
105148
"ArrayTests.res",
106-
23,
149+
36,
107150
20,
108151
41
109152
],
@@ -113,7 +156,7 @@ Test.run([
113156
Test.run([
114157
[
115158
"ArrayTests.res",
116-
25,
159+
38,
117160
20,
118161
38
119162
],
@@ -133,7 +176,7 @@ var arr = [
133176
Test.run([
134177
[
135178
"ArrayTests.res",
136-
28,
179+
41,
137180
13,
138181
38
139182
],
@@ -143,7 +186,7 @@ Test.run([
143186
Test.run([
144187
[
145188
"ArrayTests.res",
146-
39,
189+
52,
147190
13,
148191
24
149192
],
@@ -169,7 +212,7 @@ Test.run([
169212
Test.run([
170213
[
171214
"ArrayTests.res",
172-
44,
215+
57,
173216
20,
174217
42
175218
],
@@ -188,7 +231,7 @@ Test.run([
188231
Test.run([
189232
[
190233
"ArrayTests.res",
191-
46,
234+
59,
192235
13,
193236
32
194237
],
@@ -203,7 +246,7 @@ Test.run([
203246
Test.run([
204247
[
205248
"ArrayTests.res",
206-
52,
249+
65,
207250
20,
208251
30
209252
],
@@ -220,7 +263,7 @@ Test.run([
220263
Test.run([
221264
[
222265
"ArrayTests.res",
223-
54,
266+
67,
224267
13,
225268
34
226269
],
@@ -238,7 +281,7 @@ Test.run([
238281
Test.run([
239282
[
240283
"ArrayTests.res",
241-
59,
284+
72,
242285
20,
243286
41
244287
],
@@ -252,7 +295,7 @@ Test.run([
252295
Test.run([
253296
[
254297
"ArrayTests.res",
255-
60,
298+
73,
256299
20,
257300
38
258301
],
@@ -262,7 +305,7 @@ Test.run([
262305
Test.run([
263306
[
264307
"ArrayTests.res",
265-
63,
308+
76,
266309
13,
267310
22
268311
],
@@ -284,7 +327,7 @@ Test.run([
284327
Test.run([
285328
[
286329
"ArrayTests.res",
287-
68,
330+
81,
288331
20,
289332
40
290333
],
@@ -303,7 +346,7 @@ Test.run([
303346
Test.run([
304347
[
305348
"ArrayTests.res",
306-
70,
349+
83,
307350
13,
308351
30
309352
],

test/ArrayTests.res

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ Test.run(
1414
Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], List.add, list{}), eq, list{3, 2, 1})
1515
Test.run(__POS_OF__("reduce - empty"), Array.reduce([], List.add, list{}), eq, list{})
1616

17+
Test.run(
18+
__POS_OF__("reduceWithIndex"),
19+
Array.reduceWithIndex([1, 2, 3], (acc, v, i) => list{v + i, ...acc}, list{}),
20+
eq,
21+
list{5, 3, 1},
22+
)
23+
Test.run(
24+
__POS_OF__("reduceWithIndex - empty"),
25+
Array.reduceWithIndex([], (acc, v, i) => list{v + i, ...acc}, list{}),
26+
eq,
27+
list{},
28+
)
29+
1730
Test.run(
1831
__POS_OF__("reduceRight"),
1932
Array.reduceRight([1, 2, 3], List.add, list{}),

0 commit comments

Comments
 (0)