Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion collections/associate_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {};
Expand Down
2 changes: 1 addition & 1 deletion collections/associate_with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {};
Expand Down
2 changes: 1 addition & 1 deletion collections/distinct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion collections/distinct_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down
2 changes: 1 addition & 1 deletion collections/find_single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion collections/first_not_nullish_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions collections/join_to_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type JoinToStringOptions = {
* ```
*/
export function joinToString<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => string,
{
separator = ",",
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion collections/map_not_nullish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>[] = [];
Expand Down
10 changes: 5 additions & 5 deletions collections/max_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions collections/max_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion collections/max_with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions collections/min_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions collections/min_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion collections/min_with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions collections/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = [];
Expand Down
10 changes: 6 additions & 4 deletions collections/permutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// Heap's Algorithm
Copy link
Member

@kt3k kt3k May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor point, but I think this comment shouldn't be moved here as this seems referring to the algorithm in the below while loop

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]);
Expand Down
2 changes: 1 addition & 1 deletion collections/sum_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* ```
*/
export function sumOf<T>(
array: readonly T[],
array: Iterable<T>,
selector: (el: T) => number,
): number {
let sum = 0;
Expand Down
2 changes: 1 addition & 1 deletion collections/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions collections/unzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down