@nlozgachev/pipelined 0.12.0 → 0.14.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.
Files changed (213) hide show
  1. package/LICENCE +28 -0
  2. package/README.md +1 -1
  3. package/{types/src/Types/NonEmptyList.d.ts → dist/NonEmptyList-BlGFjor5.d.mts} +4 -3
  4. package/dist/NonEmptyList-BlGFjor5.d.ts +30 -0
  5. package/dist/Task-Bd3gXPRQ.d.mts +677 -0
  6. package/dist/Task-BjAkkD6t.d.ts +677 -0
  7. package/dist/chunk-4TXC322E.mjs +136 -0
  8. package/dist/chunk-BYWKZLHM.mjs +10 -0
  9. package/dist/chunk-DBIC62UV.mjs +6 -0
  10. package/dist/chunk-FAZN3IWZ.mjs +554 -0
  11. package/dist/chunk-QPTGO5AS.mjs +150 -0
  12. package/dist/chunk-UV2HMF2A.mjs +514 -0
  13. package/dist/composition.d.mts +495 -0
  14. package/dist/composition.d.ts +495 -0
  15. package/dist/composition.js +188 -0
  16. package/dist/composition.mjs +58 -0
  17. package/dist/core.d.mts +2170 -0
  18. package/dist/core.d.ts +2170 -0
  19. package/dist/core.js +698 -0
  20. package/dist/core.mjs +42 -0
  21. package/dist/index.d.mts +6 -0
  22. package/dist/index.d.ts +6 -0
  23. package/dist/index.js +1421 -0
  24. package/dist/index.mjs +120 -0
  25. package/dist/types.d.mts +54 -0
  26. package/{types/src/Types/Brand.d.ts → dist/types.d.ts} +6 -4
  27. package/dist/types.js +41 -0
  28. package/dist/types.mjs +10 -0
  29. package/dist/utils.d.mts +1285 -0
  30. package/dist/utils.d.ts +1285 -0
  31. package/dist/utils.js +722 -0
  32. package/dist/utils.mjs +18 -0
  33. package/package.json +64 -69
  34. package/esm/mod.js +0 -3
  35. package/esm/package.json +0 -3
  36. package/esm/src/Composition/compose.js +0 -3
  37. package/esm/src/Composition/converge.js +0 -3
  38. package/esm/src/Composition/curry.js +0 -42
  39. package/esm/src/Composition/flip.js +0 -20
  40. package/esm/src/Composition/flow.js +0 -8
  41. package/esm/src/Composition/fn.js +0 -85
  42. package/esm/src/Composition/index.js +0 -13
  43. package/esm/src/Composition/juxt.js +0 -3
  44. package/esm/src/Composition/memoize.js +0 -66
  45. package/esm/src/Composition/not.js +0 -25
  46. package/esm/src/Composition/on.js +0 -12
  47. package/esm/src/Composition/pipe.js +0 -3
  48. package/esm/src/Composition/tap.js +0 -33
  49. package/esm/src/Composition/uncurry.js +0 -32
  50. package/esm/src/Core/Deferred.js +0 -30
  51. package/esm/src/Core/InternalTypes.js +0 -1
  52. package/esm/src/Core/Lens.js +0 -98
  53. package/esm/src/Core/Logged.js +0 -111
  54. package/esm/src/Core/Option.js +0 -191
  55. package/esm/src/Core/Optional.js +0 -160
  56. package/esm/src/Core/Predicate.js +0 -133
  57. package/esm/src/Core/Reader.js +0 -134
  58. package/esm/src/Core/Refinement.js +0 -115
  59. package/esm/src/Core/RemoteData.js +0 -211
  60. package/esm/src/Core/Result.js +0 -170
  61. package/esm/src/Core/State.js +0 -181
  62. package/esm/src/Core/Task.js +0 -223
  63. package/esm/src/Core/TaskOption.js +0 -106
  64. package/esm/src/Core/TaskResult.js +0 -127
  65. package/esm/src/Core/TaskValidation.js +0 -128
  66. package/esm/src/Core/These.js +0 -245
  67. package/esm/src/Core/Tuple.js +0 -112
  68. package/esm/src/Core/Validation.js +0 -212
  69. package/esm/src/Core/index.js +0 -18
  70. package/esm/src/Types/Brand.js +0 -28
  71. package/esm/src/Types/NonEmptyList.js +0 -14
  72. package/esm/src/Types/index.js +0 -2
  73. package/esm/src/Utils/Arr.js +0 -570
  74. package/esm/src/Utils/Dict.js +0 -421
  75. package/esm/src/Utils/Num.js +0 -124
  76. package/esm/src/Utils/Rec.js +0 -241
  77. package/esm/src/Utils/Str.js +0 -134
  78. package/esm/src/Utils/Uniq.js +0 -265
  79. package/esm/src/Utils/index.js +0 -6
  80. package/script/mod.js +0 -19
  81. package/script/package.json +0 -3
  82. package/script/src/Composition/compose.js +0 -6
  83. package/script/src/Composition/converge.js +0 -6
  84. package/script/src/Composition/curry.js +0 -48
  85. package/script/src/Composition/flip.js +0 -24
  86. package/script/src/Composition/flow.js +0 -11
  87. package/script/src/Composition/fn.js +0 -98
  88. package/script/src/Composition/index.js +0 -29
  89. package/script/src/Composition/juxt.js +0 -6
  90. package/script/src/Composition/memoize.js +0 -71
  91. package/script/src/Composition/not.js +0 -29
  92. package/script/src/Composition/on.js +0 -16
  93. package/script/src/Composition/pipe.js +0 -6
  94. package/script/src/Composition/tap.js +0 -37
  95. package/script/src/Composition/uncurry.js +0 -38
  96. package/script/src/Core/Deferred.js +0 -33
  97. package/script/src/Core/InternalTypes.js +0 -2
  98. package/script/src/Core/Lens.js +0 -101
  99. package/script/src/Core/Logged.js +0 -114
  100. package/script/src/Core/Option.js +0 -194
  101. package/script/src/Core/Optional.js +0 -163
  102. package/script/src/Core/Predicate.js +0 -136
  103. package/script/src/Core/Reader.js +0 -137
  104. package/script/src/Core/Refinement.js +0 -118
  105. package/script/src/Core/RemoteData.js +0 -214
  106. package/script/src/Core/Result.js +0 -173
  107. package/script/src/Core/State.js +0 -184
  108. package/script/src/Core/Task.js +0 -226
  109. package/script/src/Core/TaskOption.js +0 -109
  110. package/script/src/Core/TaskResult.js +0 -130
  111. package/script/src/Core/TaskValidation.js +0 -131
  112. package/script/src/Core/These.js +0 -248
  113. package/script/src/Core/Tuple.js +0 -115
  114. package/script/src/Core/Validation.js +0 -215
  115. package/script/src/Core/index.js +0 -34
  116. package/script/src/Types/Brand.js +0 -31
  117. package/script/src/Types/NonEmptyList.js +0 -18
  118. package/script/src/Types/index.js +0 -18
  119. package/script/src/Utils/Arr.js +0 -573
  120. package/script/src/Utils/Dict.js +0 -424
  121. package/script/src/Utils/Num.js +0 -127
  122. package/script/src/Utils/Rec.js +0 -244
  123. package/script/src/Utils/Str.js +0 -137
  124. package/script/src/Utils/Uniq.js +0 -268
  125. package/script/src/Utils/index.js +0 -22
  126. package/types/mod.d.ts +0 -4
  127. package/types/mod.d.ts.map +0 -1
  128. package/types/src/Composition/compose.d.ts +0 -33
  129. package/types/src/Composition/compose.d.ts.map +0 -1
  130. package/types/src/Composition/converge.d.ts +0 -21
  131. package/types/src/Composition/converge.d.ts.map +0 -1
  132. package/types/src/Composition/curry.d.ts +0 -43
  133. package/types/src/Composition/curry.d.ts.map +0 -1
  134. package/types/src/Composition/flip.d.ts +0 -21
  135. package/types/src/Composition/flip.d.ts.map +0 -1
  136. package/types/src/Composition/flow.d.ts +0 -56
  137. package/types/src/Composition/flow.d.ts.map +0 -1
  138. package/types/src/Composition/fn.d.ts +0 -76
  139. package/types/src/Composition/fn.d.ts.map +0 -1
  140. package/types/src/Composition/index.d.ts +0 -14
  141. package/types/src/Composition/index.d.ts.map +0 -1
  142. package/types/src/Composition/juxt.d.ts +0 -18
  143. package/types/src/Composition/juxt.d.ts.map +0 -1
  144. package/types/src/Composition/memoize.d.ts +0 -46
  145. package/types/src/Composition/memoize.d.ts.map +0 -1
  146. package/types/src/Composition/not.d.ts +0 -26
  147. package/types/src/Composition/not.d.ts.map +0 -1
  148. package/types/src/Composition/on.d.ts +0 -13
  149. package/types/src/Composition/on.d.ts.map +0 -1
  150. package/types/src/Composition/pipe.d.ts +0 -56
  151. package/types/src/Composition/pipe.d.ts.map +0 -1
  152. package/types/src/Composition/tap.d.ts +0 -31
  153. package/types/src/Composition/tap.d.ts.map +0 -1
  154. package/types/src/Composition/uncurry.d.ts +0 -54
  155. package/types/src/Composition/uncurry.d.ts.map +0 -1
  156. package/types/src/Core/Deferred.d.ts +0 -49
  157. package/types/src/Core/Deferred.d.ts.map +0 -1
  158. package/types/src/Core/InternalTypes.d.ts +0 -23
  159. package/types/src/Core/InternalTypes.d.ts.map +0 -1
  160. package/types/src/Core/Lens.d.ts +0 -118
  161. package/types/src/Core/Lens.d.ts.map +0 -1
  162. package/types/src/Core/Logged.d.ts +0 -126
  163. package/types/src/Core/Logged.d.ts.map +0 -1
  164. package/types/src/Core/Option.d.ts +0 -209
  165. package/types/src/Core/Option.d.ts.map +0 -1
  166. package/types/src/Core/Optional.d.ts +0 -158
  167. package/types/src/Core/Optional.d.ts.map +0 -1
  168. package/types/src/Core/Predicate.d.ts +0 -161
  169. package/types/src/Core/Predicate.d.ts.map +0 -1
  170. package/types/src/Core/Reader.d.ts +0 -156
  171. package/types/src/Core/Reader.d.ts.map +0 -1
  172. package/types/src/Core/Refinement.d.ts +0 -138
  173. package/types/src/Core/Refinement.d.ts.map +0 -1
  174. package/types/src/Core/RemoteData.d.ts +0 -197
  175. package/types/src/Core/RemoteData.d.ts.map +0 -1
  176. package/types/src/Core/Result.d.ts +0 -182
  177. package/types/src/Core/Result.d.ts.map +0 -1
  178. package/types/src/Core/State.d.ts +0 -192
  179. package/types/src/Core/State.d.ts.map +0 -1
  180. package/types/src/Core/Task.d.ts +0 -219
  181. package/types/src/Core/Task.d.ts.map +0 -1
  182. package/types/src/Core/TaskOption.d.ts +0 -121
  183. package/types/src/Core/TaskOption.d.ts.map +0 -1
  184. package/types/src/Core/TaskResult.d.ts +0 -119
  185. package/types/src/Core/TaskResult.d.ts.map +0 -1
  186. package/types/src/Core/TaskValidation.d.ts +0 -144
  187. package/types/src/Core/TaskValidation.d.ts.map +0 -1
  188. package/types/src/Core/These.d.ts +0 -225
  189. package/types/src/Core/These.d.ts.map +0 -1
  190. package/types/src/Core/Tuple.d.ts +0 -129
  191. package/types/src/Core/Tuple.d.ts.map +0 -1
  192. package/types/src/Core/Validation.d.ts +0 -203
  193. package/types/src/Core/Validation.d.ts.map +0 -1
  194. package/types/src/Core/index.d.ts +0 -19
  195. package/types/src/Core/index.d.ts.map +0 -1
  196. package/types/src/Types/Brand.d.ts.map +0 -1
  197. package/types/src/Types/NonEmptyList.d.ts.map +0 -1
  198. package/types/src/Types/index.d.ts +0 -3
  199. package/types/src/Types/index.d.ts.map +0 -1
  200. package/types/src/Utils/Arr.d.ts +0 -403
  201. package/types/src/Utils/Arr.d.ts.map +0 -1
  202. package/types/src/Utils/Dict.d.ts +0 -310
  203. package/types/src/Utils/Dict.d.ts.map +0 -1
  204. package/types/src/Utils/Num.d.ts +0 -110
  205. package/types/src/Utils/Num.d.ts.map +0 -1
  206. package/types/src/Utils/Rec.d.ts +0 -159
  207. package/types/src/Utils/Rec.d.ts.map +0 -1
  208. package/types/src/Utils/Str.d.ts +0 -128
  209. package/types/src/Utils/Str.d.ts.map +0 -1
  210. package/types/src/Utils/Uniq.d.ts +0 -179
  211. package/types/src/Utils/Uniq.d.ts.map +0 -1
  212. package/types/src/Utils/index.d.ts +0 -7
  213. package/types/src/Utils/index.d.ts.map +0 -1
