iterable-linq-utility 0.6.0 → 0.8.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/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
@@ -33,6 +49,37 @@ export declare type AsyncAction<T> = (value: T, index: number) => Promise<Unit>;
33
49
  */
34
50
  declare function at<T>(iterable: Iterable<T>, index: number): T | undefined;
35
51
 
52
+ /**
53
+ * Returns the average of the values of `iterable`: their sum with `+` divided by their number; reads the whole source.
54
+ * The values are not checked: `NaN` makes the result `NaN`.
55
+ * @operation `Action`
56
+ * @param iterable - the source `Iterable` of numbers
57
+ * @returns the average of the values, `undefined` when `iterable` is empty
58
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
59
+ * @example
60
+ * ```ts
61
+ * Functions.average([1, 2, 3, 4]); // 2.5
62
+ * ```
63
+ * @since 0.7.0
64
+ */
65
+ declare function average(iterable: Iterable<number>): number | undefined;
66
+
67
+ /**
68
+ * Returns the average of the numbers returned by `selector` for each value; reads the whole source.
69
+ * If `selector` throws, the source is closed and the error propagates.
70
+ * @operation `Action`
71
+ * @param iterable - the source `Iterable`
72
+ * @param selector - called with each value and its index, returns the number to average; `undefined` averages the values themselves
73
+ * @returns the average of the selected numbers, `undefined` when `iterable` is empty
74
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if a provided `selector` is not a function
75
+ * @example
76
+ * ```ts
77
+ * Functions.average(['a', 'bb', 'ccc'], v => v.length); // 2
78
+ * ```
79
+ * @since 0.7.0
80
+ */
81
+ declare function average<T>(iterable: Iterable<T>, selector: Mapper<T, number> | undefined): number | undefined;
82
+
36
83
  /**
37
84
  * Implementation of a method added with `extend` or `override`. `this` is the chain the method is called on.
38
85
  * @since 0.1.0
@@ -72,6 +119,23 @@ declare type ComparerFunction<T> = (a: T, b: T) => number;
72
119
 
73
120
  declare type ComparingProps<T> = keyof T | Array<keyof T>;
74
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
+
75
139
  /**
76
140
  * Counts the values of `iterable`; reads the whole source.
77
141
  * @operation `Action`
@@ -420,8 +484,11 @@ export declare function fromRange(start: number, end: number, options?: IRangeOp
420
484
 
421
485
  declare namespace Functions {
422
486
  export {
487
+ append,
423
488
  at,
489
+ average,
424
490
  collectToArray,
491
+ concat,
425
492
  count,
426
493
  distinct,
427
494
  empty_2 as empty,
@@ -436,6 +503,7 @@ declare namespace Functions {
436
503
  forEachAsync,
437
504
  includes,
438
505
  indexOf,
506
+ join,
439
507
  lastIndexOf,
440
508
  map,
441
509
  materialize,
@@ -443,12 +511,17 @@ declare namespace Functions {
443
511
  memoize,
444
512
  getMemoizeDefaultOptions,
445
513
  min,
514
+ prepend,
446
515
  range,
447
516
  reduce,
448
517
  repeat_2 as repeat,
518
+ sequenceEqual,
519
+ single,
449
520
  skip,
450
521
  skipWhile,
522
+ slice,
451
523
  some,
524
+ sum,
452
525
  take,
453
526
  takeWhile,
454
527
  tap,
@@ -492,6 +565,19 @@ export declare interface IIterableLinqBase<T> {
492
565
  * @since 0.0.1
493
566
  */
494
567
  [Symbol.iterator](): Iterator<T, any, undefined>;
