@gkucmierz/utils 1.17.0 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.mjs +9 -1
- package/package-lock.json +2 -2
- package/package.json +1 -1
- package/spec/gpn.spec.mjs +35 -0
- package/spec/memoize.spec.mjs +32 -0
- package/src/gpn.mjs +14 -0
- package/src/memoize.mjs +38 -0
package/main.mjs
CHANGED
|
@@ -20,12 +20,18 @@ import {
|
|
|
20
20
|
import {
|
|
21
21
|
getType
|
|
22
22
|
} from './src/get-type.mjs'
|
|
23
|
+
import {
|
|
24
|
+
gpn, gpnBI
|
|
25
|
+
} from './src/gpn.mjs'
|
|
23
26
|
import {
|
|
24
27
|
heronsFormula, heronsFormulaBI
|
|
25
28
|
} from './src/herons-formula.mjs'
|
|
26
29
|
import {
|
|
27
30
|
lcm, lcmBI
|
|
28
31
|
} from './src/lcm.mjs'
|
|
32
|
+
import {
|
|
33
|
+
memoize
|
|
34
|
+
} from './src/memoize.mjs'
|
|
29
35
|
import {
|
|
30
36
|
mod, modBI
|
|
31
37
|
} from './src/mod.mjs'
|
|
@@ -52,8 +58,10 @@ export * from './src/egcd.mjs';
|
|
|
52
58
|
export * from './src/factors.mjs';
|
|
53
59
|
export * from './src/gcd.mjs';
|
|
54
60
|
export * from './src/get-type.mjs';
|
|
61
|
+
export * from './src/gpn.mjs';
|
|
55
62
|
export * from './src/herons-formula.mjs';
|
|
56
63
|
export * from './src/lcm.mjs';
|
|
64
|
+
export * from './src/memoize.mjs';
|
|
57
65
|
export * from './src/mod.mjs';
|
|
58
66
|
export * from './src/phi.mjs';
|
|
59
67
|
export * from './src/pow-mod.mjs';
|
|
@@ -62,5 +70,5 @@ export * from './src/square-root.mjs';
|
|
|
62
70
|
export * from './src/tonelli-shanks.mjs';
|
|
63
71
|
|
|
64
72
|
export default [
|
|
65
|
-
SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, heronsFormula, heronsFormulaBI, lcm, lcmBI, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
|
|
73
|
+
SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, heronsFormula, heronsFormulaBI, lcm, lcmBI, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
|
|
66
74
|
];
|
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.19.0",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.19.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"husky": "^8.0.1",
|
package/package.json
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
gpn,
|
|
4
|
+
gpnBI,
|
|
5
|
+
} from '../src/gpn.mjs';
|
|
6
|
+
|
|
7
|
+
describe('gpn', () => {
|
|
8
|
+
it('first terms', () => {
|
|
9
|
+
expect(gpn(0)).toBe(0);
|
|
10
|
+
expect(gpn(1)).toBe(1);
|
|
11
|
+
expect(gpn(2)).toBe(2);
|
|
12
|
+
expect(gpn(3)).toBe(5);
|
|
13
|
+
expect(gpn(4)).toBe(7);
|
|
14
|
+
expect(gpn(5)).toBe(12);
|
|
15
|
+
expect(gpn(6)).toBe(15);
|
|
16
|
+
expect(gpn(7)).toBe(22);
|
|
17
|
+
expect(gpn(8)).toBe(26);
|
|
18
|
+
expect(gpn(9)).toBe(35);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('gpn BI', () => {
|
|
23
|
+
it('first terms', () => {
|
|
24
|
+
expect(gpnBI(0n)).toBe(0n);
|
|
25
|
+
expect(gpnBI(1n)).toBe(1n);
|
|
26
|
+
expect(gpnBI(2n)).toBe(2n);
|
|
27
|
+
expect(gpnBI(3n)).toBe(5n);
|
|
28
|
+
expect(gpnBI(4n)).toBe(7n);
|
|
29
|
+
expect(gpnBI(5n)).toBe(12n);
|
|
30
|
+
expect(gpnBI(6n)).toBe(15n);
|
|
31
|
+
expect(gpnBI(7n)).toBe(22n);
|
|
32
|
+
expect(gpnBI(8n)).toBe(26n);
|
|
33
|
+
expect(gpnBI(9n)).toBe(35n);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
memoize,
|
|
4
|
+
} from '../src/memoize.mjs';
|
|
5
|
+
|
|
6
|
+
describe('memoize', () => {
|
|
7
|
+
it('called once', () => {
|
|
8
|
+
let cnt = 0;
|
|
9
|
+
const add = memoize((a, b) => {
|
|
10
|
+
++cnt;
|
|
11
|
+
return a + b;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
expect(add(1, 2)).toBe(3);
|
|
15
|
+
expect(cnt).toBe(1);
|
|
16
|
+
expect(add(1, 2)).toBe(3);
|
|
17
|
+
expect(cnt).toBe(1);
|
|
18
|
+
|
|
19
|
+
expect(add(2, 1)).toBe(3);
|
|
20
|
+
expect(cnt).toBe(2);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('variable args length', () => {
|
|
24
|
+
const fn = memoize((...args) => args.length);
|
|
25
|
+
|
|
26
|
+
expect(fn()).toBe(0);
|
|
27
|
+
expect(fn(1)).toBe(1);
|
|
28
|
+
expect(fn(1, 2)).toBe(2);
|
|
29
|
+
expect(fn(1, 2, 3)).toBe(3);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
package/src/gpn.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
// Generalized pentagonal numbers
|
|
3
|
+
// https://oeis.org/A001318
|
|
4
|
+
|
|
5
|
+
const getGpn = (zero, one, two, three) => {
|
|
6
|
+
const gk = k => k * (three * k - one) / two;
|
|
7
|
+
return n => {
|
|
8
|
+
const m = (n + one) / two | zero;
|
|
9
|
+
return n % two === zero ? gk(-m) : gk(m);
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const gpn = getGpn(0, 1, 2, 3);
|
|
14
|
+
export const gpnBI = getGpn(0n, 1n, 2n, 3n);
|
package/src/memoize.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
|
|
2
|
+
export const memoize = fn => {
|
|
3
|
+
const maps = [];
|
|
4
|
+
|
|
5
|
+
const getLeafMap = args => {
|
|
6
|
+
if (!maps[args.length]) maps[args.length] = new Map();
|
|
7
|
+
const map = maps[args.length];
|
|
8
|
+
|
|
9
|
+
const leaf = args.reduce((map, arg) => {
|
|
10
|
+
if (map.has(arg)) return map.get(arg);
|
|
11
|
+
const tmp = new Map();
|
|
12
|
+
map.set(arg, tmp);
|
|
13
|
+
return tmp;
|
|
14
|
+
}, map);
|
|
15
|
+
|
|
16
|
+
return leaf;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const zero = {
|
|
20
|
+
called: false,
|
|
21
|
+
ret: null,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return function() {
|
|
25
|
+
const args = [...arguments];
|
|
26
|
+
if (args.length === 0) {
|
|
27
|
+
if (zero.called) return zero.ret;
|
|
28
|
+
zero.called = true;
|
|
29
|
+
return zero.ret = fn();
|
|
30
|
+
}
|
|
31
|
+
const [firstArg, ...restArgs] = args;
|
|
32
|
+
const map = getLeafMap(restArgs);
|
|
33
|
+
if (map.has(firstArg)) return map.get(firstArg);
|
|
34
|
+
const ret = fn(...args);
|
|
35
|
+
map.set(firstArg, ret);
|
|
36
|
+
return ret;
|
|
37
|
+
};
|
|
38
|
+
};
|