Skip to content

Commit 0090ab6

Browse files
committed
refactor(array): bind to native reduce instead of reimplementing it
1 parent 93110d0 commit 0090ab6

File tree

7 files changed

+19
-37
lines changed

7 files changed

+19
-37
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 reduce(a, x, f) {
50-
var f$1 = Curry.__2(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]);
54-
}
55-
return r;
56-
}
57-
5849
function reduceWithIndex(a, x, f) {
5950
var f$1 = Curry.__3(f);
6051
var r = x;
@@ -155,7 +146,6 @@ export {
155146
sort ,
156147
indexOfOpt ,
157148
lastIndexOfOpt ,
158-
reduce ,
159149
reduceReverse ,
160150
reduceWithIndex ,
161151
findIndexOpt ,

src/Core__Array.res

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

130-
let reduceU = (a, x, f) => {
131-
let r = ref(x)
132-
for i in 0 to length(a) - 1 {
133-
r.contents = f(. r.contents, getUnsafe(a, i))
134-
}
135-
r.contents
136-
}
137-
138-
let reduce = (a, x, f) => reduceU(a, x, (. a, b) => f(a, b))
130+
@send external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce"
139131

140132
let reduceWithIndexU = (a, x, f) => {
141133
let r = ref(x)

src/Core__Array.resi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ let lastIndexOfOpt: (array<'a>, 'a) => option<int>
5959
@send external mapWithIndex: (array<'a>, ('a, int) => 'b) => array<'b> = "map"
6060

6161
/**
62-
`reduce(xs, init, f)`
62+
`reduce(xs, f, init)`
6363
6464
Applies `f` to each element of `xs` from beginning to end. Function `f` has two parameters: the item from the list and an “accumulator”; which starts with a value of `init`. `reduce` returns the final value of the accumulator.
6565
6666
```res example
67-
Array.reduce([2, 3, 4], 1, (a, b) => a + b) == 10
67+
Array.reduce([2, 3, 4], (a, b) => a + b, 1) == 10
6868
69-
Array.reduce(["a", "b", "c", "d"], "", (a, b) => a ++ b) == "abcd"
69+
Array.reduce(["a", "b", "c", "d"], (a, b) => a ++ b, "") == "abcd"
7070
```
7171
*/
72-
let reduce: (array<'b>, 'a, ('a, 'b) => 'a) => 'a
72+
@send
73+
external reduce: (array<'b>, ('a, 'b) => 'a, 'a) => 'a = "reduce"
7374

7475
/**
7576
`reduceReverse(xs, init, f)`

test/ArrayTests.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ Test.run([
5252
28
5353
],
5454
"reduce"
55-
], Core__Array.reduce([
56-
1,
57-
2,
58-
3
59-
], /* [] */0, Core__List.add), eq, {
55+
], [
56+
1,
57+
2,
58+
3
59+
].reduce(Core__List.add, /* [] */0), eq, {
6060
hd: 3,
6161
tl: {
6262
hd: 2,
@@ -75,7 +75,7 @@ Test.run([
7575
36
7676
],
7777
"reduce - empty"
78-
], Core__Array.reduce([], /* [] */0, Core__List.add), eq, /* [] */0);
78+
], [].reduce(Core__List.add, /* [] */0), eq, /* [] */0);
7979

8080
Test.run([
8181
[

test/ArrayTests.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Test.run(
1111
[3, 4, 5, 6, 7, 8, 9],
1212
)
1313

14-
Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], list{}, List.add), eq, list{3, 2, 1})
15-
Test.run(__POS_OF__("reduce - empty"), Array.reduce([], list{}, List.add), eq, list{})
14+
Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], List.add, list{}), eq, list{3, 2, 1})
15+
Test.run(__POS_OF__("reduce - empty"), Array.reduce([], List.add, list{}), eq, list{})
1616

1717
Test.run(
1818
__POS_OF__("reduceReverse"),

test/TempTests.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as Core__Int from "../src/Core__Int.mjs";
44
import * as Core__Dict from "../src/Core__Dict.mjs";
55
import * as Core__JSON from "../src/Core__JSON.mjs";
66
import * as Caml_option from "rescript/lib/es6/caml_option.js";
7-
import * as Core__Array from "../src/Core__Array.mjs";
87
import * as Core__Float from "../src/Core__Float.mjs";
98
import * as Core__BigInt from "../src/Core__BigInt.mjs";
109
import * as Core__Option from "../src/Core__Option.mjs";
@@ -22,11 +21,11 @@ var array = [
2221
4
2322
];
2423

25-
console.info(Core__Array.reduce(array.map(function (x) {
26-
return (x << 1);
27-
}), 0, (function (a, b) {
24+
console.info(array.map(function (x) {
25+
return (x << 1);
26+
}).reduce((function (a, b) {
2827
return a + b | 0;
29-
})));
28+
}), 0));
3029

3130
console.info(typeof array);
3231

test/TempTests.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Console.info("")
44
Console.info("Array")
55
Console.info("---")
66
let array = [1, 2, 3, 4]
7-
Console.info(array->Array.map(x => x * 2)->Array.reduce(0, (a, b) => a + b))
7+
Console.info(array->Array.map(x => x * 2)->Array.reduce((a, b) => a + b, 0))
88
Console.info(typeof(array))
99

1010
Console.info("")

0 commit comments

Comments
 (0)