568
+ /**
569
+ * Yields the values of the chain, then `value`.
570
+ * `value` is yielded only when the source ends, so it is never reached on an infinite chain.
571
+ * @operation `Transformation`
572
+ * @param value - the value yielded after the last value of the chain
573
+ * @returns a new chain with the values of this chain followed by `value`
574
+ * @example
575
+ * ```ts
576
+ * IterableLinq.from([1, 2, 3]).append(4).collectToArray(); // [1, 2, 3, 4]
577
+ * ```
578
+ * @since 0.8.0
579
+ */
580
+ append(value: T): IIterableLinq<T>;
495
581
  /**
496
582
  * Returns the value at `index`, like `Array.prototype.at`; a negative index counts from the end.
497
583
  * A non-negative index runs the chain up to the value, then closes the source; a negative index runs the whole chain,
@@ -508,6 +594,32 @@ export declare interface IIterableLinqBase<T> {
508
594
  * @since 0.6.0
509
595
  */
510
596
  at(index: number): T | undefined;
597
+ /**
598
+ * Returns the average of the values of a chain of numbers: their sum with `+` divided by their number; runs the whole chain.
599
+ * The values are not checked: `NaN` makes the result `NaN`.
600
+ * @operation `Action`
601
+ * @returns the average of the values, `undefined` when the chain is empty
602
+ * @example
603
+ * ```ts
604
+ * IterableLinq.from([1, 2, 3, 4]).average(); // 2.5
605
+ * ```
606
+ * @since 0.7.0
607
+ */
608
+ average(this: IIterableLinqBase<number>): number | undefined;
609
+ /**
610
+ * Returns the average of the numbers returned by `selector` for each value; runs the whole chain.
611
+ * If `selector` throws, the source is closed and the error propagates.
612
+ * @operation `Action`
613
+ * @param selector - called with each value and its index, returns the number to average; `undefined` averages the values themselves
614
+ * @returns the average of the selected numbers, `undefined` when the chain is empty
615
+ * @throws Error if a provided `selector` is not a function
616
+ * @example
617
+ * ```ts
618
+ * IterableLinq.from(['a', 'bb', 'ccc']).average(v => v.length); // 2
619
+ * ```
620
+ * @since 0.7.0
621
+ */
622
+ average(selector: Mapper<T, number> | undefined): number | undefined;
511
623
  /**
512
624
  * Runs the chain and collects its values into an `Array`.
513
625
  * @operation `Action`
@@ -519,6 +631,21 @@ export declare interface IIterableLinqBase<T> {
519
631
  * @since 0.0.1
520
632
  */
521
633
  collectToArray(): T[];
634
+ /**
635
+ * Yields the values of the chain, then the values of each iterable in `others`, in order.
636
+ * Each iterable is opened only when the previous one ends, so the iterables after an infinite chain are never read.
637
+ * Stopping early closes only the iterable being read.
638
+ * @operation `Transformation`
639
+ * @param others - the iterables read after the chain; other chains are iterables too
640
+ * @returns a new chain with the values of this chain followed by the values of `others`
641
+ * @throws Error if a value of `others` is missing or does not implement `[Symbol.iterator]`
642
+ * @example
643
+ * ```ts
644
+ * IterableLinq.from([1, 2]).concat([3], new Set([4, 5])).collectToArray(); // [1, 2, 3, 4, 5]
645
+ * ```
646
+ * @since 0.8.0
647
+ */
648
+ concat(...others: Iterable<T>[]): IIterableLinq<T>;
522
649
  /**
523
650
  * Counts the values of the chain; runs the whole chain.
524
651
  * @operation `Action`
@@ -762,6 +889,20 @@ export declare interface IIterableLinqBase<T> {
762
889
  * @since 0.6.0
763
890
  */
764
891
  indexOf(value: T): number;
