iterable-linq-utility 0.7.0 → 0.9.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/README.md +35 -12
- package/dist/index.d.cts +224 -0
- package/dist/index.d.ts +224 -0
- package/dist/iterable-linq-utility.js +420 -230
- package/dist/iterable-linq-utility.umd.cjs +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -59,6 +59,26 @@ pnpm bench chains # only the chains
|
|
|
59
59
|
BENCH_TIME=1000 pnpm bench # more time for each case (default 500 ms): more samples, less noise
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
### Report
|
|
63
|
+
|
|
64
|
+
A pull request that adds an operation or changes its performance pastes the report in its description, as it is:
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
pnpm bench:report functions/sum # the same filter as pnpm bench
|
|
68
|
+
BENCH_HOST="MacBook Pro M3" pnpm bench:report functions/sum # names the host, which a container cannot see
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
Environment: Alpine Linux v3.24 (Docker), arm64, 12 cores, CPU unknown · host MacBook Pro M3 · Node v24.20.0 · Vitest 5.0.2 · BENCH_TIME 500 ms · commit 2fba44d
|
|
73
|
+
|
|
74
|
+
| case | variant | ops/s | mean (ms) | p75 (ms) | p99 (ms) | rme | vs native |
|
|
75
|
+
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
76
|
+
| sum/map | native | 567 | 1.79 | 1.76 | 3.64 | ±2.5% | — |
|
|
77
|
+
| sum/map | chain | 855 | 1.2 | 1.26 | 1.76 | ±1.8% | +51% |
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`vs native` compares the ops/s with the `native` case of the same group. When a baseline exists, a `vs baseline` column compares them with it. [ADR 0021](docs/decisions/0021-benchmark-standards.md) explains the format.
|
|
81
|
+
|
|
62
82
|
### Find regressions
|
|
63
83
|
|
|
64
84
|
A baseline is a saved run. Each table shows it as extra rows, marked `(baseline)`, next to the new run.
|
|
@@ -68,6 +88,7 @@ git switch main
|
|
|
68
88
|
pnpm bench:baseline # saves every result in .bench/
|
|
69
89
|
git switch my-branch
|
|
70
90
|
pnpm bench # shows each case next to its "(baseline)" row
|
|
91
|
+
pnpm bench:report functions/sum # or the report, with a "vs baseline" column
|
|
71
92
|
```
|
|
72
93
|
|
|
73
94
|
The timings depend on the machine, so `.bench/` is not committed and the benchmarks do not run in CI. Create the baseline and the new run on the same machine, with the same load.
|
|
@@ -76,7 +97,7 @@ The timings depend on the machine, so `.bench/` is not committed and the benchma
|
|
|
76
97
|
|
|
77
98
|
| Column | Meaning |
|
|
78
99
|
|---|---|
|
|
79
|
-
| `hz` | runs per second: higher is faster |
|
|
100
|
+
| `hz` | runs per second (ops/s in the report): higher is faster |
|
|
80
101
|
| `mean`, `p75`, `p99` | time of one run, in ms |
|
|
81
102
|
| `rme` | relative margin of error |
|
|
82
103
|
| `samples` | how many runs were measured |
|
|
@@ -85,26 +106,28 @@ A difference smaller than the `rme` of the two rows is noise.
|
|
|
85
106
|
|
|
86
107
|
### Add a benchmark
|
|
87
108
|
|
|
88
|
-
Create `test/bench/functions/<name>.bench.ts`
|
|
109
|
+
Create `test/bench/functions/<name>.bench.ts`. `scenarios()` registers the standard groups of [ADR 0021](docs/decisions/0021-benchmark-standards.md) and [ADR 0022](docs/decisions/0022-benchmark-scenarios-in-practice.md): `<name>/direct` on `numbers`, `<name>/small` on `small`, `<name>/map` and `<name>/filter` after `map(double)` and `filter(isEven)`. `group()` adds one more group on `numbers`: an early exit (`start`, `middle`) or an optional callback.
|
|
89
110
|
|
|
90
111
|
```ts
|
|
91
|
-
import { test } from 'vitest';
|
|
92
112
|
import * as Helpers from '../helpers';
|
|
93
113
|
|
|
94
114
|
import * as IterableLinq from 'iterable-linq-utility';
|
|
95
115
|
|
|
96
116
|
// Read the exports once: an imported binding goes through a module runner getter on every read.
|
|
97
|
-
const {
|
|
98
|
-
const {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
.run();
|
|
117
|
+
const { Functions } = IterableLinq;
|
|
118
|
+
const { double, scenarios, sum } = Helpers;
|
|
119
|
+
|
|
120
|
+
scenarios('map', {
|
|
121
|
+
native: values => sum(values.map(double)), // an array
|
|
122
|
+
chain: chain => sum(chain.map(double)), // a chain
|
|
123
|
+
Functions: values => sum(Functions.map(values, double)) // an iterable
|
|
105
124
|
});
|
|
106
125
|
```
|
|
107
126
|
|
|
127
|
+
- `native` is the array method a user would write. Add `loop`, a hand-written `for…of`, when the array method is a different algorithm (`reduce` for `sum`); when there is no array method, `native` is the loop.
|
|
128
|
+
- `pnpm check:structure` fails when the bench of an operation has no `direct` or `small` group. `test/bench/functions/sum.bench.ts` is the example.
|
|
129
|
+
- The benches of `test/bench/chains/<scenario>.bench.ts`, and the groups that need other data, use `cases(bench, '<function>/<group>')` directly: the id is also the baseline folder.
|
|
130
|
+
|
|
108
131
|
- Every table needs at least two cases: add a native reference.
|
|
109
132
|
- Import the library and the helpers as namespaces and copy the exports into local constants, as above. Vitest prints a `Benchmark Warning` when a benchmark reads an imported binding too many times.
|
|
110
|
-
- The shared data is in `test/bench/helpers.ts`: `numbers` (100,000 integers), `
|
|
133
|
+
- The shared data is in `test/bench/helpers.ts`: `numbers` (100,000 integers) for every scenario, `small` (1,000 integers) for `<name>/small`, `records` (100,000 objects) only for a key or a selector on objects. The callbacks and values of the scenarios are there too: `double`, `isEven`, `first`, `middle`, `missing`.
|
package/dist/index.d.cts
CHANGED
|
@@ -6,6 +6,22 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export declare type Action<T> = (value: T, index: number) => Unit;
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Lazily yields the values of `iterable`, then `value`.
|
|
11
|
+
* `value` is yielded only when the source ends, so it is never reached on an infinite source.
|
|
12
|
+
* @operation `Transformation`
|
|
13
|
+
* @param iterable - the source `Iterable`
|
|
14
|
+
* @param value - the value yielded after the last value of the source
|
|
15
|
+
* @returns a lazy, re-runnable `Iterable` of the values of the source followed by `value`
|
|
16
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* Array.from(Functions.append([1, 2, 3], 4)); // [1, 2, 3, 4]
|
|
20
|
+
* ```
|
|
21
|
+
* @since 0.8.0
|
|
22
|
+
*/
|
|
23
|
+
declare function append<T>(iterable: Iterable<T>, value: T): Iterable<T>;
|
|
24
|
+
|
|
9
25
|
/**
|
|
10
26
|
* Callback of `forEachAsync`: an async side effect run on each value.
|
|
11
27
|
* @param value - the current value
|
|
@@ -103,6 +119,23 @@ declare type ComparerFunction<T> = (a: T, b: T) => number;
|
|
|
103
119
|
|
|
104
120
|
declare type ComparingProps<T> = keyof T | Array<keyof T>;
|
|
105
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Lazily yields the values of `iterable`, then the values of each iterable in `others`, in order.
|
|
124
|
+
* Each iterable is opened only when the previous one ends, so the iterables after an infinite one are never read.
|
|
125
|
+
* Stopping early closes only the iterable being read.
|
|
126
|
+
* @operation `Transformation`
|
|
127
|
+
* @param iterable - the source `Iterable`
|
|
128
|
+
* @param others - the iterables read after the source
|
|
129
|
+
* @returns a lazy, re-runnable `Iterable` of the values of the source followed by the values of `others`
|
|
130
|
+
* @throws Error if `iterable` or a value of `others` is missing or does not implement `[Symbol.iterator]`
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* Array.from(Functions.concat([1, 2], [3], new Set([4, 5]))); // [1, 2, 3, 4, 5]
|
|
134
|
+
* ```
|
|
135
|
+
* @since 0.8.0
|
|
136
|
+
*/
|
|
137
|
+
declare function concat<T>(iterable: Iterable<T>, ...others: Iterable<T>[]): Iterable<T>;
|
|
138
|
+
|
|
106
139
|
/**
|
|
107
140
|
* Counts the values of `iterable`; reads the whole source.
|
|
108
141
|
* @operation `Action`
|
|
@@ -451,9 +484,11 @@ export declare function fromRange(start: number, end: number, options?: IRangeOp
|
|
|
451
484
|
|
|
452
485
|
declare namespace Functions {
|
|
453
486
|
export {
|
|
487
|
+
append,
|
|
454
488
|
at,
|
|
455
489
|
average,
|
|
456
490
|
collectToArray,
|
|
491
|
+
concat,
|
|
457
492
|
count,
|
|
458
493
|
distinct,
|
|
459
494
|
empty_2 as empty,
|
|
@@ -476,16 +511,21 @@ declare namespace Functions {
|
|
|
476
511
|
memoize,
|
|
477
512
|
getMemoizeDefaultOptions,
|
|
478
513
|
min,
|
|
514
|
+
prepend,
|
|
479
515
|
range,
|
|
480
516
|
reduce,
|
|
481
517
|
repeat_2 as repeat,
|
|
518
|
+
reverse,
|
|
482
519
|
sequenceEqual,
|
|
483
520
|
single,
|
|
484
521
|
skip,
|
|
522
|
+
skipLast,
|
|
485
523
|
skipWhile,
|
|
524
|
+
slice,
|
|
486
525
|
some,
|
|
487
526
|
sum,
|
|
488
527
|
take,
|
|
528
|
+
takeLast,
|
|
489
529
|
takeWhile,
|
|
490
530
|
tap,
|
|
491
531
|
tapChain
|
|
@@ -528,6 +568,19 @@ export declare interface IIterableLinqBase<T> {
|
|
|
528
568
|
* @since 0.0.1
|
|
529
569
|
*/
|
|
530
570
|
[Symbol.iterator](): Iterator<T, any, undefined>;
|
|
571
|
+
/**
|
|
572
|
+
* Yields the values of the chain, then `value`.
|
|
573
|
+
* `value` is yielded only when the source ends, so it is never reached on an infinite chain.
|
|
574
|
+
* @operation `Transformation`
|
|
575
|
+
* @param value - the value yielded after the last value of the chain
|
|
576
|
+
* @returns a new chain with the values of this chain followed by `value`
|
|
577
|
+
* @example
|
|
578
|
+
* ```ts
|
|
579
|
+
* IterableLinq.from([1, 2, 3]).append(4).collectToArray(); // [1, 2, 3, 4]
|
|
580
|
+
* ```
|
|
581
|
+
* @since 0.8.0
|
|
582
|
+
*/
|
|
583
|
+
append(value: T): IIterableLinq<T>;
|
|
531
584
|
/**
|
|
532
585
|
* Returns the value at `index`, like `Array.prototype.at`; a negative index counts from the end.
|
|
533
586
|
* A non-negative index runs the chain up to the value, then closes the source; a negative index runs the whole chain,
|
|
@@ -581,6 +634,21 @@ export declare interface IIterableLinqBase<T> {
|
|
|
581
634
|
* @since 0.0.1
|
|
582
635
|
*/
|
|
583
636
|
collectToArray(): T[];
|
|
637
|
+
/**
|
|
638
|
+
* Yields the values of the chain, then the values of each iterable in `others`, in order.
|
|
639
|
+
* Each iterable is opened only when the previous one ends, so the iterables after an infinite chain are never read.
|
|
640
|
+
* Stopping early closes only the iterable being read.
|
|
641
|
+
* @operation `Transformation`
|
|
642
|
+
* @param others - the iterables read after the chain; other chains are iterables too
|
|
643
|
+
* @returns a new chain with the values of this chain followed by the values of `others`
|
|
644
|
+
* @throws Error if a value of `others` is missing or does not implement `[Symbol.iterator]`
|
|
645
|
+
* @example
|
|
646
|
+
* ```ts
|
|
647
|
+
* IterableLinq.from([1, 2]).concat([3], new Set([4, 5])).collectToArray(); // [1, 2, 3, 4, 5]
|
|
648
|
+
* ```
|
|
649
|
+
* @since 0.8.0
|
|
650
|
+
*/
|
|
651
|
+
concat(...others: Iterable<T>[]): IIterableLinq<T>;
|
|
584
652
|
/**
|
|
585
653
|
* Counts the values of the chain; runs the whole chain.
|
|
586
654
|
* @operation `Action`
|
|
@@ -925,6 +993,19 @@ export declare interface IIterableLinqBase<T> {
|
|
|
925
993
|
* @since 0.0.8
|
|
926
994
|
*/
|
|
927
995
|
min(comparer?: Comparer<T>): T | undefined;
|
|
996
|
+
/**
|
|
997
|
+
* Yields `value`, then the values of the chain.
|
|
998
|
+
* `value` is yielded before the source is read.
|
|
999
|
+
* @operation `Transformation`
|
|
1000
|
+
* @param value - the value yielded before the first value of the chain
|
|
1001
|
+
* @returns a new chain with `value` followed by the values of this chain
|
|
1002
|
+
* @example
|
|
1003
|
+
* ```ts
|
|
1004
|
+
* IterableLinq.from([1, 2, 3]).prepend(0).collectToArray(); // [0, 1, 2, 3]
|
|
1005
|
+
* ```
|
|
1006
|
+
* @since 0.8.0
|
|
1007
|
+
*/
|
|
1008
|
+
prepend(value: T): IIterableLinq<T>;
|
|
928
1009
|
/**
|
|
929
1010
|
* Runs the chain and accumulates its values into a single result, starting from the first value.
|
|
930
1011
|
* @operation `Action`
|
|
@@ -952,6 +1033,19 @@ export declare interface IIterableLinqBase<T> {
|
|
|
952
1033
|
* @since 0.0.10
|
|
953
1034
|
*/
|
|
954
1035
|
reduce<R>(neutralElement: R, reducer: Reducer<T, R>): R;
|
|
1036
|
+
/**
|
|
1037
|
+
* Lazily yields the values in reverse order. Unlike `Array.prototype.reverse`, the source is not changed.
|
|
1038
|
+
* The whole chain runs before the first value is yielded, so `reverse` does not end on an infinite chain.
|
|
1039
|
+
* Every run of the chain reads the source again.
|
|
1040
|
+
* @operation `Transformation`
|
|
1041
|
+
* @returns a lazy, re-runnable chain of the values from the last to the first
|
|
1042
|
+
* @example
|
|
1043
|
+
* ```ts
|
|
1044
|
+
* IterableLinq.from([1, 2, 3]).reverse().collectToArray(); // [3, 2, 1]
|
|
1045
|
+
* ```
|
|
1046
|
+
* @since 0.9.0
|
|
1047
|
+
*/
|
|
1048
|
+
reverse(): IIterableLinq<T>;
|
|
955
1049
|
/**
|
|
956
1050
|
* Tells whether the chain and `other` have the same values in the same order.
|
|
957
1051
|
* It reads the two sources side by side, and stops and closes both at the first difference or when one ends before the other.
|
|
@@ -1025,6 +1119,20 @@ export declare interface IIterableLinqBase<T> {
|
|
|
1025
1119
|
* @since 0.3.0
|
|
1026
1120
|
*/
|
|
1027
1121
|
skip(count: number): IIterableLinq<T>;
|
|
1122
|
+
/**
|
|
1123
|
+
* Lazily yields every value except the last `count`.
|
|
1124
|
+
* A value is yielded once `count` more values have been read, keeping only those `count` values, so `skipLast` works with infinite chains.
|
|
1125
|
+
* @operation `Transformation`
|
|
1126
|
+
* @param count - how many values to leave out at the end; must be a non-negative integer
|
|
1127
|
+
* @returns a lazy, re-runnable chain of the values before the last `count`
|
|
1128
|
+
* @throws Error if `count` is negative, not an integer, `NaN` or `Infinity`
|
|
1129
|
+
* @example
|
|
1130
|
+
* ```ts
|
|
1131
|
+
* IterableLinq.from([1, 2, 3, 4, 5]).skipLast(2).collectToArray(); // [1, 2, 3]
|
|
1132
|
+
* ```
|
|
1133
|
+
* @since 0.9.0
|
|
1134
|
+
*/
|
|
1135
|
+
skipLast(count: number): IIterableLinq<T>;
|
|
1028
1136
|
/**
|
|
1029
1137
|
* Skips the values while `predicate` returns `true`, then yields the first rejected value and all the rest.
|
|
1030
1138
|
* After the first rejected value, `predicate` is not called again.
|
|
@@ -1040,6 +1148,24 @@ export declare interface IIterableLinqBase<T> {
|
|
|
1040
1148
|
* @since 0.5.0
|
|
1041
1149
|
*/
|
|
1042
1150
|
skipWhile(predicate: Predicate<T>): IIterableLinq<T>;
|
|
1151
|
+
/**
|
|
1152
|
+
* Yields the values from `start` to `end` (excluded), like `Array.prototype.slice`; a negative index counts from the end.
|
|
1153
|
+
* With non-negative indexes the values are yielded as they are read, and the source is closed at `end`, so `slice` also ends an infinite chain.
|
|
1154
|
+
* A negative `end` yields each value once `-end` more values have been read, keeping only those `-end` values.
|
|
1155
|
+
* A negative `start` runs the whole chain before yielding, keeping only the last `-start` values.
|
|
1156
|
+
* @operation `Transformation`
|
|
1157
|
+
* @param start - an integer, `0` by default; `-1` is the last value
|
|
1158
|
+
* @param end - an integer; the values are yielded up to the end of the chain by default
|
|
1159
|
+
* @returns a new chain with the values from `start` to `end`
|
|
1160
|
+
* @throws Error if `start` or `end` is given and is not an integer (fractions, `NaN` and `Infinity` included)
|
|
1161
|
+
* @example
|
|
1162
|
+
* ```ts
|
|
1163
|
+
* IterableLinq.from([1, 2, 3, 4, 5]).slice(1, 3).collectToArray(); // [2, 3]
|
|
1164
|
+
* IterableLinq.from([1, 2, 3, 4, 5]).slice(-2).collectToArray(); // [4, 5]
|
|
1165
|
+
* ```
|
|
1166
|
+
* @since 0.8.0
|
|
1167
|
+
*/
|
|
1168
|
+
slice(start?: number, end?: number): IIterableLinq<T>;
|
|
1043
1169
|
/**
|
|
1044
1170
|
* Runs the chain until its first value, then stops and closes the source.
|
|
1045
1171
|
* @operation `Action`
|
|
@@ -1104,6 +1230,20 @@ export declare interface IIterableLinqBase<T> {
|
|
|
1104
1230
|
* @since 0.3.0
|
|
1105
1231
|
*/
|
|
1106
1232
|
take(count: number): IIterableLinq<T>;
|
|
1233
|
+
/**
|
|
1234
|
+
* Lazily yields the last `count` values.
|
|
1235
|
+
* The whole chain runs before the first value is yielded, keeping only the last `count` values, so `takeLast` does not end on an infinite chain.
|
|
1236
|
+
* @operation `Transformation`
|
|
1237
|
+
* @param count - how many values to yield; must be a non-negative integer
|
|
1238
|
+
* @returns a lazy, re-runnable chain of at most `count` values
|
|
1239
|
+
* @throws Error if `count` is negative, not an integer, `NaN` or `Infinity`
|
|
1240
|
+
* @example
|
|
1241
|
+
* ```ts
|
|
1242
|
+
* IterableLinq.from([1, 2, 3, 4, 5]).takeLast(2).collectToArray(); // [4, 5]
|
|
1243
|
+
* ```
|
|
1244
|
+
* @since 0.9.0
|
|
1245
|
+
*/
|
|
1246
|
+
takeLast(count: number): IIterableLinq<T>;
|
|
1107
1247
|
/**
|
|
1108
1248
|
* Yields the values while a type guard accepts them, narrowing their type, then closes the source.
|
|
1109
1249
|
* The source is never read past the first rejected value, which is not yielded.
|
|
@@ -1408,6 +1548,22 @@ export declare function override<K extends Extract<keyof IIterableLinq<unknown>,
|
|
|
1408
1548
|
*/
|
|
1409
1549
|
export declare type Predicate<T> = (value: T, index: number) => boolean;
|
|
1410
1550
|
|
|
1551
|
+
/**
|
|
1552
|
+
* Lazily yields `value`, then the values of `iterable`.
|
|
1553
|
+
* `value` is yielded before the source is read.
|
|
1554
|
+
* @operation `Transformation`
|
|
1555
|
+
* @param iterable - the source `Iterable`
|
|
1556
|
+
* @param value - the value yielded before the first value of the source
|
|
1557
|
+
* @returns a lazy, re-runnable `Iterable` of `value` followed by the values of the source
|
|
1558
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
1559
|
+
* @example
|
|
1560
|
+
* ```ts
|
|
1561
|
+
* Array.from(Functions.prepend([1, 2, 3], 0)); // [0, 1, 2, 3]
|
|
1562
|
+
* ```
|
|
1563
|
+
* @since 0.8.0
|
|
1564
|
+
*/
|
|
1565
|
+
declare function prepend<T>(iterable: Iterable<T>, value: T): Iterable<T>;
|
|
1566
|
+
|
|
1411
1567
|
/**
|
|
1412
1568
|
* Returns the numbers from 0 up to, but not including, `end`, computed as `index * step`.
|
|
1413
1569
|
* The direction follows the sign of `end`; `reverse` yields the same numbers backwards; a `NaN` bound gives an empty `Iterable`.
|
|
@@ -1513,6 +1669,22 @@ export declare function repeat<T>(value: T, count: number): IIterableLinq<T>;
|
|
|
1513
1669
|
*/
|
|
1514
1670
|
declare function repeat_2<T>(value: T, count: number): Iterable<T>;
|
|
1515
1671
|
|
|
1672
|
+
/**
|
|
1673
|
+
* Lazily yields the values in reverse order. Unlike `Array.prototype.reverse`, the source is not changed.
|
|
1674
|
+
* The whole source is read before the first value is yielded, so `reverse` does not end on an infinite source.
|
|
1675
|
+
* Every iteration reads the source again.
|
|
1676
|
+
* @operation `Transformation`
|
|
1677
|
+
* @param iterable - the source `Iterable`
|
|
1678
|
+
* @returns a lazy, re-runnable `Iterable` of the values from the last to the first
|
|
1679
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
|
|
1680
|
+
* @example
|
|
1681
|
+
* ```ts
|
|
1682
|
+
* Array.from(Functions.reverse([1, 2, 3])); // [3, 2, 1]
|
|
1683
|
+
* ```
|
|
1684
|
+
* @since 0.9.0
|
|
1685
|
+
*/
|
|
1686
|
+
declare function reverse<T>(iterable: Iterable<T>): Iterable<T>;
|
|
1687
|
+
|
|
1516
1688
|
/**
|
|
1517
1689
|
* Tells whether `iterable` and `other` have the same values in the same order.
|
|
1518
1690
|
* It reads the two sources side by side, and stops and closes both at the first difference or when one ends before the other.
|
|
@@ -1599,6 +1771,22 @@ declare function single<T>(iterable: Iterable<T>, predicate: Predicate<T> | unde
|
|
|
1599
1771
|
*/
|
|
1600
1772
|
declare function skip<T>(iterable: Iterable<T>, count: number): Iterable<T>;
|
|
1601
1773
|
|
|
1774
|
+
/**
|
|
1775
|
+
* Lazily yields every value except the last `count`.
|
|
1776
|
+
* A value is yielded once `count` more values have been read, keeping only those `count` values, so `skipLast` works with infinite sources.
|
|
1777
|
+
* @operation `Transformation`
|
|
1778
|
+
* @param iterable - the source `Iterable`
|
|
1779
|
+
* @param count - how many values to leave out at the end; must be a non-negative integer
|
|
1780
|
+
* @returns a lazy, re-runnable `Iterable` of the values before the last `count`
|
|
1781
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `count` is negative, not an integer, `NaN` or `Infinity`
|
|
1782
|
+
* @example
|
|
1783
|
+
* ```ts
|
|
1784
|
+
* Array.from(Functions.skipLast([1, 2, 3, 4, 5], 2)); // [1, 2, 3]
|
|
1785
|
+
* ```
|
|
1786
|
+
* @since 0.9.0
|
|
1787
|
+
*/
|
|
1788
|
+
declare function skipLast<T>(iterable: Iterable<T>, count: number): Iterable<T>;
|
|
1789
|
+
|
|
1602
1790
|
/**
|
|
1603
1791
|
* Lazily skips the values while `predicate` returns `true`, then yields the first rejected value and all the rest.
|
|
1604
1792
|
* After the first rejected value, `predicate` is not called again.
|
|
@@ -1616,6 +1804,26 @@ declare function skip<T>(iterable: Iterable<T>, count: number): Iterable<T>;
|
|
|
1616
1804
|
*/
|
|
1617
1805
|
declare function skipWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
|
|
1618
1806
|
|
|
1807
|
+
/**
|
|
1808
|
+
* Lazily yields the values from `start` to `end` (excluded), like `Array.prototype.slice`; a negative index counts from the end.
|
|
1809
|
+
* With non-negative indexes the values are yielded as they are read, and the source is closed at `end`, so `slice` also ends an infinite source.
|
|
1810
|
+
* A negative `end` yields each value once `-end` more values have been read, keeping only those `-end` values.
|
|
1811
|
+
* A negative `start` reads the whole source before yielding, keeping only the last `-start` values.
|
|
1812
|
+
* @operation `Transformation`
|
|
1813
|
+
* @param iterable - the source `Iterable`
|
|
1814
|
+
* @param start - an integer, `0` by default; `-1` is the last value
|
|
1815
|
+
* @param end - an integer; the values are yielded up to the end of the source by default
|
|
1816
|
+
* @returns a lazy, re-runnable `Iterable` of the values from `start` to `end`
|
|
1817
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `start` or `end` is given and is not an integer (fractions, `NaN` and `Infinity` included)
|
|
1818
|
+
* @example
|
|
1819
|
+
* ```ts
|
|
1820
|
+
* Array.from(Functions.slice([1, 2, 3, 4, 5], 1, 3)); // [2, 3]
|
|
1821
|
+
* Array.from(Functions.slice([1, 2, 3, 4, 5], -2)); // [4, 5]
|
|
1822
|
+
* ```
|
|
1823
|
+
* @since 0.8.0
|
|
1824
|
+
*/
|
|
1825
|
+
declare function slice<T>(iterable: Iterable<T>, start?: number, end?: number): Iterable<T>;
|
|
1826
|
+
|
|
1619
1827
|
/**
|
|
1620
1828
|
* Tells whether `iterable` contains a value; reads one value, then closes the source.
|
|
1621
1829
|
* @operation `Action`
|
|
@@ -1692,6 +1900,22 @@ declare function sum<T>(iterable: Iterable<T>, selector: Mapper<T, number> | und
|
|
|
1692
1900
|
*/
|
|
1693
1901
|
declare function take<T>(iterable: Iterable<T>, count: number): Iterable<T>;
|
|
1694
1902
|
|
|
1903
|
+
/**
|
|
1904
|
+
* Lazily yields the last `count` values.
|
|
1905
|
+
* The whole source is read before the first value is yielded, keeping only the last `count` values, so `takeLast` does not end on an infinite source.
|
|
1906
|
+
* @operation `Transformation`
|
|
1907
|
+
* @param iterable - the source `Iterable`
|
|
1908
|
+
* @param count - how many values to yield; must be a non-negative integer
|
|
1909
|
+
* @returns a lazy, re-runnable `Iterable` of at most `count` values
|
|
1910
|
+
* @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if `count` is negative, not an integer, `NaN` or `Infinity`
|
|
1911
|
+
* @example
|
|
1912
|
+
* ```ts
|
|
1913
|
+
* Array.from(Functions.takeLast([1, 2, 3, 4, 5], 2)); // [4, 5]
|
|
1914
|
+
* ```
|
|
1915
|
+
* @since 0.9.0
|
|
1916
|
+
*/
|
|
1917
|
+
declare function takeLast<T>(iterable: Iterable<T>, count: number): Iterable<T>;
|
|
1918
|
+
|
|
1695
1919
|
/**
|
|
1696
1920
|
* Lazily yields the values while a type guard accepts them, narrowing their type, then closes the source.
|
|
1697
1921
|
* The source is never read past the first rejected value, which is not yielded.
|