@@ -1,573 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Arr = void 0;
4
- const Deferred_js_1 = require("../Core/Deferred.js");
5
- const Option_js_1 = require("../Core/Option.js");
6
- const Result_js_1 = require("../Core/Result.js");
7
- const Task_js_1 = require("../Core/Task.js");
8
- const NonEmptyList_js_1 = require("../Types/NonEmptyList.js");
9
- /**
10
- * Functional array utilities that compose well with pipe.
11
- * All functions are data-last and curried where applicable.
12
- * Safe access functions return Option instead of throwing or returning undefined.
13
- *
14
- * @example
15
- * ```ts
16
- * pipe(
17
- * [1, 2, 3, 4, 5],
18
- * Arr.filter(n => n > 2),
19
- * Arr.map(n => n * 10),
20
- * Arr.head
21
- * ); // Some(30)
22
- * ```
23
- */
24
- var Arr;
25
- (function (Arr) {
26
- // --- Safe access ---
27
- /**
28
- * Returns the first element of an array, or None if the array is empty.
29
- *
30
- * @example
31
- * ```ts
32
- * Arr.head([1, 2, 3]); // Some(1)
33
- * Arr.head([]); // None
34
- * ```
35
- */
36
- Arr.head = (data) => data.length > 0 ? Option_js_1.Option.some(data[0]) : Option_js_1.Option.none();
37
- /**
38
- * Returns the last element of an array, or None if the array is empty.
39
- *
40
- * @example
41
- * ```ts
42
- * Arr.last([1, 2, 3]); // Some(3)
43
- * Arr.last([]); // None
44
- * ```
45
- */
46
- Arr.last = (data) => data.length > 0 ? Option_js_1.Option.some(data[data.length - 1]) : Option_js_1.Option.none();
47
- /**
48
- * Returns all elements except the first, or None if the array is empty.
49
- *
50
- * @example
51
- * ```ts
52
- * Arr.tail([1, 2, 3]); // Some([2, 3])
53
- * Arr.tail([]); // None
54
- * ```
55
- */
56
- Arr.tail = (data) => data.length > 0 ? Option_js_1.Option.some(data.slice(1)) : Option_js_1.Option.none();
57
- /**
58
- * Returns all elements except the last, or None if the array is empty.
59
- *
60
- * @example
61
- * ```ts
62
- * Arr.init([1, 2, 3]); // Some([1, 2])
63
- * Arr.init([]); // None
64
- * ```
65
- */
66
- Arr.init = (data) => data.length > 0 ? Option_js_1.Option.some(data.slice(0, -1)) : Option_js_1.Option.none();
67
- // --- Search ---
68
- /**
69
- * Returns the first element matching the predicate, or None.
70
- *
71
- * @example
72
- * ```ts
73
- * pipe([1, 2, 3, 4], Arr.findFirst(n => n > 2)); // Some(3)
74
- * ```
75
- */
76
- Arr.findFirst = (predicate) => (data) => {
77
- const idx = data.findIndex(predicate);
78
- return idx >= 0 ? Option_js_1.Option.some(data[idx]) : Option_js_1.Option.none();
79
- };
80
- /**
81
- * Returns the last element matching the predicate, or None.
82
- *
83
- * @example
84
- * ```ts
85
- * pipe([1, 2, 3, 4], Arr.findLast(n => n > 2)); // Some(4)
86
- * ```
87
- */
88
- Arr.findLast = (predicate) => (data) => {
89
- for (let i = data.length - 1; i >= 0; i--) {
90
- if (predicate(data[i]))
91
- return Option_js_1.Option.some(data[i]);
92
- }
93
- return Option_js_1.Option.none();
94
- };
95
- /**
96
- * Returns the index of the first element matching the predicate, or None.
97
- *
98
- * @example
99
- * ```ts
100
- * pipe([1, 2, 3, 4], Arr.findIndex(n => n > 2)); // Some(2)
101
- * ```
102
- */
103
- Arr.findIndex = (predicate) => (data) => {
104
- const idx = data.findIndex(predicate);
105
- return idx >= 0 ? Option_js_1.Option.some(idx) : Option_js_1.Option.none();
106
- };
107
- // --- Transform ---
108
- /**
109
- * Transforms each element of an array.
110
- *
111
- * @example
112
- * ```ts
113
- * pipe([1, 2, 3], Arr.map(n => n * 2)); // [2, 4, 6]
114
- * ```
115
- */
116
- Arr.map = (f) => (data) => {
117
- const n = data.length;
118
- const result = new Array(n);
119
- for (let i = 0; i < n; i++)
120
- result[i] = f(data[i]);
121
- return result;
122
- };
123
- /**
124
- * Filters elements that satisfy the predicate.
125
- *
126
- * @example
127
- * ```ts
128
- * pipe([1, 2, 3, 4], Arr.filter(n => n % 2 === 0)); // [2, 4]
129
- * ```
130
- */
131
- Arr.filter = (predicate) => (data) => {
132
- const n = data.length;
133
- const result = [];
134
- for (let i = 0; i < n; i++) {
135
- if (predicate(data[i]))
136
- result.push(data[i]);
137
- }
138
- return result;
139
- };
140
- /**
141
- * Splits an array into two groups based on a predicate.
142
- * First group contains elements that satisfy the predicate,
143
- * second group contains the rest.
144
- *
145
- * @example
146
- * ```ts
147
- * pipe([1, 2, 3, 4], Arr.partition(n => n % 2 === 0)); // [[2, 4], [1, 3]]
148
- * ```
149
- */
150
- Arr.partition = (predicate) => (data) => {
151
- const pass = [];
152
- const fail = [];
153
- for (const a of data) {
154
- (predicate(a) ? pass : fail).push(a);
155
- }
156
- return [pass, fail];
157
- };
158
- /**
159
- * Groups elements by a key function.
160
- *
161
- * @example
162
- * ```ts
163
- * pipe(
164
- * ["apple", "avocado", "banana"],
165
- * Arr.groupBy(s => s[0])
166
- * ); // { a: ["apple", "avocado"], b: ["banana"] }
167
- * ```
168
- */
169
- Arr.groupBy = (f) => (data) => {
170
- const result = {};
171
- for (const a of data) {
172
- const key = f(a);
173
- if (!result[key])
174
- result[key] = [];
175
- result[key].push(a);
176
- }
177
- return result;
178
- };
179
- /**
180
- * Removes duplicate elements using strict equality.
181
- *
182
- * @example
183
- * ```ts
184
- * Arr.uniq([1, 2, 2, 3, 1]); // [1, 2, 3]
185
- * ```
186
- */
187
- Arr.uniq = (data) => [
188
- ...new Set(data),
189
- ];
190
- /**
191
- * Removes duplicate elements by comparing the result of a key function.
192
- *
193
- * @example
194
- * ```ts
195
- * pipe(
196
- * [{id: 1, name: "a"}, {id: 1, name: "b"}, {id: 2, name: "c"}],
197
- * Arr.uniqBy(x => x.id)
198
- * ); // [{id: 1, name: "a"}, {id: 2, name: "c"}]
199
- * ```
200
- */
201
- Arr.uniqBy = (f) => (data) => {
202
- const seen = new Set();
203
- const result = [];
204
- for (const a of data) {
205
- const key = f(a);
206
- if (!seen.has(key)) {
207
- seen.add(key);
208
- result.push(a);
209
- }
210
- }
211
- return result;
212
- };
213
- /**
214
- * Sorts an array using a comparison function. Returns a new array.
215
- *
216
- * @example
217
- * ```ts
218
- * pipe([3, 1, 2], Arr.sortBy((a, b) => a - b)); // [1, 2, 3]
219
- * ```
220
- */
221
- Arr.sortBy = (compare) => (data) => [...data].sort(compare);
222
- // --- Combine ---
223
- /**
224
- * Pairs up elements from two arrays. Stops at the shorter array.
225
- *
226
- * @example
227
- * ```ts
228
- * pipe([1, 2, 3], Arr.zip(["a", "b"])); // [[1, "a"], [2, "b"]]
229
- * ```
230
- */
231
- Arr.zip = (other) => (data) => {
232
- const len = Math.min(data.length, other.length);
233
- const result = new Array(len);
234
- for (let i = 0; i < len; i++) {
235
- result[i] = [data[i], other[i]];
236
- }
237
- return result;
238
- };
239
- /**
240
- * Combines elements from two arrays using a function. Stops at the shorter array.
241
- *
242
- * @example
243
- * ```ts
244
- * pipe([1, 2], Arr.zipWith((a, b) => a + b, ["a", "b"])); // ["1a", "2b"]
245
- * ```
246
- */
247
- Arr.zipWith = (f) => (other) => (data) => {
248
- const len = Math.min(data.length, other.length);
249
- const result = new Array(len);
250
- for (let i = 0; i < len; i++) {
251
- result[i] = f(data[i], other[i]);
252
- }
253
- return result;
254
- };
255
- /**
256
- * Inserts a separator between every element.
257
- *
258
- * @example
259
- * ```ts
260
- * pipe([1, 2, 3], Arr.intersperse(0)); // [1, 0, 2, 0, 3]
261
- * ```
262
- */
263
- Arr.intersperse = (sep) => (data) => {
264
- if (data.length <= 1)
265
- return data;
266
- const result = [data[0]];
267
- for (let i = 1; i < data.length; i++) {
268
- result.push(sep, data[i]);
269
- }
270
- return result;
271
- };
272
- /**
273
- * Splits an array into chunks of the given size.
274
- *
275
- * @example
276
- * ```ts
277
- * pipe([1, 2, 3, 4, 5], Arr.chunksOf(2)); // [[1, 2], [3, 4], [5]]
278
- * ```
279
- */
280
- Arr.chunksOf = (n) => (data) => {
281
- if (n <= 0)
282
- return [];
283
- const result = [];
284
- for (let i = 0; i < data.length; i += n) {
285
- result.push(data.slice(i, i + n));
286
- }
287
- return result;
288
- };
289
- /**
290
- * Flattens a nested array by one level.
291
- *
292
- * @example
293
- * ```ts
294
- * Arr.flatten([[1, 2], [3], [4, 5]]); // [1, 2, 3, 4, 5]
295
- * ```
296
- */
297
- Arr.flatten = (data) => [].concat(...data);
298
- /**
299
- * Maps each element to an array and flattens the result.
300
- *
301
- * @example
302
- * ```ts
303
- * pipe([1, 2, 3], Arr.flatMap(n => [n, n * 10])); // [1, 10, 2, 20, 3, 30]
304
- * ```
305
- */
306
- Arr.flatMap = (f) => (data) => {
307
- const n = data.length;
308
- const result = [];
309
- for (let i = 0; i < n; i++) {
310
- const chunk = f(data[i]);
311
- const m = chunk.length;
312
- for (let j = 0; j < m; j++)
313
- result.push(chunk[j]);
314
- }
315
- return result;
316
- };
317
- /**
318
- * Reduces an array from the left.
319
- *
320
- * @example
321
- * ```ts
322
- * pipe([1, 2, 3], Arr.reduce(0, (acc, n) => acc + n)); // 6
323
- * ```
324
- */
325
- Arr.reduce = (initial, f) => (data) => data.reduce(f, initial);
326
- // --- Traverse / Sequence ---
327
- /**
328
- * Maps each element to an Option and collects the results.
329
- * Returns None if any mapping returns None.
330
- *
331
- * @example
332
- * ```ts
333
- * const parseNum = (s: string): Option<number> => {
334
- * const n = Number(s);
335
- * return isNaN(n) ? Option.none() : Option.some(n);
336
- * };
337
- *
338
- * pipe(["1", "2", "3"], Arr.traverse(parseNum)); // Some([1, 2, 3])
339
- * pipe(["1", "x", "3"], Arr.traverse(parseNum)); // None
340
- * ```
341
- */
342
- Arr.traverse = (f) => (data) => {
343
- const n = data.length;
344
- const result = new Array(n);
345
- for (let i = 0; i < n; i++) {
346
- const mapped = f(data[i]);
347
- if (mapped.kind === "None")
348
- return Option_js_1.Option.none();
349
- result[i] = mapped.value;
350
- }
351
- return Option_js_1.Option.some(result);
352
- };
353
- /**
354
- * Maps each element to a Result and collects the results.
355
- * Returns the first Err if any mapping fails.
356
- *
357
- * @example
358
- * ```ts
359
- * pipe(
360
- * [1, 2, 3],
361
- * Arr.traverseResult(n => n > 0 ? Result.ok(n) : Result.err("negative"))
362
- * ); // Ok([1, 2, 3])
363
- * ```
364
- */
365
- Arr.traverseResult = (f) => (data) => {
366
- const n = data.length;
367
- const result = new Array(n);
368
- for (let i = 0; i < n; i++) {
369
- const mapped = f(data[i]);
370
- if (mapped.kind === "Error")
371
- return mapped;
372
- result[i] = mapped.value;
373
- }
374
- return Result_js_1.Result.ok(result);
375
- };
376
- /**
377
- * Maps each element to a Task and runs all in parallel.
378
- *
379
- * @example
380
- * ```ts
381
- * pipe(
382
- * [1, 2, 3],
383
- * Arr.traverseTask(n => Task.resolve(n * 2))
384
- * )(); // Promise<[2, 4, 6]>
385
- * ```
386
- */
387
- Arr.traverseTask = (f) => (data) => Task_js_1.Task.from(() => Promise.all(data.map((a) => Deferred_js_1.Deferred.toPromise(f(a)()))));
388
- /**
389
- * Collects an array of Options into an Option of array.
390
- * Returns None if any element is None.
391
- *
392
- * @example
393
- * ```ts
394
- * Arr.sequence([Option.some(1), Option.some(2)]); // Some([1, 2])
395
- * Arr.sequence([Option.some(1), Option.none()]); // None
396
- * ```
397
- */
398
- Arr.sequence = (data) => Arr.traverse((a) => a)(data);
399
- /**
400
- * Collects an array of Results into a Result of array.
401
- * Returns the first Err if any element is Err.
402
- */
403
- Arr.sequenceResult = (data) => Arr.traverseResult((a) => a)(data);
404
- /**
405
- * Collects an array of Tasks into a Task of array. Runs in parallel.
406
- */
407
- Arr.sequenceTask = (data) => Arr.traverseTask((a) => a)(data);
408
- /**
409
- * Maps each element to a TaskResult and runs them sequentially.
410
- * Returns the first Err encountered, or Ok of all results if all succeed.
411
- *
412
- * @example
413
- * ```ts
414
- * const validate = (n: number): TaskResult<string, number> =>
415
- * n > 0 ? TaskResult.ok(n) : TaskResult.err("non-positive");
416
- *
417
- * pipe(
418
- * [1, 2, 3],
419
- * Arr.traverseTaskResult(validate)
420
- * )(); // Deferred<Ok([1, 2, 3])>
421
- *
422
- * pipe(
423
- * [1, -1, 3],
424
- * Arr.traverseTaskResult(validate)
425
- * )(); // Deferred<Err("non-positive")>
426
- * ```
427
- */
428
- Arr.traverseTaskResult = (f) => (data) => Task_js_1.Task.from(async () => {
429
- const result = [];
430
- for (const a of data) {
431
- const r = await Deferred_js_1.Deferred.toPromise(f(a)());
432
- if (Result_js_1.Result.isErr(r))
433
- return r;
434
- result.push(r.value);
435
- }
436
- return Result_js_1.Result.ok(result);
437
- });
438
- /**
439
- * Collects an array of TaskResults into a TaskResult of array.
440
- * Returns the first Err if any element is Err, runs sequentially.
441
- */
442
- Arr.sequenceTaskResult = (data) => Arr.traverseTaskResult((a) => a)(data);
443
- /**
444
- * Returns true if the array is non-empty (type guard).
445
- */
446
- Arr.isNonEmpty = (data) => (0, NonEmptyList_js_1.isNonEmptyList)(data);
447
- /**
448
- * Returns the length of an array.
449
- */
450
- Arr.size = (data) => data.length;
451
- /**
452
- * Returns true if any element satisfies the predicate.
453
- *
454
- * @example
455
- * ```ts
456
- * pipe([1, 2, 3], Arr.some(n => n > 2)); // true
457
- * ```
458
- */
459
- Arr.some = (predicate) => (data) => {
460
- const n = data.length;
461
- for (let i = 0; i < n; i++)
462
- if (predicate(data[i]))
463
- return true;
464
- return false;
465
- };
466
- /**
467
- * Returns true if all elements satisfy the predicate.
468
- *
469
- * @example
470
- * ```ts
471
- * pipe([1, 2, 3], Arr.every(n => n > 0)); // true
472
- * ```
473
- */
474
- Arr.every = (predicate) => (data) => {
475
- const n = data.length;
476
- for (let i = 0; i < n; i++)
477
- if (!predicate(data[i]))
478
- return false;
479
- return true;
480
- };
481
- /**
482
- * Reverses an array. Returns a new array.
483
- *
484
- * @example
485
- * ```ts
486
- * Arr.reverse([1, 2, 3]); // [3, 2, 1]
487
- * ```
488
- */
489
- Arr.reverse = (data) => [...data].reverse();
490
- /**
491
- * Takes the first n elements from an array.
492
- *
493
- * @example
494
- * ```ts
495
- * pipe([1, 2, 3, 4], Arr.take(2)); // [1, 2]
496
- * ```
497
- */
498
- Arr.take = (n) => (data) => n <= 0 ? [] : data.slice(0, n);
499
- /**
500
- * Drops the first n elements from an array.
501
- *
502
- * @example
503
- * ```ts
504
- * pipe([1, 2, 3, 4], Arr.drop(2)); // [3, 4]
505
- * ```
506
- */
507
- Arr.drop = (n) => (data) => data.slice(n);
508
- /**
509
- * Takes elements from the start while the predicate holds.
510
- *
511
- * @example
512
- * ```ts
513
- * pipe([1, 2, 3, 1], Arr.takeWhile(n => n < 3)); // [1, 2]
514
- * ```
515
- */
516
- Arr.takeWhile = (predicate) => (data) => {
517
- const result = [];
518
- for (const a of data) {
519
- if (!predicate(a))
520
- break;
521
- result.push(a);
522
- }
523
- return result;
524
- };
525
- /**
526
- * Drops elements from the start while the predicate holds.
527
- *
528
- * @example
529
- * ```ts
530
- * pipe([1, 2, 3, 1], Arr.dropWhile(n => n < 3)); // [3, 1]
531
- * ```
532
- */
533
- Arr.dropWhile = (predicate) => (data) => {
534
- let i = 0;
535
- while (i < data.length && predicate(data[i]))
536
- i++;
537
- return data.slice(i);
538
- };
539
- /**
540
- * Like `reduce`, but returns every intermediate accumulator as an array.
541
- * The initial value is not included — the output has the same length as the input.
542
- *
543
- * @example
544
- * ```ts
545
- * pipe([1, 2, 3], Arr.scan(0, (acc, n) => acc + n)); // [1, 3, 6]
546
- * ```
547
- */
548
- Arr.scan = (initial, f) => (data) => {
549
- const n = data.length;
550
- const result = new Array(n);
551
- let acc = initial;
552
- for (let i = 0; i < n; i++) {
553
- acc = f(acc, data[i]);
554
- result[i] = acc;
555
- }
556
- return result;
557
- };
558
- /**
559
- * Splits an array at an index into a `[before, after]` tuple.
560
- * Negative indices clamp to 0; indices beyond the array length clamp to the end.
561
- *
562
- * @example
563
- * ```ts
564
- * pipe([1, 2, 3, 4], Arr.splitAt(2)); // [[1, 2], [3, 4]]
565
- * pipe([1, 2, 3], Arr.splitAt(0)); // [[], [1, 2, 3]]
566
- * pipe([1, 2, 3], Arr.splitAt(10)); // [[1, 2, 3], []]
567
- * ```
568
- */
569
- Arr.splitAt = (index) => (data) => {
570
- const i = Math.max(0, index);
571
- return [data.slice(0, i), data.slice(i)];
572
- };
573
- })(Arr || (exports.Arr = Arr = {}));