feat(collections): switch functions to take iterables when possible (#3401)

This commit is contained in:
Un Known 2023-05-30 19:48:21 -04:00 committed by GitHub
parent 64a0d90682
commit 5199824fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 40 additions and 38 deletions

View File

@ -26,7 +26,7 @@
* ```
*/
export function associateBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => string,
): Record<string, T> {
const ret: Record<string, T> = {};

View File

@ -22,7 +22,7 @@
* ```
*/
export function associateWith<T>(
array: readonly string[],
array: Iterable<string>,
selector: (key: string) => T,
): Record<string, T> {
const ret: Record<string, T> = {};

View File

@ -16,7 +16,7 @@
* assertEquals(distinctNumbers, [3, 2, 5]);
* ```
*/
export function distinct<T>(array: readonly T[]): T[] {
export function distinct<T>(array: Iterable<T>): T[] {
const set = new Set(array);
return Array.from(set);

View File

@ -17,7 +17,7 @@
* ```
*/
export function distinctBy<T, D>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => D,
): T[] {
const selectedValues = new Set<D>();

View File

@ -23,7 +23,7 @@
* ```
*/
export function findSingle<T>(
array: readonly T[],
array: Iterable<T>,
predicate: (el: T) => boolean,
): T | undefined {
let match: T | undefined = undefined;

View File

@ -22,7 +22,7 @@
* ```
*/
export function firstNotNullishOf<T, O>(
array: readonly T[],
array: Iterable<T>,
selector: (item: T) => O | undefined | null,
): NonNullable<O> | undefined {
for (const current of array) {

View File

@ -43,7 +43,7 @@ export type JoinToStringOptions = {
* ```
*/
export function joinToString<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => string,
{
separator = ",",
@ -56,8 +56,8 @@ export function joinToString<T>(
let result = "";
let index = -1;
while (++index < array.length) {
const el = array[index];
for (const el of array) {
index++;
if (index > 0) {
result += separator;

View File

@ -23,7 +23,7 @@
* ```
*/
export function mapNotNullish<T, O>(
array: readonly T[],
array: Iterable<T>,
transformer: (el: T) => O,
): NonNullable<O>[] {
const ret: NonNullable<O>[] = [];

View File

@ -22,23 +22,23 @@
* ```
*/
export function maxBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => number,
): T | undefined;
export function maxBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => string,
): T | undefined;
export function maxBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => bigint,
): T | undefined;
export function maxBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => Date,
): T | undefined;
export function maxBy<T>(
array: readonly T[],
array: Iterable<T>,
selector:
| ((el: T) => number)
| ((el: T) => string)

View File

@ -23,17 +23,17 @@
* ```
*/
export function maxOf<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => number,
): number | undefined;
export function maxOf<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => bigint,
): bigint | undefined;
export function maxOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
array: readonly T[],
array: Iterable<T>,
selector: S,
): ReturnType<S> | undefined {
let maximumValue: ReturnType<S> | undefined = undefined;

View File

@ -21,7 +21,7 @@
* ```
*/
export function maxWith<T>(
array: readonly T[],
array: Iterable<T>,
comparator: (a: T, b: T) => number,
): T | undefined {
let max: T | undefined = undefined;

View File

@ -22,23 +22,23 @@
* ```
*/
export function minBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => number,
): T | undefined;
export function minBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => string,
): T | undefined;
export function minBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => bigint,
): T | undefined;
export function minBy<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => Date,
): T | undefined;
export function minBy<T>(
array: readonly T[],
array: Iterable<T>,
selector:
| ((el: T) => number)
| ((el: T) => string)

View File

@ -22,17 +22,17 @@
* ```
*/
export function minOf<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => number,
): number | undefined;
export function minOf<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => bigint,
): bigint | undefined;
export function minOf<T, S extends ((el: T) => number) | ((el: T) => bigint)>(
array: readonly T[],
array: Iterable<T>,
selector: S,
): ReturnType<S> | undefined {
let minimumValue: ReturnType<S> | undefined = undefined;

View File

@ -17,7 +17,7 @@
* ```
*/
export function minWith<T>(
array: readonly T[],
array: Iterable<T>,
comparator: (a: T, b: T) => number,
): T | undefined {
let min: T | undefined = undefined;

View File

@ -19,15 +19,15 @@
* ```
*/
export function partition<T, U extends T>(
array: readonly T[],
array: Iterable<T>,
predicate: (el: T) => el is U,
): [U[], Exclude<T, U>[]];
export function partition<T>(
array: readonly T[],
array: Iterable<T>,
predicate: (el: T) => boolean,
): [T[], T[]];
export function partition(
array: readonly unknown[],
array: Iterable<unknown>,
predicate: (el: unknown) => boolean,
): [unknown[], unknown[]] {
const matches: Array<unknown> = [];

View File

@ -20,16 +20,18 @@
* ]);
* ```
*/
export function permutations<T>(inputArray: readonly T[]): T[][] {
export function permutations<T>(inputArray: Iterable<T>): T[][] {
const ret: T[][] = [];
const k = inputArray.length;
const array = [...inputArray];
const k = array.length;
if (k === 0) {
return ret;
}
// Heap's Algorithm
const array = [...inputArray];
const c = new Array<number>(k).fill(0);
ret.push([...array]);

View File

@ -21,7 +21,7 @@
* ```
*/
export function sumOf<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => number,
): number {
let sum = 0;

View File

@ -16,7 +16,7 @@
* assertEquals(shoppingList, ["Pepper", "Carrots", "Leek", "Radicchio"]);
* ```
*/
export function union<T>(...arrays: (readonly T[])[]): T[] {
export function union<T>(...arrays: Iterable<T>[]): T[] {
const set = new Set<T>();
for (const array of arrays) {

View File

@ -25,8 +25,8 @@
export function unzip<T, U>(pairs: readonly [T, U][]): [T[], U[]] {
const { length } = pairs;
const ret: [T[], U[]] = [
Array.from({ length }),
Array.from({ length }),
Array<T>(length),
Array<U>(length),
];
pairs.forEach(([first, second], index) => {