892
+ /**
893
+ * Joins the values of the chain in a string, like `Array.prototype.join`; runs the whole chain.
894
+ * `null` and `undefined` become empty strings, every other value is converted with its `toString`.
895
+ * @operation `Action`
896
+ * @param separator - the string between two values; defaults to `,`
897
+ * @returns the joined values, `''` when the chain is empty
898
+ * @example
899
+ * ```ts
900
+ * IterableLinq.from([1, 2, 3]).join(); // '1,2,3'
901
+ * IterableLinq.from(['a', 'b']).join(' - '); // 'a - b'
902
+ * ```
903
+ * @since 0.7.0
904
+ */
905
+ join(separator?: string): string;
765
906
  /**
766
907
  * Returns the index of the last value strictly equal (`===`) to `value`, like `Array.prototype.lastIndexOf`;
767
908
  * runs the whole chain.
@@ -849,6 +990,19 @@ export declare interface IIterableLinqBase<T> {
849
990
  * @since 0.0.8
850
991
  */
851
992
  min(comparer?: Comparer<T>): T | undefined;
993
+ /**
994
+ * Yields `value`, then the values of the chain.
995
+ * `value` is yielded before the source is read.
996
+ * @operation `Transformation`
997
+ * @param value - the value yielded before the first value of the chain
998
+ * @returns a new chain with `value` followed by the values of this chain
999
+ * @example
1000
+ * ```ts
1001
+ * IterableLinq.from([1, 2, 3]).prepend(0).collectToArray(); // [0, 1, 2, 3]
1002
+ * ```
1003
+ * @since 0.8.0
1004
+ */
1005
+ prepend(value: T): IIterableLinq<T>;
852
1006
  /**
853
1007
  * Runs the chain and accumulates its values into a single result, starting from the first value.
854
1008
  * @operation `Action`
@@ -876,6 +1030,66 @@ export declare interface IIterableLinqBase<T> {
876
1030
  * @since 0.0.10
877
1031
  */
878
1032
  reduce<R>(neutralElement: R, reducer: Reducer<T, R>): R;
1033
+ /**
1034
+ * Tells whether the chain and `other` have the same values in the same order.
1035
+ * It reads the two sources side by side, and stops and closes both at the first difference or when one ends before the other.
1036
+ * If `equals` throws, both sources are closed and the error propagates; if a source throws, the other one is closed.
1037
+ * @operation `Action`
1038
+ * @param other - the `Iterable` to compare with, for example another chain
1039
+ * @param equals - called with a value of the chain and the value of `other` at the same position; defaults to `===`
1040
+ * @returns `true` if the two sources have the same number of values and every pair is equal
1041
+ * @throws Error if `other` is missing or does not implement `[Symbol.iterator]`, or if a provided `equals` is not a function
1042
+ * @example
1043
+ * ```ts
1044
+ * IterableLinq.from([1, 2, 3]).sequenceEqual([1, 2, 3]); // true
1045
+ * IterableLinq.from(['a', 'bb']).sequenceEqual(['x', 'yy'], (a, b) => a.length === b.length); // true
1046
+ * ```
1047
+ * @since 0.7.0
1048
+ */
1049
+ sequenceEqual(other: Iterable<T>, equals?: (a: T, b: T) => boolean): boolean;
1050
+ /**
1051
+ * Returns the only value of the chain, `undefined` if it is empty; throws if it has more than one.
1052
+ * It stops and closes the source at the second value, so it also ends an infinite chain.
1053
+ * @operation `Action`
1054
+ * @returns the only value, or `undefined` when the chain is empty
1055
+ * @throws Error if the chain contains more than one value
1056
+ * @example
1057
+ * ```ts
1058
+ * IterableLinq.from([5]).single(); // 5
1059
+ * IterableLinq.from([1, 2]).single(); // throws
1060
+ * ```
1061
+ * @since 0.7.0
1062
+ */
1063
+ single(): T | undefined;
1064
+ /**
1065
+ * Returns the only value accepted by a type guard, narrowing its type, `undefined` if there is none; throws if there is more than one.
1066
+ * It stops and closes the source at the second match. If `predicate` throws, the source is closed and the error propagates.
1067
+ * @operation `Action`
1068
+ * @param predicate - a type guard called with each value and its index
1069
+ * @returns the only value accepted by `predicate`, or `undefined` if there is none
1070
+ * @throws Error if `predicate` is not a function, or if more than one value satisfies it
1071
+ * @example
1072
+ * ```ts
1073
+ * const values: (number | string)[] = [1, 'two', 3];
1074
+ * IterableLinq.from(values).single((v): v is string => typeof v === 'string'); // string | undefined, 'two'
1075
+ * ```
1076
+ * @since 0.7.0
1077
+ */
1078
+ single<S extends T>(predicate: (value: T, index: number) => value is S): S | undefined;
1079
+ /**
1080
+ * Returns the only value that satisfies `predicate`, `undefined` if there is none; throws if there is more than one.
1081
+ * It stops and closes the source at the second match. If `predicate` throws, the source is closed and the error propagates.
1082
+ * @operation `Action`
1083
+ * @param predicate - called with each value and its index; `undefined` looks for the only value of the chain
1084
+ * @returns the only value that satisfies `predicate`, or `undefined` if there is none
1085
+ * @throws Error if a provided `predicate` is not a function, or if more than one value satisfies it
1086
+ * @example
1087
+ * ```ts
1088
+ * IterableLinq.from([1, 5, 2]).single(v => v > 4); // 5
1089
+ * ```
1090
+ * @since 0.7.0
1091
+ */
1092
+ single(predicate: Predicate<T> | undefined): T | undefined;
879
1093
  /**
880
1094
  * Lazily skips the first `count` values and yields the rest.
881
1095
  * @operation `Transformation`
@@ -904,6 +1118,24 @@ export declare interface IIterableLinqBase<T> {
904
1118
  * @since 0.5.0
905
1119
  */
