data-structure-typed 2.0.2 → 2.0.4

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/package.json CHANGED
@@ -1,12 +1,19 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Standard data structure",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
7
7
  "browser": "dist/umd/data-structure-typed.min.js",
8
8
  "types": "dist/esm/index.d.ts",
9
9
  "umd:main": "dist/umd/data-structure-typed.min.js",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/esm/index.js",
13
+ "require": "./dist/cjs/index.js",
14
+ "types": "./dist/esm/index.d.ts"
15
+ }
16
+ },
10
17
  "scripts": {
11
18
  "build": "npm run build:ecu && npm run build:docs-class",
12
19
  "build:esm": "rm -rf dist/esm && tsc -p tsconfig-esm.json",
@@ -20,7 +20,7 @@ import type {
20
20
  NodeDisplayLayout,
21
21
  NodePredicate,
22
22
  OptNodeOrNull,
23
- RBTNColor, Thunk,
23
+ RBTNColor,
24
24
  ToEntryFn
25
25
  } from '../../types';
26
26
  import { IBinaryTree } from '../../interfaces';
@@ -1304,12 +1304,12 @@ export class BinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = o
1304
1304
  return callback(dfs(startNode));
1305
1305
  } else {
1306
1306
  // Indirect implementation of iteration using tail recursion optimization
1307
- const dfs = (cur: BinaryTreeNode<K, V>): BinaryTreeNode<K, V> | Thunk<BinaryTreeNode<K, V>> => {
1307
+ const dfs = trampoline((cur: BinaryTreeNode<K, V>): BinaryTreeNode<K, V> => {
1308
1308
  if (!this.isRealNode(cur.left)) return cur;
1309
- return () => dfs(cur.left!);
1310
- };
1309
+ return dfs.cont(cur.left);
1310
+ });
1311
1311
 
1312
- return callback(trampoline(() => dfs(startNode)));
1312
+ return callback(dfs(startNode));
1313
1313
  }
1314
1314
  }
1315
1315
 
@@ -1352,12 +1352,13 @@ export class BinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = o
1352
1352
 
1353
1353
  return callback(dfs(startNode));
1354
1354
  } else {
1355
- const dfs = (cur: BinaryTreeNode<K, V>) => {
1355
+ // Indirect implementation of iteration using tail recursion optimization
1356
+ const dfs = trampoline((cur: BinaryTreeNode<K, V>) => {
1356
1357
  if (!this.isRealNode(cur.right)) return cur;
1357
- return () => dfs(cur.right!) as Thunk<BinaryTreeNode<K, V>>;
1358
- };
1358
+ return dfs.cont(cur.right);
1359
+ });
1359
1360
 
1360
- return callback(trampoline(() => dfs(startNode)));
1361
+ return callback(dfs(startNode));
1361
1362
  }
1362
1363
  }
1363
1364
 
@@ -24,7 +24,7 @@ import { LinearBase } from '../base/linear-base';
24
24
  * let maxSum = 0;
25
25
  * let currentSum = 0;
26
26
  *
