Skip to content

Commit a2e2df9

Browse files
committed
refactor(array): replace reduceReverse with native binding to reduceRight
1 parent 0090ab6 commit a2e2df9

File tree

5 files changed

+19
-35
lines changed

5 files changed

+19
-35
lines changed

src/Core__Array.mjs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ function reduceWithIndex(a, x, f) {
5555
return r;
5656
}
5757

58-
function reduceReverse(a, x, f) {
59-
var f$1 = Curry.__2(f);
60-
var r = x;
61-
for(var i = a.length - 1 | 0; i >= 0; --i){
62-
r = f$1(r, a[i]);
63-
}
64-
return r;
65-
}
66-
6758
function findIndexOpt(array, finder) {
6859
var index = array.findIndex(finder);
6960
if (index !== -1) {
@@ -146,7 +137,6 @@ export {
146137
sort ,
147138
indexOfOpt ,
148139
lastIndexOfOpt ,
149-
reduceReverse ,
150140
reduceWithIndex ,
151141
findIndexOpt ,
152142
reverse ,

src/Core__Array.res

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,8 @@ let reduceWithIndexU = (a, x, f) => {
139139

140140
let reduceWithIndex = (a, x, f) => reduceWithIndexU(a, x, (. a, b, c) => f(a, b, c))
141141

142-
let reduceReverseU = (a, x, f) => {
143-
let r = ref(x)
144-
for i in length(a) - 1 downto 0 {
145-
r.contents = f(. r.contents, getUnsafe(a, i))
146-
}
147-
r.contents
148-
}
149-
150-
let reduceReverse = (a, x, f) => reduceReverseU(a, x, (. a, b) => f(a, b))
142+
@send
143+
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"
151144

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

src/Core__Array.resi

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

7575
/**
76-
`reduceReverse(xs, init, f)`
76+
`reduceRight(xs, f, init)`
7777
7878
Works like `Array.reduce`; except that function `f` is applied to each item of `xs` from the last back to the first.
7979
8080
```res example
81-
Array.reduceReverse(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "dcba"
81+
Array.reduceRight(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "dcba"
8282
```
8383
*/
84-
let reduceReverse: (array<'b>, 'a, ('a, 'b) => 'a) => 'a
84+
@send
85+
external reduceRight: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduceRight"
8586

8687
/**
8788
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.

test/ArrayTests.mjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ Test.run([
8282
"ArrayTests.res",
8383
18,
8484
13,
85-
28
85+
26
8686
],
87-
"reduceReverse"
88-
], Core__Array.reduceReverse([
89-
1,
90-
2,
91-
3
92-
], /* [] */0, Core__List.add), eq, {
87+
"reduceRight"
88+
], [
89+
1,
90+
2,
91+
3
92+
].reduceRight(Core__List.add, /* [] */0), eq, {
9393
hd: 1,
9494
tl: {
9595
hd: 2,
@@ -105,10 +105,10 @@ Test.run([
105105
"ArrayTests.res",
106106
23,
107107
20,
108-
43
108+
41
109109
],
110-
"reduceReverse - empty"
111-
], Core__Array.reduceReverse([], /* [] */0, Core__List.add), eq, /* [] */0);
110+
"reduceRight - empty"
111+
], [].reduceRight(Core__List.add, /* [] */0), eq, /* [] */0);
112112

113113
Test.run([
114114
[

test/ArrayTests.res

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

1717
Test.run(
18-
__POS_OF__("reduceReverse"),
19-
Array.reduceReverse([1, 2, 3], list{}, List.add),
18+
__POS_OF__("reduceRight"),
19+
Array.reduceRight([1, 2, 3], List.add, list{}),
2020
eq,
2121
list{1, 2, 3},
2222
)
23-
Test.run(__POS_OF__("reduceReverse - empty"), Array.reduceReverse([], list{}, List.add), eq, list{})
23+
Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], List.add, list{}), eq, list{})
2424

2525
Test.run(__POS_OF__("shuffle - length"), Array.shuffle([1, 2, 3])->Array.length, eq, 3)
2626

0 commit comments

Comments
 (0)