avl-tree-typed 2.0.5 → 2.1.1

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.
Files changed (104) hide show
  1. package/dist/common/index.js +1 -1
  2. package/dist/constants/index.js +1 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  4. package/dist/data-structures/base/iterable-element-base.js +149 -107
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  6. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  7. package/dist/data-structures/base/linear-base.d.ts +250 -192
  8. package/dist/data-structures/base/linear-base.js +137 -274
  9. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  10. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  12. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  13. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  14. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  15. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  16. package/dist/data-structures/binary-tree/binary-tree.js +602 -873
  17. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  18. package/dist/data-structures/binary-tree/bst.js +505 -481
  19. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  20. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  21. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  22. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  23. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  24. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  25. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  26. package/dist/data-structures/graph/abstract-graph.js +267 -237
  27. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  28. package/dist/data-structures/graph/directed-graph.js +146 -233
  29. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  30. package/dist/data-structures/graph/map-graph.js +56 -59
  31. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  32. package/dist/data-structures/graph/undirected-graph.js +129 -149
  33. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  34. package/dist/data-structures/hash/hash-map.js +270 -457
  35. package/dist/data-structures/heap/heap.d.ts +214 -289
  36. package/dist/data-structures/heap/heap.js +340 -349
  37. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  38. package/dist/data-structures/heap/max-heap.js +11 -66
  39. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  40. package/dist/data-structures/heap/min-heap.js +11 -66
  41. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  42. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  43. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  44. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  45. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  46. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  47. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  48. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  49. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  50. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  51. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  52. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  53. package/dist/data-structures/queue/deque.d.ts +227 -254
  54. package/dist/data-structures/queue/deque.js +309 -348
  55. package/dist/data-structures/queue/queue.d.ts +180 -201
  56. package/dist/data-structures/queue/queue.js +265 -248
  57. package/dist/data-structures/stack/stack.d.ts +124 -102
  58. package/dist/data-structures/stack/stack.js +181 -125
  59. package/dist/data-structures/trie/trie.d.ts +164 -165
  60. package/dist/data-structures/trie/trie.js +189 -172
  61. package/dist/interfaces/binary-tree.d.ts +56 -6
  62. package/dist/interfaces/graph.d.ts +16 -0
  63. package/dist/types/data-structures/base/base.d.ts +1 -1
  64. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  65. package/dist/types/utils/utils.d.ts +1 -0
  66. package/dist/utils/number.js +1 -2
  67. package/dist/utils/utils.d.ts +1 -1
  68. package/dist/utils/utils.js +9 -8
  69. package/package.json +15 -15
  70. package/src/data-structures/base/iterable-element-base.ts +238 -115
  71. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  72. package/src/data-structures/base/linear-base.ts +271 -277
  73. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  74. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  75. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  76. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  77. package/src/data-structures/binary-tree/bst.ts +565 -572
  78. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  79. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  80. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  81. package/src/data-structures/graph/abstract-graph.ts +339 -264
  82. package/src/data-structures/graph/directed-graph.ts +146 -236
  83. package/src/data-structures/graph/map-graph.ts +63 -60
  84. package/src/data-structures/graph/undirected-graph.ts +129 -152
  85. package/src/data-structures/hash/hash-map.ts +274 -496
  86. package/src/data-structures/heap/heap.ts +389 -402
  87. package/src/data-structures/heap/max-heap.ts +12 -76
  88. package/src/data-structures/heap/min-heap.ts +13 -76
  89. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  90. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  91. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  92. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  93. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  94. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  95. package/src/data-structures/queue/deque.ts +381 -357
  96. package/src/data-structures/queue/queue.ts +310 -264
  97. package/src/data-structures/stack/stack.ts +217 -131
  98. package/src/data-structures/trie/trie.ts +240 -175
  99. package/src/interfaces/binary-tree.ts +240 -6
  100. package/src/interfaces/graph.ts +37 -0
  101. package/src/types/data-structures/base/base.ts +5 -5
  102. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  103. package/src/types/utils/utils.ts +2 -0
  104. package/src/utils/utils.ts +9 -14
@@ -1,16 +1,250 @@
1
1
  import { BinaryTreeNode } from '../data-structures';
2
- import type { BinaryTreeDeleteResult, BinaryTreeOptions, BTNRep, NodePredicate } from '../types';
2
+ import type {
3
+ BinaryTreeDeleteResult,
4
+ BinaryTreeOptions,
5
+ BTNRep,
6
+ DFSOrderPattern,
7
+ EntryCallback,
8
+ IterationType,
9
+ NodeCallback,
10
+ NodePredicate,
11
+ OptNodeOrNull,
12
+ ReduceEntryCallback,
13
+ ToEntryFn
14
+ } from '../types';
3
15
 