27
- * nums.forEach((num, i) => {
27
+ * nums.forEach((num) => {
28
28
  * queue.push(num);
29
29
  * currentSum += num;
30
30
  *
@@ -1,4 +1,9 @@
1
- export type Thunk<T> = () => T | Thunk<T>;
1
+ export type ToThunkFn<R = any> = () => R;
2
+ export type Thunk<R = any> = ToThunkFn<R> & { __THUNK__?: symbol };
3
+ export type TrlFn<A extends any[] = any[], R = any> = (...args: A) => R;
4
+ export type TrlAsyncFn = (...args: any[]) => any;
5
+
6
+ export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
2
7
 
3
8
  export type Any = string | number | bigint | boolean | symbol | undefined | object;
4
9
 
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { Comparable, ComparablePrimitive, Thunk } from '../types';
8
+ import type { Comparable, ComparablePrimitive, Thunk, ToThunkFn, TrlAsyncFn, TrlFn } from '../types';
9
9
 
10
10
  /**
11
11
  * The function generates a random UUID (Universally Unique Identifier) in TypeScript.
@@ -47,18 +47,90 @@ export const arrayRemove = function <T>(array: T[], predicate: (item: T, index:
47
47
  return result;
48
48
  };
49
49
 
50
+ export const THUNK_SYMBOL = Symbol('thunk');
50
51
 
51
- export function isThunk<T>(result: T | Thunk<T>): result is Thunk<T> {
52
- return typeof result === 'function';
53
- }
52
+ /**
53
+ * The function `isThunk` checks if a given value is a function with a specific symbol property.
54
+ * @param {any} fnOrValue - The `fnOrValue` parameter in the `isThunk` function can be either a
55
+ * function or a value that you want to check if it is a thunk. Thunks are functions that are wrapped
56
+ * around a value or computation for lazy evaluation. The function checks if the `fnOrValue` is
57
+ * @returns The function `isThunk` is checking if the input `fnOrValue` is a function and if it has a
58
+ * property `__THUNK__` equal to `THUNK_SYMBOL`. The return value will be `true` if both conditions are
59
+ * met, otherwise it will be `false`.
60
+ */
61
+ export const isThunk = (fnOrValue: any) => {
62
+ return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL;
63
+ };
54
64
 
55
- export function trampoline<T>(thunk: Thunk<T>): T {
56
- let result: T | Thunk<T> = thunk;
57
- while (isThunk(result)) {
58
- result = result();
59
- }
60
- return result;
61
- }
65
+ /**
66
+ * The `toThunk` function in TypeScript converts a function into a thunk by wrapping it in a closure.
67
+ * @param {ToThunkFn} fn - `fn` is a function that will be converted into a thunk.
68
+ * @returns A thunk function is being returned. Thunk functions are functions that delay the evaluation
69
+ * of an expression or operation until it is explicitly called or invoked. In this case, the `toThunk`
70
+ * function takes a function `fn` as an argument and returns a thunk function that, when called, will
71
+ * execute the `fn` function provided as an argument.
72
+ */
73
+ export const toThunk = (fn: ToThunkFn): Thunk => {
74
+ const thunk = () => fn();
75
+ thunk.__THUNK__ = THUNK_SYMBOL;
76
+ return thunk;
77
+ };
78
+
79
+ /**
80
+ * The `trampoline` function in TypeScript enables tail call optimization by using thunks to avoid
81
+ * stack overflow.
82
+ * @param {TrlFn} fn - The `fn` parameter in the `trampoline` function is a function that takes any
83
+ * number of arguments and returns a value.
84
+ * @returns The `trampoline` function returns an object with two properties:
85
+ * 1. A function that executes the provided function `fn` and continues to execute any thunks returned
86
+ * by `fn` until a non-thunk value is returned.
87
+ * 2. A `cont` property that is a function which creates a thunk for the provided function `fn`.
88
+ */
89
+ export const trampoline = (fn: TrlFn) => {
90
+ const cont = (...args: [...Parameters<TrlFn>]): ReturnType<TrlFn> => toThunk(() => fn(...args));
91
+
92
+ return Object.assign(
93
+ (...args: [...Parameters<TrlFn>]) => {
94
+ let result = fn(...args);
95
+
96
+ while (isThunk(result) && typeof result === 'function') {
97
+ result = result();
98
+ }
99
+
100
+ return result;
101
+ },
102
+ { cont }
103
+ );
104
+ };
105
+
106
+ /**
107
+ * The `trampolineAsync` function in TypeScript allows for asynchronous trampolining of a given
108
+ * function.
109
+ * @param {TrlAsyncFn} fn - The `fn` parameter in the `trampolineAsync` function is expected to be a
110
+ * function that returns a Promise. This function will be called recursively until a non-thunk value is
111
+ * returned.
112
+ * @returns The `trampolineAsync` function returns an object with two properties:
113
+ * 1. An async function that executes the provided `TrlAsyncFn` function and continues to execute any
114
+ * thunks returned by the function until a non-thunk value is returned.
115
+ * 2. A `cont` property that is a function which wraps the provided `TrlAsyncFn` function in a thunk
116
+ * and returns it.
117
+ */
118
+ export const trampolineAsync = (fn: TrlAsyncFn) => {
119
+ const cont = (...args: [...Parameters<TrlAsyncFn>]): ReturnType<TrlAsyncFn> => toThunk(() => fn(...args));
120
+
121
+ return Object.assign(
122
+ async (...args: [...Parameters<TrlAsyncFn>]) => {
123
+ let result = await fn(...args);
124
+
125
+ while (isThunk(result) && typeof result === 'function') {
126
+ result = await result();
127
+ }
128
+
129
+ return result;
130
+ },
131
+ { cont }
132
+ );
133
+ };
62
134
 
63
135
  /**
64
136
  * The function `getMSB` returns the most significant bit of a given number.
@@ -1,4 +1,4 @@
1
- import { isComparable, trampoline } from '../../../src';
1
+ import { isComparable } from '../../../src';
2
2
 
3
3
  describe('isNaN', () => {
4
4
  it('should isNaN', function () {
@@ -173,35 +173,3 @@ describe('isComparable', () => {
173
173
  });
174
174
  });
175
175
 
176
- describe('Factorial Performance Tests', () => {
177
- const depth = 5000;
178
- let arr: number[];
179
-
180
- const recurseTrampoline = (n: number, arr: number[], acc = 1): (() => any) | number => {
181
- if (n === 0) return acc;
182
- arr.unshift(1);
183
- return () => recurseTrampoline(n - 1, arr, acc);
184
- };
185
-
186
- const recurse = (n: number, arr: number[], acc = 1): number => {
187
- if (n === 0) return acc;
188
- arr.unshift(1);
189
- return recurse(n - 1, arr, acc);
190
- };
191
-
192
- beforeEach(() => {
193
- arr = new Array(depth).fill(0);
194
- });
195
-
196
- it('should calculate recursive function using trampoline without stack overflow', () => {
197
- const result = trampoline(() => recurseTrampoline(depth, arr));
198
- expect(result).toBe(1);
199
- expect(arr.length).toBe(depth + depth);
200
- });
201
-
202
- it('should calculate recursive directly and possibly stack overflow', () => {
203
- const result = recurse(depth, arr);
204
- expect(result).toBe(1);
205
- expect(arr.length).toBe(depth + depth);
206
- });
207
- });