906
1120
  skipWhile(predicate: Predicate<T>): IIterableLinq<T>;
1121
+ /**
1122
+ * Yields the values from `start` to `end` (excluded), like `Array.prototype.slice`; a negative index counts from the end.
1123
+ * 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.
1124
+ * A negative `end` yields each value once `-end` more values have been read, keeping only those `-end` values.
1125
+ * A negative `start` runs the whole chain before yielding, keeping only the last `-start` values.
1126
+ * @operation `Transformation`
1127
+ * @param start - an integer, `0` by default; `-1` is the last value
1128
+ * @param end - an integer; the values are yielded up to the end of the chain by default
1129
+ * @returns a new chain with the values from `start` to `end`
1130
+ * @throws Error if `start` or `end` is given and is not an integer (fractions, `NaN` and `Infinity` included)
1131
+ * @example
1132
+ * ```ts
1133
+ * IterableLinq.from([1, 2, 3, 4, 5]).slice(1, 3).collectToArray(); // [2, 3]
1134
+ * IterableLinq.from([1, 2, 3, 4, 5]).slice(-2).collectToArray(); // [4, 5]
1135
+ * ```
1136
+ * @since 0.8.0
1137
+ */
1138
+ slice(start?: number, end?: number): IIterableLinq<T>;
907
1139
  /**
908
1140
  * Runs the chain until its first value, then stops and closes the source.
909
1141
  * @operation `Action`
@@ -928,6 +1160,32 @@ export declare interface IIterableLinqBase<T> {
928
1160
  * @since 0.0.1
929
1161
  */
930
1162
  some(predicate: Predicate<T> | undefined): boolean;
