Skip to content

Commit 8eb7433

Browse files
committed
feat(array): add reduceRightWithIndex
1 parent 3042949 commit 8eb7433

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

src/Core__Array.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ let sort = (arr, cmp) => {
131131
@send external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce"
132132
@send
133133
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"
134+
@send
135+
external reduceRightWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduceRight"
134136

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

src/Core__Array.resi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ external reduceWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduce"
9696
@send
9797
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"
9898

99+
/**
100+
`reduceRightWithIndex(xs, f, init)`
101+
102+
Like `reduceRight`, but with an additional index argument on the callback function.
103+
104+
```res example
105+
Array.reduceRightWithIndex([1, 2, 3, 4], (acc, x, i) => acc + x + i, 0) == 16
106+
```
107+
*/
108+
@send
109+
external reduceRightWithIndex: (array<'b>, ('a, 'b, int) => 'a, 'a) => 'a = "reduceRight"
110+
99111
@send external some: (array<'a>, 'a => bool) => bool = "some"
100112
@send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some"
101113
@get_index external get: (array<'a>, int) => option<'a> = ""

test/ArrayTests.mjs

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,50 @@ Test.run([
156156
Test.run([
157157
[
158158
"ArrayTests.res",
159-
38,
159+
39,
160+
13,
161+
35
162+
],
163+
"reduceEightWithIndex"
164+
], [
165+
1,
166+
2,
167+
3
168+
].reduceRight((function (acc, v, i) {
169+
return {
170+
hd: v + i | 0,
171+
tl: acc
172+
};
173+
}), /* [] */0), eq, {
174+
hd: 1,
175+
tl: {
176+
hd: 3,
177+
tl: {
178+
hd: 5,
179+
tl: /* [] */0
180+
}
181+
}
182+
});
183+
184+
Test.run([
185+
[
186+
"ArrayTests.res",
187+
45,
188+
13,
189+
38
190+
],
191+
"reduceWithIndex - empty"
192+
], [].reduceRight((function (acc, v, i) {
193+
return {
194+
hd: v + i | 0,
195+
tl: acc
196+
};
197+
}), /* [] */0), eq, /* [] */0);
198+
199+
Test.run([
200+
[
201+
"ArrayTests.res",
202+
51,
160203
20,
161204
38
162205
],
@@ -176,7 +219,7 @@ var arr = [
176219
Test.run([
177220
[
178221
"ArrayTests.res",
179-
41,
222+
54,
180223
13,
181224
38
182225
],
@@ -186,7 +229,7 @@ Test.run([
186229
Test.run([
187230
[
188231
"ArrayTests.res",
189-
52,
232+
65,
190233
13,
191234
24
192235
],
@@ -212,7 +255,7 @@ Test.run([
212255
Test.run([
213256
[
214257
"ArrayTests.res",
215-
57,
258+
70,
216259
20,
217260
42
218261
],
@@ -231,7 +274,7 @@ Test.run([
231274
Test.run([
232275
[
233276
"ArrayTests.res",
234-
59,
277+
72,
235278
13,
236279
32
237280
],
@@ -246,7 +289,7 @@ Test.run([
246289
Test.run([
247290
[
248291
"ArrayTests.res",
249-
65,
292+
78,
250293
20,
251294
30
252295
],
@@ -263,7 +306,7 @@ Test.run([
263306
Test.run([
264307
[
265308
"ArrayTests.res",
266-
67,
309+
80,
267310
13,
268311
34
269312
],
@@ -281,7 +324,7 @@ Test.run([
281324
Test.run([
282325
[
283326
"ArrayTests.res",
284-
72,
327+
85,
285328
20,
286329
41
287330
],
@@ -295,7 +338,7 @@ Test.run([
295338
Test.run([
296339
[
297340
"ArrayTests.res",
298-
73,
341+
86,
299342
20,
300343
38
301344
],
@@ -305,7 +348,7 @@ Test.run([
305348
Test.run([
306349
[
307350
"ArrayTests.res",
308-
76,
351+
89,
309352
13,
310353
22
311354
],
@@ -327,7 +370,7 @@ Test.run([
327370
Test.run([
328371
[
329372
"ArrayTests.res",
330-
81,
373+
94,
331374
20,
332375
40
333376
],
@@ -346,7 +389,7 @@ Test.run([
346389
Test.run([
347390
[
348391
"ArrayTests.res",
349-
83,
392+
96,
350393
13,
351394
30
352395
],

test/ArrayTests.res

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ Test.run(
3535
)
3636
Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], List.add, list{}), eq, list{})
3737

38+
Test.run(
39+
__POS_OF__("reduceEightWithIndex"),
40+
Array.reduceRightWithIndex([1, 2, 3], (acc, v, i) => list{v + i, ...acc}, list{}),
41+
eq,
42+
list{1, 3, 5},
43+
)
44+
Test.run(
45+
__POS_OF__("reduceWithIndex - empty"),
46+
Array.reduceRightWithIndex([], (acc, v, i) => list{v + i, ...acc}, list{}),
47+
eq,
48+
list{},
49+
)
50+
3851
Test.run(__POS_OF__("shuffle - length"), Array.shuffle([1, 2, 3])->Array.length, eq, 3)
3952

4053
Test.run(

0 commit comments

Comments
 (0)