4
- export interface IBinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> {
5
- createNode(key: K, value?: BinaryTreeNode['value']): BinaryTreeNode;
16
+ /**
17
+ * Public, implementation-agnostic binary tree API.
18
+ * K = key, V = value, R = raw/record used with toEntryFn (optional).
19
+ * Transforming methods like `map` use method-level generics MK/MV/MR.
20
+ */
21
+ export interface IBinaryTree<K = any, V = any, R = any> {
22
+ // ---- state ----
23
+ readonly size: number;
24
+ readonly root: BinaryTreeNode<K, V> | null | undefined;
25
+ readonly isMapMode: boolean;
26
+ // NOTE: iterationType is mutable on the class; remove readonly here to match
27
+ iterationType: IterationType;
28
+ // Extra public state/getters implemented by BinaryTree (useful to callers)
29
+ readonly NIL: BinaryTreeNode<K, V>;
30
+ readonly store: Map<K, V | undefined>;
31
+ readonly toEntryFn?: ToEntryFn<K, V, R>;
32
+ readonly isDuplicate: boolean;
6
33
 
7
- createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R, MK, MV, MR>;
34
+ // ---- construction / mutation ----
35
+ _createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
36
+
37
+ createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R>;
8
38
 
9
39
  add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
10
40
 
11
- addMany(nodes: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>>>, values?: Iterable<V | undefined>): boolean[];
41
+ // Accept raw R as well (when toEntryFn is configured)
42
+ addMany(
43
+ keysNodesEntriesOrRaws: Iterable<
44
+ K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
45
+ >,
46
+ values?: Iterable<V | undefined>
47
+ ): boolean[];
12
48
 
49
+ // Accept BTNRep, predicate, or raw R for deletion
13
50
  delete(
14
- predicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>
51
+ keyNodeEntryRawOrPredicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>
15
52
  ): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
53
+
54
+ clear(): void;
55
+
56
+ isEmpty(): boolean;
57
+
58
+ // ---- query / read ----
59
+
60
+ // Widen `get` to support entry and optional traversal context (matches impl)
61
+ get(
62
+ keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
63
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
64
+ iterationType?: IterationType
65
+ ): V | undefined;
66
+
67
+ // `has` also supports node/entry/predicate and optional traversal context
68
+ has(
69
+ keyNodeEntryOrPredicate?:
70
+ | K
71
+ | BinaryTreeNode<K, V>
72
+ | [K | null | undefined, V | undefined]
73
+ | null
74
+ | undefined
75
+ | NodePredicate<BinaryTreeNode<K, V> | null>,
76
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
77
+ iterationType?: IterationType
78
+ ): boolean;
79
+
80
+ hasValue(value: V): boolean;
81
+
82
+ find(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): [K, V | undefined] | undefined;
83
+
84
+ // ---- iteration ----
85
+ [Symbol.iterator](): IterableIterator<[K, V | undefined]>;
86
+
87
+ entries(): IterableIterator<[K, V | undefined]>;
88
+
89
+ keys(): IterableIterator<K>;
90
+
91
+ values(): IterableIterator<V | undefined>;
92
+
93
+ forEach(callbackfn: EntryCallback<K, V | undefined, void>, thisArg?: unknown): void;
94
+
95
+ every(callbackfn: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): boolean;
96
+
97
+ some(callbackfn: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): boolean;
98
+
99
+ reduce<U>(reducer: ReduceEntryCallback<K, V | undefined, U>, initialValue: U): U;
100
+
101
+ // ---- node access / extremes ----
102
+ getNode(
103
+ keyNodeEntryOrPredicate:
104
+ | K
105
+ | BinaryTreeNode<K, V>
106
+ | [K | null | undefined, V | undefined]
107
+ | null
108
+ | undefined
109
+ | NodePredicate<BinaryTreeNode<K, V> | null>,
110
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
111
+ iterationType?: IterationType
112
+ ): OptNodeOrNull<BinaryTreeNode<K, V>>;
113
+
114
+ getLeftMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(
115
+ callback?: C,
116
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
117
+ iterationType?: IterationType
118
+ ): ReturnType<C>;
119
+
120
+ getRightMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(
121
+ callback?: C,
122
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
123
+ iterationType?: IterationType
124
+ ): ReturnType<C>;
125
+
126
+ // ---- traversal ----
127
+ dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(
128
+ callback?: C,
129
+ pattern?: DFSOrderPattern,
130
+ onlyOne?: boolean,
131
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
132
+ iterationType?: IterationType
133
+ ): ReturnType<C>[];
134
+
135
+ dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
136
+ callback?: C,
137
+ pattern?: DFSOrderPattern,
138
+ onlyOne?: boolean,
139
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
140
+ iterationType?: IterationType,
141
+ includeNull?: boolean
142
+ ): ReturnType<C>[];
143
+
144
+ bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(
145
+ callback?: C,
146
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
147
+ iterationType?: IterationType,
148
+ includeNull?: false
149
+ ): ReturnType<C>[];
150
+
151
+ bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
152
+ callback?: C,
153
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
154
+ iterationType?: IterationType,
155
+ includeNull?: true
156
+ ): ReturnType<C>[];
157
+
158
+ morris<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
159
+ callback?: C,
160
+ pattern?: DFSOrderPattern,
161
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
162
+ ): ReturnType<C>[];
163
+
164
+ leaves<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
165
+ callback?: C,
166
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
167
+ iterationType?: IterationType
168
+ ): ReturnType<C>[];
169
+
170
+ listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(
171
+ callback?: C,
172
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
173
+ iterationType?: IterationType,
174
+ includeNull?: false
175
+ ): ReturnType<C>[][];
176
+
177
+ listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
178
+ callback?: C,
179
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
180
+ iterationType?: IterationType,
181
+ includeNull?: true
182
+ ): ReturnType<C>[][];
183
+
184
+ getPathToRoot<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(
185
+ beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
186
+ callback?: C,
187
+ isReverse?: boolean
188
+ ): ReturnType<C>[];
189
+
190
+ // ---- metrics & validation ----
191
+ getDepth(
192
+ dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
193
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
194
+ ): number;
195
+
196
+ getHeight(
197
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
198
+ iterationType?: IterationType
199
+ ): number;
200
+
201
+ getMinHeight(
202
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
203
+ iterationType?: IterationType
204
+ ): number;
205
+
206
+ isPerfectlyBalanced(
207
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
208
+ ): boolean;
209
+
210
+ isBST(
211
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
212
+ iterationType?: IterationType
213
+ ): boolean;
214
+
215
+ // ---- search helpers ----
216
+ search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
217
+ keyNodeEntryOrPredicate:
218
+ | K
219
+ | BinaryTreeNode<K, V>
220
+ | [K | null | undefined, V | undefined]
221
+ | null
222
+ | undefined
223
+ | NodePredicate<BinaryTreeNode<K, V> | null>,
224
+ onlyOne?: boolean,
225
+ callback?: C,
226
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
227
+ iterationType?: IterationType
228
+ ): ReturnType<C>[];
229
+
230
+ // ---- immutable transforms ----
231
+ clone(): this;
232
+
233
+ filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
234
+
235
+ map<MK = K, MV = V, MR = any>(
236
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
237
+ options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
238
+ thisArg?: unknown
239
+ ): IBinaryTree<MK, MV, MR>;
240
+
241
+ // ---- bulk / interop ----
242
+ merge(anotherTree: IBinaryTree<K, V, R>): void;
243
+
244
+ refill(
245
+ keysNodesEntriesOrRaws: Iterable<
246
+ K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
247
+ >,
248
+ values?: Iterable<V | undefined>
249
+ ): void;
16
250
  }
