mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
feat(collections): switch functions to take iterables when possible (#3401)
This commit is contained in:
parent
64a0d90682
commit
5199824fca
@ -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> = {};
|
||||
|
@ -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> = {};
|
||||
|
@ -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);
|
||||
|
@ -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>();
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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>[] = [];
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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> = [];
|
||||
|
@ -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]);
|
||||
|
@ -21,7 +21,7 @@
|
||||
* ```
|
||||
*/
|
||||
export function sumOf<T>(
|
||||
array: readonly T[],
|
||||
array: Iterable<T>,
|
||||
selector: (el: T) => number,
|
||||
): number {
|
||||
let sum = 0;
|
||||
|
@ -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) {
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user