1163
+ /**
1164
+ * Sums the values of a chain of numbers with `+`; runs the whole chain.
1165
+ * The values are not checked: `NaN` makes the result `NaN`.
1166
+ * @operation `Action`
1167
+ * @returns the sum of the values, `0` when the chain is empty
1168
+ * @example
1169
+ * ```ts
1170
+ * IterableLinq.from([1, 2, 3]).sum(); // 6
1171
+ * ```
1172
+ * @since 0.7.0
1173
+ */
1174
+ sum(this: IIterableLinqBase<number>): number;
1175
+ /**
1176
+ * Sums the numbers returned by `selector` for each value; runs the whole chain.
1177
+ * If `selector` throws, the source is closed and the error propagates.
1178
+ * @operation `Action`
1179
+ * @param selector - called with each value and its index, returns the number to add; `undefined` sums the values themselves
1180
+ * @returns the sum of the selected numbers, `0` when the chain is empty
1181
+ * @throws Error if a provided `selector` is not a function
1182
+ * @example
1183
+ * ```ts
1184
+ * IterableLinq.from(['a', 'bb', 'ccc']).sum(v => v.length); // 6
1185
+ * ```
1186
+ * @since 0.7.0
1187
+ */
1188
+ sum(selector: Mapper<T, number> | undefined): number;
931
1189
  /**
932
1190
  * Yields the first `count` values, then closes the source.
933
1191
  * The source is never read past the `count`-th value, so `take` also ends an infinite chain.
@@ -1094,6 +1352,23 @@ export declare interface IRangeOptions {
1094
1352
  */
1095
1353
  export declare function isIterableLinq(value: unknown): value is IIterableLinq<unknown>;
1096
1354
 
1355
+ /**
1356
+ * Joins the values of `iterable` in a string, like `Array.prototype.join`; reads the whole source.
1357
+ * `null` and `undefined` become empty strings, every other value is converted with its `toString`.
1358
+ * @operation `Action`
1359
+ * @param iterable - the source `Iterable`
1360
+ * @param separator - the string between two values; defaults to `,`
1361
+ * @returns the joined values, `''` when `iterable` is empty
1362
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
1363
+ * @example
1364
+ * ```ts
1365
+ * Functions.join([1, 2, 3]); // '1,2,3'
1366
+ * Functions.join(['a', 'b'], ' - '); // 'a - b'
1367
+ * ```
1368
+ * @since 0.7.0
1369
+ */
1370
+ declare function join<T>(iterable: Iterable<T>, separator?: string): string;
1371
+
1097
1372
  /**
1098
1373
  * Returns the index of the last value strictly equal (`===`) to `value`, like `Array.prototype.lastIndexOf`;
1099
1374
  * reads the whole source.
@@ -1229,6 +1504,22 @@ export declare function override<K extends Extract<keyof IIterableLinq<unknown>,
1229
1504
  */
1230
1505
  export declare type Predicate<T> = (value: T, index: number) => boolean;
1231
1506
 
1507
+ /**
1508
+ * Lazily yields `value`, then the values of `iterable`.
1509
+ * `value` is yielded before the source is read.
1510
+ * @operation `Transformation`
1511
+ * @param iterable - the source `Iterable`
1512
+ * @param value - the value yielded before the first value of the source
1513
+ * @returns a lazy, re-runnable `Iterable` of `value` followed by the values of the source
1514
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
1515
+ * @example
1516
+ * ```ts
1517
+ * Array.from(Functions.prepend([1, 2, 3], 0)); // [0, 1, 2, 3]
1518
+ * ```
1519
+ * @since 0.8.0
1520
+ */
1521
+ declare function prepend<T>(iterable: Iterable<T>, value: T): Iterable<T>;
1522
+
1232
1523
  /**
1233
1524
  * Returns the numbers from 0 up to, but not including, `end`, computed as `index * step`.
1234
1525
  * The direction follows the sign of `end`; `reverse` yields the same numbers backwards; a `NaN` bound gives an empty `Iterable`.
@@ -1334,6 +1625,77 @@ export declare function repeat<T>(value: T, count: number): IIterableLinq<T>;
1334
1625
  */
1335
1626
  declare function repeat_2<T>(value: T, count: number): Iterable<T>;
1336
1627
 