@@ -1,7 +1,44 @@
1
1
  import { VertexKey } from '../types';
2
2
 
3
3
  export interface IGraph<V, E, VO, EO> {
4
+ // Vertex factories
4
5
  createVertex(key: VertexKey, value?: V): VO;
5
6
 
7
+ // Edge factories
6
8
  createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
9
+
10
+ // Core vertex ops
11
+ getVertex(vertexKey: VertexKey): VO | undefined;
12
+
13
+ hasVertex(vertexOrKey: VO | VertexKey): boolean;
14
+
15
+ addVertex(vertex: VO): boolean;
16
+
17
+ addVertex(key: VertexKey, value?: V): boolean;
18
+
19
+ deleteVertex(vertexOrKey: VO | VertexKey): boolean;
20
+
21
+ // Core edge ops
22
+ deleteEdge(edge: EO): EO | undefined;
23
+
24
+ getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
25
+
26
+ degreeOf(vertexOrKey: VO | VertexKey): number;
27
+
28
+ edgeSet(): EO[];
29
+
30
+ edgesOf(vertexOrKey: VO | VertexKey): EO[];
31
+
32
+ getNeighbors(vertexOrKey: VO | VertexKey): VO[];
33
+
34
+ getEndsOfEdge(edge: EO): [VO, VO] | undefined;
35
+
36
+ // Container-like ops
37
+ isEmpty(): boolean;
38
+
39
+ clear(): void;
40
+
41
+ clone(): this;
42
+
43
+ filter(...args: any[]): this;
7
44
  }
