Skip to content

Commit 681572e

Browse files
♻️ refactor: Use arrow functions where possible.
Also reorder some intermediate computations to help minifier.
1 parent f77b562 commit 681572e

14 files changed

+144
-148
lines changed

src/_add.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* Dummy add template.
33
*/
44

5-
export default function _add({mul, add}) {
6-
return function (a0, a1, b0, b1) {
7-
const d = mul(a1, b1);
8-
const x = mul(a0, b1);
9-
const y = mul(b0, a1);
10-
const n = add(x, y);
5+
const _add = ({mul, add}) => (a0, a1, b0, b1) => {
6+
const x = mul(a0, b1);
7+
const y = mul(b0, a1);
8+
const n = add(x, y);
9+
const d = mul(a1, b1);
1110

12-
return [n, d];
13-
};
14-
}
11+
return [n, d];
12+
};
13+
14+
export default _add;

src/_cmp.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export default function _cmp({jz, lt0, cmp, neg, divmod}) {
2-
const compare_positive_fractions = function (a, b, c, d) {
1+
const _cmp = ({jz, lt0, cmp, neg, divmod}) => {
2+
const compare_positive_fractions = (a, b, c, d) => {
33
if (jz(b)) {
44
if (jz(d)) return 0;
55
return 1;
@@ -11,7 +11,7 @@ export default function _cmp({jz, lt0, cmp, neg, divmod}) {
1111
return cmp(q1, q2) || compare_positive_fractions(d, r2, b, r1);
1212
};
1313

14-
return function (a, b, c, d) {
14+
return (a, b, c, d) => {
1515
if (lt0(b)) {
1616
b = neg(b);
1717
a = neg(a);
@@ -30,4 +30,6 @@ export default function _cmp({jz, lt0, cmp, neg, divmod}) {
3030
if (lt0(c)) return 1;
3131
return compare_positive_fractions(a, b, c, d);
3232
};
33-
}
33+
};
34+
35+
export default _cmp;

src/_cmp_no_bounds.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
export default function _cmp_no_bounds({jz, lt0, mul, cmp}) {
2-
return function (a, b, c, d) {
3-
if (jz(b)) {
4-
if (lt0(a)) {
5-
// A/b = -Infinity
6-
return jz(d)
7-
? lt0(c)
8-
? 0 // B/d = -Infinity
9-
: -1 // B/d = Infinity
10-
: -1; // B/d is finite
11-
}
12-
13-
// A/b = Infinity
1+
const _cmp_no_bounds = ({jz, lt0, mul, cmp}) => (a, b, c, d) => {
2+
if (jz(b)) {
3+
if (lt0(a)) {
4+
// A/b = -Infinity
145
return jz(d)
156
? lt0(c)
16-
? 1 // B/d = -Infinity
17-
: 0 // B/d = Infinity
18-
: 1; // B/d is finite
7+
? 0 // B/d = -Infinity
8+
: -1 // B/d = Infinity
9+
: -1; // B/d is finite
1910
}
2011

21-
// A/b is finite
22-
if (jz(d))
23-
return lt0(c)
12+
// A/b = Infinity
13+
return jz(d)
14+
? lt0(c)
2415
? 1 // B/d = -Infinity
25-
: -1; // B/d = Infinity
16+
: 0 // B/d = Infinity
17+
: 1; // B/d is finite
18+
}
19+
20+
// A/b is finite
21+
if (jz(d))
22+
return lt0(c)
23+
? 1 // B/d = -Infinity
24+
: -1; // B/d = Infinity
25+
26+
return cmp(mul(a, d), mul(b, c));
27+
};
2628

27-
return cmp(mul(a, d), mul(b, c));
28-
};
29-
}
29+
export default _cmp_no_bounds;

src/_decimals.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for transient + repetend by multiplying x by b**M and do a single division ?
33
// That may be too much space in most cases. Though necessary when d is prime.
44

5-
export default function _decimals({eq, muln, divmod}) {
6-
return function* (b, d, n, hasrepetend, x) {
5+
const _decimals = ({eq, muln, divmod}) =>
6+
function* (b, d, n, hasrepetend, x) {
77
// Computes the length of the repetend of x/d (1 <= x < d) in base b
88
// with transient part of size n.
99

@@ -30,4 +30,5 @@ export default function _decimals({eq, muln, divmod}) {
3030
x = r;
3131
}
3232
};
33-
}
33+
34+
export default _decimals;

src/_digits.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,11 @@ import {_take} from '@aureooms/js-itertools';
22
import _decimals from './_decimals.js';
33
import _transient from './_transient.js';
44

5-
export default function _digits({
6-
jz,
7-
gt1,
8-
eq,
9-
muln,
10-
divmodn,
11-
divmod,
12-
egcd,
13-
sgn,
14-
abs,
15-
}) {
5+
const _digits = ({jz, gt1, eq, muln, divmodn, divmod, egcd, sgn, abs}) => {
166
const tr = _transient({jz, gt1, divmodn});
177
const dec = _decimals({eq, muln, divmod});
188

19-
return function (b, bfactors, x, d) {
9+
return (b, bfactors, x, d) => {
2010
const [integral, r] = divmod(abs(x), d);
2111

2212
const {u, v} = egcd(d, r);
@@ -31,4 +21,6 @@ export default function _digits({
3121

3222
return {sign: sgn(x), integral, transient, repetend};
3323
};
34-
}
24+
};
25+
26+
export default _digits;

src/_div.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
* Dummy div template.
33
*/
44

5-
export default function _div({mul}) {
6-
return function (a0, a1, b0, b1) {
7-
const d = mul(a1, b0);
8-
const n = mul(a0, b1);
5+
const _div = ({mul}) => (a0, a1, b0, b1) => {
6+
const n = mul(a0, b1);
7+
const d = mul(a1, b0);
8+
return [n, d];
9+
};
910

10-
return [n, d];
11-
};
12-
}
11+
export default _div;

src/_mul.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Dummy mul template.
33
*/
44

5-
export default function _mul({mul}) {
6-
return function (a0, a1, b0, b1) {
7-
const d = mul(a1, b1);
8-
const n = mul(a0, b0);
5+
const _mul = ({mul}) => (a0, a1, b0, b1) => {
6+
const n = mul(a0, b0);
7+
const d = mul(a1, b1);
98

10-
return [n, d];
11-
};
12-
}
9+
return [n, d];
10+
};
11+
12+
export default _mul;

src/_parse_fixed_point.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import DECIMAL_PREFIX from './constants/DECIMAL_PREFIX.js';
22
import REPETEND_PREFIX from './constants/REPETEND_PREFIX.js';
33

4-
export default function _parse_fixed_point({_chr, reg, sub}) {
5-
return function (base, s) {
6-
const [integral, decimal] = s.split(DECIMAL_PREFIX);
7-
const [transient, repetend] = decimal.split(REPETEND_PREFIX);
4+
const _parse_fixed_point = ({_chr, reg, sub}) => (base, s) => {
5+
const [integral, decimal] = s.split(DECIMAL_PREFIX);
6+
const [transient, repetend] = decimal.split(REPETEND_PREFIX);
87

9-
const _integral = integral === '0' ? '' : integral;
10-
const _repetend = repetend || '';
8+
const _integral = integral === '0' ? '' : integral;
9+
const _repetend = repetend || '';
1110

12-
const _denominator =
13-
(!_repetend
14-
? _chr(1)
15-
: new Array(repetend.length + 1).join(_chr(base - 1))) +
16-
new Array(transient.length + 1).join(_chr(0));
11+
const _denominator =
12+
(!_repetend
13+
? _chr(1)
14+
: new Array(repetend.length + 1).join(_chr(base - 1))) +
15+
new Array(transient.length + 1).join(_chr(0));
1716

18-
const _big = _integral + transient + _repetend;
19-
const _small = _integral + transient || '0';
17+
const _big = _integral + transient + _repetend;
18+
const _small = _integral + transient || '0';
2019

21-
const _bign = reg(_big, base);
22-
const _smalln = reg(_small, base);
20+
const _bign = reg(_big, base);
21+
const _smalln = reg(_small, base);
2322

24-
const numerator = _repetend ? sub(_bign, _smalln) : _smalln;
25-
const denominator = reg(_denominator, base);
23+
const numerator = _repetend ? sub(_bign, _smalln) : _smalln;
24+
const denominator = reg(_denominator, base);
2625

27-
return [numerator, denominator];
28-
};
29-
}
26+
return [numerator, denominator];
27+
};
28+
29+
export default _parse_fixed_point;

src/_parse_fraction.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import FRACTION_SEP from './constants/FRACTION_SEP.js';
22

3-
export default function _parse_fraction({reg}) {
4-
return function (base, s) {
5-
const [_numerator, _denominator] = s.split(FRACTION_SEP);
3+
const _parse_fraction = ({reg}) => (base, s) => {
4+
const [_numerator, _denominator] = s.split(FRACTION_SEP);
65

7-
return [reg(_numerator, base), reg(_denominator, base)];
8-
};
9-
}
6+
return [reg(_numerator, base), reg(_denominator, base)];
7+
};
8+
9+
export default _parse_fraction;

src/_pow.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
export default function _pow({pown}) {
2-
return function (a, b, n) {
3-
return n >= 0 ? [pown(a, n), pown(b, n)] : [pown(b, -n), pown(a, -n)];
4-
};
5-
}
1+
const _pow = ({pown}) => (a, b, n) =>
2+
n >= 0 ? [pown(a, n), pown(b, n)] : [pown(b, -n), pown(a, -n)];
3+
4+
export default _pow;

src/_simplify.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export default function _simplify({egcd, sgn, neg}) {
2-
return function (a, b) {
3-
const {u, v} = egcd(a, b);
4-
return [sgn(v) === sgn(a) ? v : neg(v), sgn(u) === sgn(b) ? u : neg(u)];
5-
};
6-
}
1+
const _simplify = ({egcd, sgn, neg}) => (a, b) => {
2+
const {u, v} = egcd(a, b);
3+
return [sgn(v) === sgn(a) ? v : neg(v), sgn(u) === sgn(b) ? u : neg(u)];
4+
};
5+
6+
export default _simplify;

src/_stringify_digits.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
export default function _stringify_digits({str}) {
2-
return function (base, {sign, integral, transient, repetend}) {
3-
const toStr = (x) => str(x, base);
1+
const _stringify_digits = ({str}) => (
2+
base,
3+
{sign, integral, transient, repetend},
4+
) => {
5+
const toStr = (x) => str(x, base);
46

5-
let repr = '';
7+
let repr = '';
68

7-
if (sign < 0) repr += '-';
9+
if (sign < 0) repr += '-';
810

9-
repr += toStr(integral);
11+
repr += toStr(integral);
1012

11-
if (transient.length > 0 || repetend.length > 0) repr += '.';
12-
// eslint-disable-next-line unicorn/no-array-callback-reference
13-
repr += transient.map(toStr).join('');
13+
if (transient.length > 0 || repetend.length > 0) repr += '.';
14+
// eslint-disable-next-line unicorn/no-array-callback-reference
15+
repr += transient.map(toStr).join('');
1416

15-
if (repetend.length > 0) repr += '|';
16-
// eslint-disable-next-line unicorn/no-array-callback-reference
17-
repr += repetend.map(toStr).join('');
17+
if (repetend.length > 0) repr += '|';
18+
// eslint-disable-next-line unicorn/no-array-callback-reference
19+
repr += repetend.map(toStr).join('');
1820

19-
return repr;
20-
};
21-
}
21+
return repr;
22+
};
23+
24+
export default _stringify_digits;

src/_sub.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* Dummy sub template.
33
*/
44

5-
export default function _sub({mul, sub}) {
6-
return function (a0, a1, b0, b1) {
7-
const d = mul(a1, b1);
8-
const x = mul(a0, b1);
9-
const y = mul(b0, a1);
10-
const n = sub(x, y);
5+
const _sub = ({mul, sub}) => (a0, a1, b0, b1) => {
6+
const x = mul(a0, b1);
7+
const y = mul(b0, a1);
8+
const n = sub(x, y);
9+
const d = mul(a1, b1);
1110

12-
return [n, d];
13-
};
14-
}
11+
return [n, d];
12+
};
13+
14+
export default _sub;

src/_transient.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
// Credits https://github.com/aureooms-research/repeating-decimal
22

3-
export default function _transient({jz, gt1, divmodn}) {
4-
return function (bfactors, d) {
5-
// Computes the length of the non repeating part in x / d
6-
// ( for any 1 <= x < d with x and d co-prime ) decimals in
7-
// base b whose prime factors are given. Returns tuple ( n , hasrepetend )
8-
// where n is the length of the non repeating part and hasrepetend
9-
// determines if 1 / d repeats or not.
3+
const _transient = ({jz, gt1, divmodn}) => (bfactors, d) => {
4+
// Computes the length of the non repeating part in x / d
5+
// ( for any 1 <= x < d with x and d co-prime ) decimals in
6+
// base b whose prime factors are given. Returns tuple ( n , hasrepetend )
7+
// where n is the length of the non repeating part and hasrepetend
8+
// determines if 1 / d repeats or not.
109

11-
let n = 0;
10+
let n = 0;
1211

13-
for (const f of bfactors) {
14-
let m = 0;
12+
for (const f of bfactors) {
13+
let m = 0;
1514

16-
while (true) {
17-
const [q, r] = divmodn(d, f);
15+
while (true) {
16+
const [q, r] = divmodn(d, f);
1817

19-
if (!jz(r)) break;
18+
if (!jz(r)) break;
2019

21-
++m;
22-
d = q;
23-
}
24-
25-
n = Math.max(n, m);
20+
++m;
21+
d = q;
2622
}
2723

28-
return [n, gt1(d)];
29-
};
30-
}
24+
n = Math.max(n, m);
25+
}
26+
27+
return [n, gt1(d)];
28+
};
29+
30+
export default _transient;

0 commit comments

Comments
 (0)