1628
+ /**
1629
+ * Tells whether `iterable` and `other` have the same values in the same order.
1630
+ * It reads the two sources side by side, and stops and closes both at the first difference or when one ends before the other.
1631
+ * If `equals` throws, both sources are closed and the error propagates; if a source throws, the other one is closed.
1632
+ * @operation `Action`
1633
+ * @param iterable - the source `Iterable`
1634
+ * @param other - the `Iterable` to compare with
1635
+ * @param equals - called with a value of `iterable` and the value of `other` at the same position; defaults to `===`
1636
+ * @returns `true` if the two sources have the same number of values and every pair is equal
1637
+ * @throws Error if `iterable` or `other` is missing or does not implement `[Symbol.iterator]`, or if a provided `equals` is not a function
1638
+ * @example
1639
+ * ```ts
1640
+ * Functions.sequenceEqual([1, 2, 3], [1, 2, 3]); // true
1641
+ * Functions.sequenceEqual([1, 2], [1, 2, 3]); // false
1642
+ * Functions.sequenceEqual([{ id: 1 }], [{ id: 1 }], (a, b) => a.id === b.id); // true
1643
+ * ```
1644
+ * @since 0.7.0
1645
+ */
1646
+ declare function sequenceEqual<T>(iterable: Iterable<T>, other: Iterable<T>, equals?: (a: T, b: T) => boolean): boolean;
1647
+
1648
+ /**
1649
+ * Returns the only value of `iterable`, `undefined` if it is empty; throws if it has more than one.
1650
+ * It stops and closes the source at the second value, so it also ends on an infinite source.
1651
+ * @operation `Action`
1652
+ * @param iterable - the source `Iterable`
1653
+ * @returns the only value, or `undefined` when `iterable` is empty
1654
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if it contains more than one value
1655
+ * @example
1656
+ * ```ts
1657
+ * Functions.single([5]); // 5
1658
+ * Functions.single([]); // undefined
1659
+ * Functions.single([1, 2]); // throws
1660
+ * ```
1661
+ * @since 0.7.0
1662
+ */
1663
+ declare function single<T>(iterable: Iterable<T>): T | undefined;
1664
+
1665
+ /**
1666
+ * Returns the only value accepted by a type guard, narrowing its type, `undefined` if there is none; throws if there is more than one.
1667
+ * It stops and closes the source at the second match. If `predicate` throws, the source is closed and the error propagates.
1668
+ * @operation `Action`
1669
+ * @param iterable - the source `Iterable`
1670
+ * @param predicate - a type guard called with each value and its index
1671
+ * @returns the only value accepted by `predicate`, or `undefined` if there is none
1672
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, if `predicate` is not a function, or if more than one value satisfies it
1673
+ * @example
1674
+ * ```ts
1675
+ * const values: (number | string)[] = [1, 'two', 3];
1676
+ * Functions.single(values, (v): v is string => typeof v === 'string'); // string | undefined, 'two'
1677
+ * ```
1678
+ * @since 0.7.0
1679
+ */
1680
+ declare function single<T, S extends T>(iterable: Iterable<T>, predicate: (value: T, index: number) => value is S): S | undefined;
1681
+
1682
+ /**
1683
+ * Returns the only value that satisfies `predicate`, `undefined` if there is none; throws if there is more than one.
1684
+ * It stops and closes the source at the second match. If `predicate` throws, the source is closed and the error propagates.
1685
+ * @operation `Action`
1686
+ * @param iterable - the source `Iterable`
1687
+ * @param predicate - called with each value and its index; `undefined` looks for the only value of `iterable`
1688
+ * @returns the only value that satisfies `predicate`, or `undefined` if there is none
1689
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, if a provided `predicate` is not a function, or if more than one value satisfies it
1690
+ * @example
1691
+ * ```ts
1692
+ * Functions.single([1, 5, 2], v => v > 4); // 5
1693
+ * Functions.single([5, 6], v => v > 4); // throws
1694
+ * ```
1695
+ * @since 0.7.0
1696
+ */
1697
+ declare function single<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefined): T | undefined;
1698
+
1337
1699
  /**
1338
1700
  * Lazily skips the first `count` values and yields the rest.
1339
1701
  * @operation `Transformation`
@@ -1366,6 +1728,26 @@ declare function skip<T>(iterable: Iterable<T>, count: number): Iterable<T>;
1366
1728
  */