@@ -11,12 +11,12 @@ export type ReduceEntryCallback<K, V, R> = (
11
11
  original: IterableEntryBase<K, V>
12
12
  ) => R;
13
13
 
14
- export type ReduceElementCallback<E, R, RT = E> = (
15
- accumulator: RT,
16
- element: E,
14
+ export type ReduceElementCallback<E, R, U = E> = (
15
+ accumulator: U,
16
+ value: E,
17
17
  index: number,
18
- original: IterableElementBase<E, R>
19
- ) => RT;
18
+ self: IterableElementBase<E, R>
19
+ ) => U;
20
20
 
21
21
  export type ReduceLinearCallback<E, RT = E> = (
22
22
  accumulator: RT,
@@ -11,3 +11,8 @@ export type DijkstraResult<V> =
11
11
  minPath: V[];
12
12
  }
13
13
  | undefined;
14
+
15
+ export type GraphOptions<V = any> = {
16
+ vertexValueInitializer?: (key: VertexKey) => V;
17
+ defaultEdgeWeight?: number;
18
+ };
@@ -4,6 +4,8 @@ export type Any = string | number | bigint | boolean | symbol | undefined | obje
4
4
 
5
5
  export type Arithmetic = number | bigint;
6
6
 
7
+ export type ElemOf<T> = T extends (infer U)[] ? U : never;
8
+
7
9
  export type ComparablePrimitive = number | bigint | string | boolean;
8
10
 
9
11
  export interface BaseComparableObject {
@@ -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, TrampolineThunk, Trampoline } from '../types';
8
+ import type { Comparable, ComparablePrimitive, Trampoline, TrampolineThunk } from '../types';
9
9
 
10
10
  /**
11
11
  * The function generates a random UUID (Universally Unique Identifier) in TypeScript.
@@ -209,9 +209,7 @@ export function isComparable(value: unknown, isForceObjectComparable = false): v
209
209
  * @param computation - A function that, when executed, returns the next trampoline step.
210
210
  * @returns A TrampolineThunk object containing the deferred computation.
211
211
  */
212
- export const makeTrampolineThunk = <T>(
213
- computation: () => Trampoline<T>
214
- ): TrampolineThunk<T> => ({
212
+ export const makeTrampolineThunk = <T>(computation: () => Trampoline<T>): TrampolineThunk<T> => ({
215
213
  isThunk: true, // Marker indicating this is a thunk
216
214
  fn: computation // The deferred computation function
217
215
  });
@@ -226,13 +224,11 @@ export const makeTrampolineThunk = <T>(
226
224
  * @param value - The value to test.
227
225
  * @returns True if the value is a valid TrampolineThunk, false otherwise.
228
226
  */
229
- export const isTrampolineThunk = <T>(
230
- value: Trampoline<T>
231
- ): value is TrampolineThunk<T> =>
227
+ export const isTrampolineThunk = <T>(value: Trampoline<T>): value is TrampolineThunk<T> =>
232
228
  typeof value === 'object' && // Must be an object
233
- value !== null && // Must not be null
234
- 'isThunk' in value && // Must have the 'isThunk' property
235
- value.isThunk; // The flag must be true
229
+ value !== null && // Must not be null
230
+ 'isThunk' in value && // Must have the 'isThunk' property
231
+ value.isThunk; // The flag must be true
236
232
 
237
233
  /**
238
234
  * Executes a trampoline computation until a final (non-thunk) result is obtained.
@@ -247,7 +243,8 @@ export const isTrampolineThunk = <T>(
247
243
  */
248
244
  export function trampoline<T>(initial: Trampoline<T>): T {
249
245
  let current = initial; // Start with the initial trampoline value
250
- while (isTrampolineThunk(current)) { // Keep unwrapping while we have thunks
246
+ while (isTrampolineThunk(current)) {
247
+ // Keep unwrapping while we have thunks
251
248
  current = current.fn(); // Execute the deferred function to get the next step
252
249
  }
253
250
  return current; // Once no thunks remain, return the final result
@@ -302,9 +299,7 @@ export function makeTrampoline<Args extends any[], Result>(
302
299
  * @param initial - The initial Trampoline or Promise of Trampoline to start execution from.
303
300
  * @returns A Promise that resolves to the final result (a non-thunk value).
304
301
  */
305
- export async function asyncTrampoline<T>(
306
- initial: Trampoline<T> | Promise<Trampoline<T>>
307
- ): Promise<T> {
302
+ export async function asyncTrampoline<T>(initial: Trampoline<T> | Promise<Trampoline<T>>): Promise<T> {
308
303
  let current = await initial; // Wait for the initial step to resolve if it's a Promise
309
304
 
310
305
  // Keep executing thunks until we reach a non-thunk (final) value