Skip to content

Commit acb50bc

Browse files
committed
Clean up our hashing functions
Use arrow functions with implicit returns to clean up most of this basic math. Also removes some pretty basic add/subtract functions, along with an unused product function.
1 parent 433d817 commit acb50bc

File tree

1 file changed

+8
-27
lines changed

1 file changed

+8
-27
lines changed

src/lib/hashingFunctions.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
const _addition = (a, b) => {
2-
return a + b;
3-
};
1+
const isEven: (num: number) => boolean = num => num % 2 === 0;
42

5-
const _subtraction = (a, b) => {
6-
return a - b;
7-
};
3+
export const sum: (array: number[]) => number = arr =>
4+
arr.reduce((a, b) => a + b, 0);
85

9-
const _multiplication = (a, b) => {
10-
return a * b;
11-
};
12-
13-
export const sum = array => {
14-
return array.reduce(_addition, 0);
15-
};
16-
17-
export const sumAndDiff = array => {
18-
return array.reduce((prev, curr, index) => {
19-
if (index % 2 === 0) {
20-
return _addition(prev, curr);
21-
} else {
22-
return _subtraction(prev, curr);
23-
}
24-
}, 0);
25-
};
26-
27-
export const product = array => {
28-
return array.reduce(_multiplication, 1);
29-
};
6+
export const sumAndDiff: (array: number[]) => number = array =>
7+
array.reduce(
8+
(prev, curr, index) => (isEven(index) ? prev + curr : prev - curr),
9+
0,
10+
);

0 commit comments

Comments
 (0)