1367
1729
  declare function skipWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T>;
1368
1730
 
1731
+ /**
1732
+ * Lazily yields the values from `start` to `end` (excluded), like `Array.prototype.slice`; a negative index counts from the end.
1733
+ * 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.
1734
+ * A negative `end` yields each value once `-end` more values have been read, keeping only those `-end` values.
1735
+ * A negative `start` reads the whole source before yielding, keeping only the last `-start` values.
1736
+ * @operation `Transformation`
1737
+ * @param iterable - the source `Iterable`
1738
+ * @param start - an integer, `0` by default; `-1` is the last value
1739
+ * @param end - an integer; the values are yielded up to the end of the source by default
1740
+ * @returns a lazy, re-runnable `Iterable` of the values from `start` to `end`
1741
+ * @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)
1742
+ * @example
1743
+ * ```ts
1744
+ * Array.from(Functions.slice([1, 2, 3, 4, 5], 1, 3)); // [2, 3]
1745
+ * Array.from(Functions.slice([1, 2, 3, 4, 5], -2)); // [4, 5]
1746
+ * ```
1747
+ * @since 0.8.0
1748
+ */
1749
+ declare function slice<T>(iterable: Iterable<T>, start?: number, end?: number): Iterable<T>;
1750
+
1369
1751
  /**
1370
1752
  * Tells whether `iterable` contains a value; reads one value, then closes the source.
1371
1753
  * @operation `Action`
@@ -1395,6 +1777,37 @@ declare function some<T>(iterable: Iterable<T>): boolean;
1395
1777
  */
1396
1778
  declare function some<T>(iterable: Iterable<T>, predicate: Predicate<T> | undefined): boolean;
1397
1779
 
1780
+ /**
1781
+ * Sums the values of `iterable` with `+`; reads the whole source.
1782
+ * The values are not checked: `NaN` makes the result `NaN`.
1783
+ * @operation `Action`
1784
+ * @param iterable - the source `Iterable` of numbers
1785
+ * @returns the sum of the values, `0` when `iterable` is empty
1786
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`
1787
+ * @example
1788
+ * ```ts
1789
+ * Functions.sum([1, 2, 3]); // 6
1790
+ * ```
1791
+ * @since 0.7.0
1792
+ */
1793
+ declare function sum(iterable: Iterable<number>): number;
1794
+
1795
+ /**
1796
+ * Sums the numbers returned by `selector` for each value; reads the whole source.
1797
+ * If `selector` throws, the source is closed and the error propagates.
1798
+ * @operation `Action`
1799
+ * @param iterable - the source `Iterable`
1800
+ * @param selector - called with each value and its index, returns the number to add; `undefined` sums the values themselves
1801
+ * @returns the sum of the selected numbers, `0` when `iterable` is empty
1802
+ * @throws Error if `iterable` is missing or does not implement `[Symbol.iterator]`, or if a provided `selector` is not a function
1803
+ * @example
1804
+ * ```ts
1805
+ * Functions.sum(['a', 'bb', 'ccc'], v => v.length); // 6
1806
+ * ```
1807
+ * @since 0.7.0
1808
+ */
1809
+ declare function sum<T>(iterable: Iterable<T>, selector: Mapper<T, number> | undefined): number;
1810
+
1398
1811
  /**
1399
1812
  * Lazily yields the first `count` values, then closes the source.
1400
1813
  * The source is never read past the `count`-th value, so `take` also ends an infinite source.