iterflow 0.2.2
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/LICENSE +24 -0
- package/README.md +228 -0
- package/dist/fn/index.cjs +904 -0
- package/dist/fn/index.cjs.map +1 -0
- package/dist/fn/index.d.cts +1187 -0
- package/dist/fn/index.d.ts +1187 -0
- package/dist/fn/index.js +837 -0
- package/dist/fn/index.js.map +1 -0
- package/dist/index.cjs +4030 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2264 -0
- package/dist/index.d.ts +2264 -0
- package/dist/index.js +3977 -0
- package/dist/index.js.map +1 -0
- package/package.json +111 -0
|
@@ -0,0 +1,904 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/errors.ts
|
|
4
|
+
var iterflowError = class extends Error {
|
|
5
|
+
operation;
|
|
6
|
+
context;
|
|
7
|
+
constructor(message, operation, context) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "iterflowError";
|
|
10
|
+
this.operation = operation;
|
|
11
|
+
this.context = context;
|
|
12
|
+
if (Error.captureStackTrace) {
|
|
13
|
+
Error.captureStackTrace(this, this.constructor);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns a detailed error message with context
|
|
18
|
+
*/
|
|
19
|
+
toDetailedString() {
|
|
20
|
+
let msg = `${this.name}: ${this.message}`;
|
|
21
|
+
if (this.operation) {
|
|
22
|
+
msg += `
|
|
23
|
+
Operation: ${this.operation}`;
|
|
24
|
+
}
|
|
25
|
+
if (this.context && Object.keys(this.context).length > 0) {
|
|
26
|
+
msg += "\n Context:";
|
|
27
|
+
for (const [key, value] of Object.entries(this.context)) {
|
|
28
|
+
msg += `
|
|
29
|
+
${key}: ${JSON.stringify(value)}`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (this.stack) {
|
|
33
|
+
msg += `
|
|
34
|
+
Stack: ${this.stack.split("\n").slice(1).join("\n")}`;
|
|
35
|
+
}
|
|
36
|
+
return msg;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var ValidationError = class extends iterflowError {
|
|
40
|
+
constructor(message, operation, context) {
|
|
41
|
+
super(message, operation, context);
|
|
42
|
+
this.name = "ValidationError";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/validation.ts
|
|
47
|
+
function validatePositiveInteger(value, paramName, operation) {
|
|
48
|
+
if (!Number.isInteger(value)) {
|
|
49
|
+
throw new ValidationError(
|
|
50
|
+
`${paramName} must be an integer, got ${value}`,
|
|
51
|
+
operation,
|
|
52
|
+
{ paramName, value }
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
if (value < 1) {
|
|
56
|
+
throw new ValidationError(
|
|
57
|
+
`${paramName} must be at least 1, got ${value}`,
|
|
58
|
+
operation,
|
|
59
|
+
{ paramName, value }
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function validateRange(value, min2, max2, paramName, operation) {
|
|
64
|
+
if (value < min2 || value > max2) {
|
|
65
|
+
throw new ValidationError(
|
|
66
|
+
`${paramName} must be between ${min2} and ${max2}, got ${value}`,
|
|
67
|
+
operation,
|
|
68
|
+
{ paramName, value, min: min2, max: max2 }
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function validateNonZero(value, paramName, operation) {
|
|
73
|
+
if (value === 0) {
|
|
74
|
+
throw new ValidationError(`${paramName} cannot be zero`, operation, {
|
|
75
|
+
paramName,
|
|
76
|
+
value
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/fn/composition.ts
|
|
82
|
+
function pipe(...fns) {
|
|
83
|
+
return (input) => fns.reduce((acc, fn) => fn(acc), input);
|
|
84
|
+
}
|
|
85
|
+
function compose(...fns) {
|
|
86
|
+
return (input) => fns.reduceRight((acc, fn) => fn(acc), input);
|
|
87
|
+
}
|
|
88
|
+
function createOperation(name, generator) {
|
|
89
|
+
if (generator.length === 1) {
|
|
90
|
+
return (iterable) => {
|
|
91
|
+
const result = generator(iterable);
|
|
92
|
+
Object.defineProperty(result, "name", { value: name });
|
|
93
|
+
return result;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return (...configs) => {
|
|
97
|
+
return (iterable) => {
|
|
98
|
+
const result = generator(iterable, ...configs);
|
|
99
|
+
Object.defineProperty(result, "name", { value: name });
|
|
100
|
+
return result;
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function mapTransducer(fn) {
|
|
105
|
+
return (reducer) => {
|
|
106
|
+
return (accumulator, value) => {
|
|
107
|
+
return reducer(accumulator, fn(value));
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function filterTransducer(predicate) {
|
|
112
|
+
return (reducer) => {
|
|
113
|
+
return (accumulator, value) => {
|
|
114
|
+
return predicate(value) ? reducer(accumulator, value) : accumulator;
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function takeTransducer(n) {
|
|
119
|
+
return (reducer) => {
|
|
120
|
+
let taken = 0;
|
|
121
|
+
return (accumulator, value) => {
|
|
122
|
+
if (taken < n) {
|
|
123
|
+
taken++;
|
|
124
|
+
const result = reducer(accumulator, value);
|
|
125
|
+
if (taken === n) {
|
|
126
|
+
return reduced(result);
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
return accumulator;
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function composeTransducers(...transducers) {
|
|
135
|
+
return (reducer) => {
|
|
136
|
+
return transducers.reduceRight((acc, xf) => xf(acc), reducer);
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
var REDUCED = Symbol("@@transducer/reduced");
|
|
140
|
+
function reduced(value) {
|
|
141
|
+
return {
|
|
142
|
+
[REDUCED]: true,
|
|
143
|
+
value
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function isReduced(value) {
|
|
147
|
+
return value != null && value[REDUCED] === true;
|
|
148
|
+
}
|
|
149
|
+
function transduce(transducer, reducer, initial, iterable) {
|
|
150
|
+
const xform = transducer(reducer);
|
|
151
|
+
let accumulator = initial;
|
|
152
|
+
for (const value of iterable) {
|
|
153
|
+
accumulator = xform(accumulator, value);
|
|
154
|
+
if (isReduced(accumulator)) {
|
|
155
|
+
return accumulator.value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return accumulator;
|
|
159
|
+
}
|
|
160
|
+
function transducerToIterator(transducer) {
|
|
161
|
+
return function* (iterable) {
|
|
162
|
+
const results = [];
|
|
163
|
+
const reducer = (acc, value) => {
|
|
164
|
+
acc.push(value);
|
|
165
|
+
return acc;
|
|
166
|
+
};
|
|
167
|
+
const xform = transducer(reducer);
|
|
168
|
+
for (const value of iterable) {
|
|
169
|
+
const before = results.length;
|
|
170
|
+
const result = xform(results, value);
|
|
171
|
+
if (isReduced(result)) {
|
|
172
|
+
for (let i = before; i < result.value.length; i++) {
|
|
173
|
+
yield result.value[i];
|
|
174
|
+
}
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
for (let i = before; i < results.length; i++) {
|
|
178
|
+
yield results[i];
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/fn/index.ts
|
|
185
|
+
function sum(iterable) {
|
|
186
|
+
let total = 0;
|
|
187
|
+
for (const value of iterable) {
|
|
188
|
+
total += value;
|
|
189
|
+
}
|
|
190
|
+
return total;
|
|
191
|
+
}
|
|
192
|
+
function mean(iterable) {
|
|
193
|
+
let total = 0;
|
|
194
|
+
let count2 = 0;
|
|
195
|
+
for (const value of iterable) {
|
|
196
|
+
total += value;
|
|
197
|
+
count2++;
|
|
198
|
+
}
|
|
199
|
+
return count2 === 0 ? void 0 : total / count2;
|
|
200
|
+
}
|
|
201
|
+
function min(iterable) {
|
|
202
|
+
let minimum = void 0;
|
|
203
|
+
for (const value of iterable) {
|
|
204
|
+
if (minimum === void 0 || value < minimum) {
|
|
205
|
+
minimum = value;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return minimum;
|
|
209
|
+
}
|
|
210
|
+
function max(iterable) {
|
|
211
|
+
let maximum = void 0;
|
|
212
|
+
for (const value of iterable) {
|
|
213
|
+
if (maximum === void 0 || value > maximum) {
|
|
214
|
+
maximum = value;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return maximum;
|
|
218
|
+
}
|
|
219
|
+
function count(iterable) {
|
|
220
|
+
let count2 = 0;
|
|
221
|
+
for (const _ of iterable) {
|
|
222
|
+
count2++;
|
|
223
|
+
}
|
|
224
|
+
return count2;
|
|
225
|
+
}
|
|
226
|
+
function median(iterable) {
|
|
227
|
+
const values = Array.from(iterable);
|
|
228
|
+
if (values.length === 0) return void 0;
|
|
229
|
+
values.sort((a, b) => a - b);
|
|
230
|
+
const mid = Math.floor(values.length / 2);
|
|
231
|
+
if (values.length % 2 === 0) {
|
|
232
|
+
return (values[mid - 1] + values[mid]) / 2;
|
|
233
|
+
} else {
|
|
234
|
+
return values[mid];
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function variance(iterable) {
|
|
238
|
+
const values = Array.from(iterable);
|
|
239
|
+
if (values.length === 0) return void 0;
|
|
240
|
+
const mean2 = values.reduce((sum2, val) => sum2 + val, 0) / values.length;
|
|
241
|
+
let sumSquaredDiffs = 0;
|
|
242
|
+
for (let i = 0; i < values.length; i++) {
|
|
243
|
+
const diff = values[i] - mean2;
|
|
244
|
+
sumSquaredDiffs += diff * diff;
|
|
245
|
+
}
|
|
246
|
+
return sumSquaredDiffs / values.length;
|
|
247
|
+
}
|
|
248
|
+
function stdDev(iterable) {
|
|
249
|
+
const varianceValue = variance(iterable);
|
|
250
|
+
return varianceValue === void 0 ? void 0 : Math.sqrt(varianceValue);
|
|
251
|
+
}
|
|
252
|
+
function percentile(iterable, p) {
|
|
253
|
+
validateRange(p, 0, 100, "percentile", "percentile");
|
|
254
|
+
const values = Array.from(iterable);
|
|
255
|
+
if (values.length === 0) return void 0;
|
|
256
|
+
values.sort((a, b) => a - b);
|
|
257
|
+
if (p === 0) return values[0];
|
|
258
|
+
if (p === 100) return values[values.length - 1];
|
|
259
|
+
const index = p / 100 * (values.length - 1);
|
|
260
|
+
const lower = Math.floor(index);
|
|
261
|
+
const upper = Math.ceil(index);
|
|
262
|
+
if (lower === upper) {
|
|
263
|
+
return values[lower];
|
|
264
|
+
}
|
|
265
|
+
const weight = index - lower;
|
|
266
|
+
return values[lower] * (1 - weight) + values[upper] * weight;
|
|
267
|
+
}
|
|
268
|
+
function mode(iterable) {
|
|
269
|
+
const values = Array.from(iterable);
|
|
270
|
+
if (values.length === 0) return void 0;
|
|
271
|
+
const frequency = /* @__PURE__ */ new Map();
|
|
272
|
+
let maxFreq = 0;
|
|
273
|
+
for (const value of values) {
|
|
274
|
+
const count2 = (frequency.get(value) || 0) + 1;
|
|
275
|
+
frequency.set(value, count2);
|
|
276
|
+
maxFreq = Math.max(maxFreq, count2);
|
|
277
|
+
}
|
|
278
|
+
const modes = [];
|
|
279
|
+
for (const [value, freq] of frequency) {
|
|
280
|
+
if (freq === maxFreq) {
|
|
281
|
+
modes.push(value);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return modes.sort((a, b) => a - b);
|
|
285
|
+
}
|
|
286
|
+
function quartiles(iterable) {
|
|
287
|
+
const values = Array.from(iterable);
|
|
288
|
+
if (values.length === 0) return void 0;
|
|
289
|
+
values.sort((a, b) => a - b);
|
|
290
|
+
const calculatePercentile = (p) => {
|
|
291
|
+
if (p === 0) return values[0];
|
|
292
|
+
if (p === 100) return values[values.length - 1];
|
|
293
|
+
const index = p / 100 * (values.length - 1);
|
|
294
|
+
const lower = Math.floor(index);
|
|
295
|
+
const upper = Math.ceil(index);
|
|
296
|
+
if (lower === upper) {
|
|
297
|
+
return values[lower];
|
|
298
|
+
}
|
|
299
|
+
const weight = index - lower;
|
|
300
|
+
return values[lower] * (1 - weight) + values[upper] * weight;
|
|
301
|
+
};
|
|
302
|
+
return {
|
|
303
|
+
Q1: calculatePercentile(25),
|
|
304
|
+
Q2: calculatePercentile(50),
|
|
305
|
+
Q3: calculatePercentile(75)
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
function span(iterable) {
|
|
309
|
+
let minimum = void 0;
|
|
310
|
+
let maximum = void 0;
|
|
311
|
+
for (const value of iterable) {
|
|
312
|
+
if (minimum === void 0 || value < minimum) {
|
|
313
|
+
minimum = value;
|
|
314
|
+
}
|
|
315
|
+
if (maximum === void 0 || value > maximum) {
|
|
316
|
+
maximum = value;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return minimum === void 0 || maximum === void 0 ? void 0 : maximum - minimum;
|
|
320
|
+
}
|
|
321
|
+
function product(iterable) {
|
|
322
|
+
let result = 1;
|
|
323
|
+
for (const value of iterable) {
|
|
324
|
+
result *= value;
|
|
325
|
+
}
|
|
326
|
+
return result;
|
|
327
|
+
}
|
|
328
|
+
function covariance(iter1, iter2) {
|
|
329
|
+
const values1 = Array.from(iter1);
|
|
330
|
+
const values2 = Array.from(iter2);
|
|
331
|
+
if (values1.length === 0 || values2.length === 0 || values1.length !== values2.length) {
|
|
332
|
+
return void 0;
|
|
333
|
+
}
|
|
334
|
+
const mean1 = values1.reduce((sum2, val) => sum2 + val, 0) / values1.length;
|
|
335
|
+
const mean2 = values2.reduce((sum2, val) => sum2 + val, 0) / values2.length;
|
|
336
|
+
let covariance2 = 0;
|
|
337
|
+
for (let i = 0; i < values1.length; i++) {
|
|
338
|
+
covariance2 += (values1[i] - mean1) * (values2[i] - mean2);
|
|
339
|
+
}
|
|
340
|
+
return covariance2 / values1.length;
|
|
341
|
+
}
|
|
342
|
+
function correlation(iter1, iter2) {
|
|
343
|
+
const values1 = Array.from(iter1);
|
|
344
|
+
const values2 = Array.from(iter2);
|
|
345
|
+
if (values1.length === 0 || values2.length === 0 || values1.length !== values2.length) {
|
|
346
|
+
return void 0;
|
|
347
|
+
}
|
|
348
|
+
const mean1 = values1.reduce((sum2, val) => sum2 + val, 0) / values1.length;
|
|
349
|
+
const mean2 = values2.reduce((sum2, val) => sum2 + val, 0) / values2.length;
|
|
350
|
+
let covariance2 = 0;
|
|
351
|
+
let variance1 = 0;
|
|
352
|
+
let variance2 = 0;
|
|
353
|
+
for (let i = 0; i < values1.length; i++) {
|
|
354
|
+
const diff1 = values1[i] - mean1;
|
|
355
|
+
const diff2 = values2[i] - mean2;
|
|
356
|
+
covariance2 += diff1 * diff2;
|
|
357
|
+
variance1 += diff1 * diff1;
|
|
358
|
+
variance2 += diff2 * diff2;
|
|
359
|
+
}
|
|
360
|
+
const stdDev1 = Math.sqrt(variance1 / values1.length);
|
|
361
|
+
const stdDev2 = Math.sqrt(variance2 / values2.length);
|
|
362
|
+
if (stdDev1 === 0 || stdDev2 === 0) {
|
|
363
|
+
return void 0;
|
|
364
|
+
}
|
|
365
|
+
return covariance2 / (values1.length * stdDev1 * stdDev2);
|
|
366
|
+
}
|
|
367
|
+
function map(fn) {
|
|
368
|
+
return function* (iterable) {
|
|
369
|
+
for (const value of iterable) {
|
|
370
|
+
yield fn(value);
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
function filter(predicate) {
|
|
375
|
+
return function* (iterable) {
|
|
376
|
+
for (const value of iterable) {
|
|
377
|
+
if (predicate(value)) {
|
|
378
|
+
yield value;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
function take(limit) {
|
|
384
|
+
return function* (iterable) {
|
|
385
|
+
let count2 = 0;
|
|
386
|
+
for (const value of iterable) {
|
|
387
|
+
if (count2 >= limit) break;
|
|
388
|
+
yield value;
|
|
389
|
+
count2++;
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function drop(count2) {
|
|
394
|
+
return function* (iterable) {
|
|
395
|
+
let dropped = 0;
|
|
396
|
+
for (const value of iterable) {
|
|
397
|
+
if (dropped < count2) {
|
|
398
|
+
dropped++;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
yield value;
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
function flatMap(fn) {
|
|
406
|
+
return function* (iterable) {
|
|
407
|
+
for (const value of iterable) {
|
|
408
|
+
yield* fn(value);
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
function concat() {
|
|
413
|
+
return function* (...iterables) {
|
|
414
|
+
for (const iterable of iterables) {
|
|
415
|
+
yield* iterable;
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
function intersperse(separator) {
|
|
420
|
+
return function* (iterable) {
|
|
421
|
+
let isFirst = true;
|
|
422
|
+
for (const value of iterable) {
|
|
423
|
+
if (!isFirst) {
|
|
424
|
+
yield separator;
|
|
425
|
+
}
|
|
426
|
+
yield value;
|
|
427
|
+
isFirst = false;
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
function scan(fn, initial) {
|
|
432
|
+
return function* (iterable) {
|
|
433
|
+
let accumulator = initial;
|
|
434
|
+
yield accumulator;
|
|
435
|
+
for (const value of iterable) {
|
|
436
|
+
accumulator = fn(accumulator, value);
|
|
437
|
+
yield accumulator;
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
function enumerate() {
|
|
442
|
+
return function* (iterable) {
|
|
443
|
+
let index = 0;
|
|
444
|
+
for (const value of iterable) {
|
|
445
|
+
yield [index, value];
|
|
446
|
+
index++;
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
function reverse() {
|
|
451
|
+
return function* (iterable) {
|
|
452
|
+
const buffer = Array.from(iterable);
|
|
453
|
+
for (let i = buffer.length - 1; i >= 0; i--) {
|
|
454
|
+
yield buffer[i];
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function sort(iterable) {
|
|
459
|
+
return (function* () {
|
|
460
|
+
const buffer = Array.from(iterable);
|
|
461
|
+
buffer.sort((a, b) => {
|
|
462
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
463
|
+
return a - b;
|
|
464
|
+
}
|
|
465
|
+
return String(a).localeCompare(String(b));
|
|
466
|
+
});
|
|
467
|
+
yield* buffer;
|
|
468
|
+
})();
|
|
469
|
+
}
|
|
470
|
+
function sortBy(compareFn) {
|
|
471
|
+
return function* (iterable) {
|
|
472
|
+
const buffer = Array.from(iterable);
|
|
473
|
+
buffer.sort(compareFn);
|
|
474
|
+
yield* buffer;
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
function window(size) {
|
|
478
|
+
validatePositiveInteger(size, "size", "window");
|
|
479
|
+
return function* (iterable) {
|
|
480
|
+
const buffer = new Array(size);
|
|
481
|
+
let count2 = 0;
|
|
482
|
+
let index = 0;
|
|
483
|
+
for (const value of iterable) {
|
|
484
|
+
buffer[index] = value;
|
|
485
|
+
count2++;
|
|
486
|
+
index = (index + 1) % size;
|
|
487
|
+
if (count2 >= size) {
|
|
488
|
+
const window2 = new Array(size);
|
|
489
|
+
for (let i = 0; i < size; i++) {
|
|
490
|
+
window2[i] = buffer[(index + i) % size];
|
|
491
|
+
}
|
|
492
|
+
yield window2;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
function chunk(size) {
|
|
498
|
+
validatePositiveInteger(size, "size", "chunk");
|
|
499
|
+
return function* (iterable) {
|
|
500
|
+
let buffer = new Array(size);
|
|
501
|
+
let bufferIndex = 0;
|
|
502
|
+
for (const value of iterable) {
|
|
503
|
+
buffer[bufferIndex++] = value;
|
|
504
|
+
if (bufferIndex === size) {
|
|
505
|
+
yield buffer;
|
|
506
|
+
buffer = new Array(size);
|
|
507
|
+
bufferIndex = 0;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
if (bufferIndex > 0) {
|
|
511
|
+
yield buffer.slice(0, bufferIndex);
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function pairwise(iterable) {
|
|
516
|
+
return (function* () {
|
|
517
|
+
const windowIter = window(2)(iterable);
|
|
518
|
+
for (const arr of { [Symbol.iterator]: () => windowIter }) {
|
|
519
|
+
yield [arr[0], arr[1]];
|
|
520
|
+
}
|
|
521
|
+
})();
|
|
522
|
+
}
|
|
523
|
+
function partition(predicate) {
|
|
524
|
+
return function(iterable) {
|
|
525
|
+
const truthy = [];
|
|
526
|
+
const falsy = [];
|
|
527
|
+
for (const value of iterable) {
|
|
528
|
+
if (predicate(value)) {
|
|
529
|
+
truthy.push(value);
|
|
530
|
+
} else {
|
|
531
|
+
falsy.push(value);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return [truthy, falsy];
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
function groupBy(keyFn) {
|
|
538
|
+
return function(iterable) {
|
|
539
|
+
const groups = /* @__PURE__ */ new Map();
|
|
540
|
+
for (const value of iterable) {
|
|
541
|
+
const key = keyFn(value);
|
|
542
|
+
if (!groups.has(key)) {
|
|
543
|
+
groups.set(key, []);
|
|
544
|
+
}
|
|
545
|
+
groups.get(key).push(value);
|
|
546
|
+
}
|
|
547
|
+
return groups;
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
function distinct(iterable) {
|
|
551
|
+
return (function* () {
|
|
552
|
+
const seen = /* @__PURE__ */ new Set();
|
|
553
|
+
for (const value of iterable) {
|
|
554
|
+
if (!seen.has(value)) {
|
|
555
|
+
seen.add(value);
|
|
556
|
+
yield value;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
})();
|
|
560
|
+
}
|
|
561
|
+
function distinctBy(keyFn) {
|
|
562
|
+
return function* (iterable) {
|
|
563
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
564
|
+
for (const value of iterable) {
|
|
565
|
+
const key = keyFn(value);
|
|
566
|
+
if (!seenKeys.has(key)) {
|
|
567
|
+
seenKeys.add(key);
|
|
568
|
+
yield value;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
function tap(fn) {
|
|
574
|
+
return function* (iterable) {
|
|
575
|
+
for (const value of iterable) {
|
|
576
|
+
fn(value);
|
|
577
|
+
yield value;
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
function takeWhile(predicate) {
|
|
582
|
+
return function* (iterable) {
|
|
583
|
+
for (const value of iterable) {
|
|
584
|
+
if (!predicate(value)) break;
|
|
585
|
+
yield value;
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
function dropWhile(predicate) {
|
|
590
|
+
return function* (iterable) {
|
|
591
|
+
let dropping = true;
|
|
592
|
+
for (const value of iterable) {
|
|
593
|
+
if (dropping && predicate(value)) {
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
dropping = false;
|
|
597
|
+
yield value;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
function toArray(iterable) {
|
|
602
|
+
return Array.from(iterable);
|
|
603
|
+
}
|
|
604
|
+
function reduce(fn, initial) {
|
|
605
|
+
return function(iterable) {
|
|
606
|
+
let accumulator = initial;
|
|
607
|
+
for (const value of iterable) {
|
|
608
|
+
accumulator = fn(accumulator, value);
|
|
609
|
+
}
|
|
610
|
+
return accumulator;
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
function find(predicate) {
|
|
614
|
+
return function(iterable) {
|
|
615
|
+
for (const value of iterable) {
|
|
616
|
+
if (predicate(value)) {
|
|
617
|
+
return value;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
return void 0;
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
function findIndex(predicate) {
|
|
624
|
+
return function(iterable) {
|
|
625
|
+
let index = 0;
|
|
626
|
+
for (const value of iterable) {
|
|
627
|
+
if (predicate(value)) {
|
|
628
|
+
return index;
|
|
629
|
+
}
|
|
630
|
+
index++;
|
|
631
|
+
}
|
|
632
|
+
return -1;
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
function some(predicate) {
|
|
636
|
+
return function(iterable) {
|
|
637
|
+
for (const value of iterable) {
|
|
638
|
+
if (predicate(value)) {
|
|
639
|
+
return true;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
return false;
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function every(predicate) {
|
|
646
|
+
return function(iterable) {
|
|
647
|
+
for (const value of iterable) {
|
|
648
|
+
if (!predicate(value)) {
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return true;
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
function first(iterable, defaultValue) {
|
|
656
|
+
const iterator = iterable[Symbol.iterator]();
|
|
657
|
+
const result = iterator.next();
|
|
658
|
+
return result.done ? defaultValue : result.value;
|
|
659
|
+
}
|
|
660
|
+
function last(iterable, defaultValue) {
|
|
661
|
+
let lastValue = defaultValue;
|
|
662
|
+
let hasValue = false;
|
|
663
|
+
for (const value of iterable) {
|
|
664
|
+
lastValue = value;
|
|
665
|
+
hasValue = true;
|
|
666
|
+
}
|
|
667
|
+
return hasValue ? lastValue : defaultValue;
|
|
668
|
+
}
|
|
669
|
+
function nth(index) {
|
|
670
|
+
return function(iterable) {
|
|
671
|
+
if (index < 0) {
|
|
672
|
+
return void 0;
|
|
673
|
+
}
|
|
674
|
+
let currentIndex = 0;
|
|
675
|
+
for (const value of iterable) {
|
|
676
|
+
if (currentIndex === index) {
|
|
677
|
+
return value;
|
|
678
|
+
}
|
|
679
|
+
currentIndex++;
|
|
680
|
+
}
|
|
681
|
+
return void 0;
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
function isEmpty(iterable) {
|
|
685
|
+
const iterator = iterable[Symbol.iterator]();
|
|
686
|
+
const result = iterator.next();
|
|
687
|
+
return result.done === true;
|
|
688
|
+
}
|
|
689
|
+
function includes(searchValue) {
|
|
690
|
+
return function(iterable) {
|
|
691
|
+
for (const value of iterable) {
|
|
692
|
+
if (value === searchValue) {
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return false;
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
function zip(iter1, iter2) {
|
|
700
|
+
return (function* () {
|
|
701
|
+
const it1 = iter1[Symbol.iterator]();
|
|
702
|
+
const it2 = iter2[Symbol.iterator]();
|
|
703
|
+
while (true) {
|
|
704
|
+
const result1 = it1.next();
|
|
705
|
+
const result2 = it2.next();
|
|
706
|
+
if (result1.done || result2.done) {
|
|
707
|
+
break;
|
|
708
|
+
}
|
|
709
|
+
yield [result1.value, result2.value];
|
|
710
|
+
}
|
|
711
|
+
})();
|
|
712
|
+
}
|
|
713
|
+
function zipWith(iter1, iter2, fn) {
|
|
714
|
+
return (function* () {
|
|
715
|
+
const zipIter = zip(iter1, iter2);
|
|
716
|
+
for (const [a, b] of { [Symbol.iterator]: () => zipIter }) {
|
|
717
|
+
yield fn(a, b);
|
|
718
|
+
}
|
|
719
|
+
})();
|
|
720
|
+
}
|
|
721
|
+
function range(startOrStop, stop, step = 1) {
|
|
722
|
+
const actualStart = stop === void 0 ? 0 : startOrStop;
|
|
723
|
+
const actualStop = stop === void 0 ? startOrStop : stop;
|
|
724
|
+
return (function* () {
|
|
725
|
+
validateNonZero(step, "step", "range");
|
|
726
|
+
if (step > 0) {
|
|
727
|
+
for (let i = actualStart; i < actualStop; i += step) {
|
|
728
|
+
yield i;
|
|
729
|
+
}
|
|
730
|
+
} else {
|
|
731
|
+
for (let i = actualStart; i > actualStop; i += step) {
|
|
732
|
+
yield i;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
})();
|
|
736
|
+
}
|
|
737
|
+
function repeat(value, times) {
|
|
738
|
+
return (function* () {
|
|
739
|
+
if (times === void 0) {
|
|
740
|
+
while (true) {
|
|
741
|
+
yield value;
|
|
742
|
+
}
|
|
743
|
+
} else {
|
|
744
|
+
for (let i = 0; i < times; i++) {
|
|
745
|
+
yield value;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
})();
|
|
749
|
+
}
|
|
750
|
+
function interleave(...iterables) {
|
|
751
|
+
return (function* () {
|
|
752
|
+
if (iterables.length === 0) return;
|
|
753
|
+
const iterators = iterables.map((it) => it[Symbol.iterator]());
|
|
754
|
+
const active = new Set(iterators);
|
|
755
|
+
while (active.size > 0) {
|
|
756
|
+
for (const iterator of iterators) {
|
|
757
|
+
if (!active.has(iterator)) continue;
|
|
758
|
+
const result = iterator.next();
|
|
759
|
+
if (result.done) {
|
|
760
|
+
active.delete(iterator);
|
|
761
|
+
} else {
|
|
762
|
+
yield result.value;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
})();
|
|
767
|
+
}
|
|
768
|
+
function merge(...args) {
|
|
769
|
+
let compareFn;
|
|
770
|
+
let iterables;
|
|
771
|
+
if (typeof args[0] === "function") {
|
|
772
|
+
compareFn = args[0];
|
|
773
|
+
iterables = args.slice(1);
|
|
774
|
+
} else {
|
|
775
|
+
compareFn = (a, b) => {
|
|
776
|
+
if (a < b) return -1;
|
|
777
|
+
if (a > b) return 1;
|
|
778
|
+
return 0;
|
|
779
|
+
};
|
|
780
|
+
iterables = args;
|
|
781
|
+
}
|
|
782
|
+
return (function* () {
|
|
783
|
+
if (iterables.length === 0) return;
|
|
784
|
+
const heap = [];
|
|
785
|
+
for (let i = 0; i < iterables.length; i++) {
|
|
786
|
+
const iterator = iterables[i][Symbol.iterator]();
|
|
787
|
+
const result = iterator.next();
|
|
788
|
+
if (!result.done) {
|
|
789
|
+
heap.push({ value: result.value, iterator, index: i });
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
const bubbleDown = (index) => {
|
|
793
|
+
const length = heap.length;
|
|
794
|
+
while (true) {
|
|
795
|
+
let smallest = index;
|
|
796
|
+
const leftChild = 2 * index + 1;
|
|
797
|
+
const rightChild = 2 * index + 2;
|
|
798
|
+
if (leftChild < length && compareFn(heap[leftChild].value, heap[smallest].value) < 0) {
|
|
799
|
+
smallest = leftChild;
|
|
800
|
+
}
|
|
801
|
+
if (rightChild < length && compareFn(heap[rightChild].value, heap[smallest].value) < 0) {
|
|
802
|
+
smallest = rightChild;
|
|
803
|
+
}
|
|
804
|
+
if (smallest === index) break;
|
|
805
|
+
[heap[index], heap[smallest]] = [heap[smallest], heap[index]];
|
|
806
|
+
index = smallest;
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
for (let i = Math.floor(heap.length / 2) - 1; i >= 0; i--) {
|
|
810
|
+
bubbleDown(i);
|
|
811
|
+
}
|
|
812
|
+
while (heap.length > 0) {
|
|
813
|
+
const { value, iterator } = heap[0];
|
|
814
|
+
yield value;
|
|
815
|
+
const result = iterator.next();
|
|
816
|
+
if (result.done) {
|
|
817
|
+
heap[0] = heap[heap.length - 1];
|
|
818
|
+
heap.pop();
|
|
819
|
+
if (heap.length > 0) {
|
|
820
|
+
bubbleDown(0);
|
|
821
|
+
}
|
|
822
|
+
} else {
|
|
823
|
+
heap[0].value = result.value;
|
|
824
|
+
bubbleDown(0);
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
})();
|
|
828
|
+
}
|
|
829
|
+
function chain(...iterables) {
|
|
830
|
+
return (function* () {
|
|
831
|
+
for (const iterable of iterables) {
|
|
832
|
+
yield* iterable;
|
|
833
|
+
}
|
|
834
|
+
})();
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
exports.chain = chain;
|
|
838
|
+
exports.chunk = chunk;
|
|
839
|
+
exports.compose = compose;
|
|
840
|
+
exports.composeTransducers = composeTransducers;
|
|
841
|
+
exports.concat = concat;
|
|
842
|
+
exports.correlation = correlation;
|
|
843
|
+
exports.count = count;
|
|
844
|
+
exports.covariance = covariance;
|
|
845
|
+
exports.createOperation = createOperation;
|
|
846
|
+
exports.distinct = distinct;
|
|
847
|
+
exports.distinctBy = distinctBy;
|
|
848
|
+
exports.drop = drop;
|
|
849
|
+
exports.dropWhile = dropWhile;
|
|
850
|
+
exports.enumerate = enumerate;
|
|
851
|
+
exports.every = every;
|
|
852
|
+
exports.filter = filter;
|
|
853
|
+
exports.filterTransducer = filterTransducer;
|
|
854
|
+
exports.find = find;
|
|
855
|
+
exports.findIndex = findIndex;
|
|
856
|
+
exports.first = first;
|
|
857
|
+
exports.flatMap = flatMap;
|
|
858
|
+
exports.groupBy = groupBy;
|
|
859
|
+
exports.includes = includes;
|
|
860
|
+
exports.interleave = interleave;
|
|
861
|
+
exports.intersperse = intersperse;
|
|
862
|
+
exports.isEmpty = isEmpty;
|
|
863
|
+
exports.isReduced = isReduced;
|
|
864
|
+
exports.last = last;
|
|
865
|
+
exports.map = map;
|
|
866
|
+
exports.mapTransducer = mapTransducer;
|
|
867
|
+
exports.max = max;
|
|
868
|
+
exports.mean = mean;
|
|
869
|
+
exports.median = median;
|
|
870
|
+
exports.merge = merge;
|
|
871
|
+
exports.min = min;
|
|
872
|
+
exports.mode = mode;
|
|
873
|
+
exports.nth = nth;
|
|
874
|
+
exports.pairwise = pairwise;
|
|
875
|
+
exports.partition = partition;
|
|
876
|
+
exports.percentile = percentile;
|
|
877
|
+
exports.pipe = pipe;
|
|
878
|
+
exports.product = product;
|
|
879
|
+
exports.quartiles = quartiles;
|
|
880
|
+
exports.range = range;
|
|
881
|
+
exports.reduce = reduce;
|
|
882
|
+
exports.reduced = reduced;
|
|
883
|
+
exports.repeat = repeat;
|
|
884
|
+
exports.reverse = reverse;
|
|
885
|
+
exports.scan = scan;
|
|
886
|
+
exports.some = some;
|
|
887
|
+
exports.sort = sort;
|
|
888
|
+
exports.sortBy = sortBy;
|
|
889
|
+
exports.span = span;
|
|
890
|
+
exports.stdDev = stdDev;
|
|
891
|
+
exports.sum = sum;
|
|
892
|
+
exports.take = take;
|
|
893
|
+
exports.takeTransducer = takeTransducer;
|
|
894
|
+
exports.takeWhile = takeWhile;
|
|
895
|
+
exports.tap = tap;
|
|
896
|
+
exports.toArray = toArray;
|
|
897
|
+
exports.transduce = transduce;
|
|
898
|
+
exports.transducerToIterator = transducerToIterator;
|
|
899
|
+
exports.variance = variance;
|
|
900
|
+
exports.window = window;
|
|
901
|
+
exports.zip = zip;
|
|
902
|
+
exports.zipWith = zipWith;
|
|
903
|
+
//# sourceMappingURL=index.cjs.map
|
|
904
|
+
//# sourceMappingURL=index.cjs.map
|