Skip to content

Commit

Permalink
feat: improve zipwith, zipallwith, mergewith, mergeallwith methods an…
Browse files Browse the repository at this point in the history
…d fix related tests
  • Loading branch information
vitoke committed Nov 20, 2021
1 parent 2dce274 commit 915f9f2
Show file tree
Hide file tree
Showing 26 changed files with 739 additions and 1,290 deletions.
537 changes: 239 additions & 298 deletions deno_dist/collection-types/map/base.ts

Large diffs are not rendered by default.

76 changes: 0 additions & 76 deletions deno_dist/hashed/map/immutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,46 +100,6 @@ export class HashMapEmpty<K = any, V = any>
value: [],
};
}

mergeAll<O>(fillValue: O, ...sources: any): any {
return this.context.mergeAll(
fillValue,
this,
...(sources as any as [any, ...any[]])
);
}

mergeAllWith<R, O, I extends readonly [unknown, ...unknown[]]>(
fillValue: O,
mergeFun: (
key: K,
value: V | O,
...values: { [KT in keyof I]: I[KT] | O }
) => R,
...sources: any
): any {
return this.context.mergeAllWith(
fillValue,
mergeFun as any,
this,
...(sources as any as [any, ...any[]])
);
}

merge(...sources: any): any {
return this.context.merge(this, ...(sources as any as any[]));
}

mergeWith<R, K, I extends readonly [unknown, ...unknown[]]>(
mergeFun: (key: K, ...values: I) => R,
...sources: any
): any {
return this.context.mergeWith(
mergeFun as any,
this as any,
...(sources as any as [any, ...any[]])
);
}
}

export abstract class HashMapNonEmptyBase<K, V>
Expand Down Expand Up @@ -272,42 +232,6 @@ export abstract class HashMapNonEmptyBase<K, V>
value: this.toArray(),
};
}

mergeAll<O>(fillValue: O, ...sources: any): any {
return this.context.mergeAll(
fillValue,
this,
...(sources as any as [any, ...any[]])
);
}

mergeAllWith<R, O>(
fillValue: O,
mergeFun: (key: K, value: V | O, ...values: any) => R,
...sources: any
): any {
return this.context.mergeAllWith(
fillValue,
mergeFun as any,
this,
...(sources as any as [any, ...any[]])
);
}

merge(...sources: any): any {
return this.context.merge(this, ...(sources as any as any[]));
}

mergeWith<R, K, I extends readonly [unknown, ...unknown[]]>(
mergeFun: (key: K, ...values: I) => R,
...sources: any
): any {
return this.context.mergeWith(
mergeFun as any,
this as any,
...(sources as any as [any, ...any[]])
);
}
}

export type MapEntrySet<K, V> = HashMapBlock<K, V> | HashMapCollision<K, V>;
Expand Down
8 changes: 4 additions & 4 deletions deno_dist/hashed/map/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ export namespace HashMap {
extends CustomBase.RMapBase.Builder<K, V, HashMap.Types> {}

export interface Types extends RMap.Types {
normal: HashMap<this['_K'], this['_V']>;
nonEmpty: HashMap.NonEmpty<this['_K'], this['_V']>;
context: HashMap.Context<this['_K']>;
builder: HashMap.Builder<this['_K'], this['_V']>;
readonly normal: HashMap<this['_K'], this['_V']>;
readonly nonEmpty: HashMap.NonEmpty<this['_K'], this['_V']>;
readonly context: HashMap.Context<this['_K']>;
readonly builder: HashMap.Builder<this['_K'], this['_V']>;
}
}

Expand Down
8 changes: 4 additions & 4 deletions deno_dist/hashed/set/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export namespace HashSet {
extends CustomBase.RSetBase.Builder<T, HashSet.Types> {}

export interface Types extends CustomBase.RSetBase.Types {
normal: HashSet<this['_T']>;
nonEmpty: HashSet.NonEmpty<this['_T']>;
context: HashSet.Context<this['_T']>;
builder: HashSet.Builder<this['_T']>;
readonly normal: HashSet<this['_T']>;
readonly nonEmpty: HashSet.NonEmpty<this['_T']>;
readonly context: HashSet.Context<this['_T']>;
readonly builder: HashSet.Builder<this['_T']>;
}
}

Expand Down
40 changes: 0 additions & 40 deletions deno_dist/ordered/map/implementation/empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,44 +109,4 @@ export class OrderedMapEmpty<
value: [],
};
}

mergeAll<O>(fillValue: O, ...sources: any): any {
return this.context.mergeAll(
fillValue,
this,
...(sources as any as [any, ...any[]])
);
}

mergeAllWith<R, O>(
fillValue: O,
mergeFun: (key: K, value: V | O, ...values: any) => R,
...sources: any
): any {
return this.context.mergeAllWith(
fillValue,
mergeFun as any,
this,
...(sources as any as [any, ...any[]])
);
}

merge(...sources: any): any {
return this.context.merge(
((key: any, ...values: any): any => values) as any,
this,
...(sources as any as [any, ...any[]])
);
}

mergeWith<R, K>(
mergeFun: (key: K, ...values: any) => R,
...sources: any
): any {
return this.context.mergeWith(
mergeFun as any,
this as any,
...(sources as any as [any, ...any[]])
);
}
}
36 changes: 0 additions & 36 deletions deno_dist/ordered/map/implementation/non-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,40 +263,4 @@ export class OrderedMapNonEmpty<
value: this.sourceMap.toJSON().value,
};
}

mergeAll<O>(fillValue: O, ...sources: any): any {
return this.context.mergeAll(
fillValue,
this,
...(sources as any as [any, ...any[]])
);
}

mergeAllWith<R, O>(
fillValue: O,
mergeFun: (key: K, value: V | O, ...values: any) => R,
...sources: any
): any {
return this.context.mergeAllWith(
fillValue,
mergeFun as any,
this,
...(sources as any as [any, ...any[]])
);
}

merge(...sources: any): any {
return this.context.merge(this, ...(sources as any as any[]));
}

mergeWith<R, K>(
mergeFun: (key: K, ...values: any) => R,
...sources: any
): any {
return this.context.mergeWith(
mergeFun as any,
this as any,
...(sources as any as any[])
);
}
}
72 changes: 0 additions & 72 deletions deno_dist/sorted/map/immutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,46 +165,6 @@ export class SortedMapEmpty<K = any, V = any>
value: [],
};
}

mergeAll<O>(fillValue: O, ...sources: any): any {
return this.context.mergeAll(
fillValue,
this,
...(sources as any as [any, ...any[]])
);
}

mergeAllWith<R, O, I extends readonly [unknown, ...unknown[]]>(
fillValue: O,
mergeFun: (
key: K,
value: V | O,
...values: { [KT in keyof I]: I[KT] | O }
) => R,
...sources: any
): any {
return this.context.mergeAllWith(
fillValue,
mergeFun as any,
this,
...(sources as any as [any, ...any[]])
);
}

merge(...sources: any): any {
return this.context.merge(this, ...(sources as any as any[]));
}

mergeWith<R, K, I extends readonly [unknown, ...unknown[]]>(
mergeFun: (key: K, ...values: I) => R,
...sources: any
): any {
return this.context.mergeWith(
mergeFun as any,
this as any,
...(sources as any as any[])
);
}
}

export abstract class SortedMapNode<K, V>
Expand Down Expand Up @@ -467,38 +427,6 @@ export abstract class SortedMapNode<K, V>
value: this.toArray(),
};
}

mergeAll<O>(fillValue: O, ...sources: any): any {
return this.context.mergeAll(
fillValue,
this,
...(sources as any as [any, ...any[]])
);
}

mergeAllWith<R, O>(
fillValue: O,
mergeFun: (key: K, value: V | O, ...values: any) => R,
...sources: any
): any {
return this.context.mergeAllWith(
fillValue,
mergeFun as any,
this,
...(sources as any as [any, ...any[]])
);
}

merge(...sources: any): any {
return this.context.merge(this, ...sources);
}

mergeWith<R, K, I extends readonly [unknown, ...unknown[]]>(
mergeFun: (key: K, ...values: I) => R,
...sources: any
): any {
return this.context.mergeWith(mergeFun as any, this, ...sources);
}
}

export class SortedMapLeaf<K, V> extends SortedMapNode<K, V> {
Expand Down
60 changes: 32 additions & 28 deletions deno_dist/stream/stream/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,20 +567,20 @@ export function unfold<T>(
* @note ends the Stream when any of the given streams ends
*/
export const zipWith: {
<I extends readonly unknown[], R>(
zipFun: (...values: I) => R,
<I extends readonly unknown[]>(
...iters: { [K in keyof I]: StreamSource.NonEmpty<I[K]> } & unknown[]
): Stream.NonEmpty<R>;
<I extends readonly unknown[], R>(
zipFun: (...values: I) => R,
): <R>(zipFun: (...values: I) => R) => Stream.NonEmpty<R>;
<I extends readonly unknown[]>(
...iters: { [K in keyof I]: StreamSource<I[K]> } & unknown[]
): Stream<R>;
} = (zipFun, ...iters) => {
if (iters.some(StreamSource.isEmptyInstance)) {
return Stream.empty();
}
): <R>(zipFun: (...values: I) => R) => Stream<R>;
} = (...iters) => {
return (zipFun): any => {
if (iters.some(StreamSource.isEmptyInstance)) {
return Stream.empty();
}

return new FromStream(() => new ZipWithIterator(iters as any, zipFun)) as any;
return new FromStream(() => new ZipWithIterator(iters as any, zipFun));
};
};

/**
Expand All @@ -597,7 +597,7 @@ export const zip: {
<I extends readonly unknown[]>(
...iters: { [K in keyof I]: StreamSource<I[K]> } & unknown[]
): Stream<I>;
} = (...iters) => zipWith(Array, ...(iters as any));
} = (...iters) => zipWith(...(iters as any))(Array);

/**
* Returns a Stream with the result of applying given `zipFun` to each successive value resulting from the given `streams`, adding
Expand All @@ -616,25 +616,29 @@ export const zip: {
* // => [10, 13, 5]
*/
export const zipAllWith: {
<I extends readonly unknown[], O, R>(
fillValue: OptLazy<O>,
zipFun: (...values: { [K in keyof I]: I[K] | O }) => R,
<I extends readonly unknown[]>(
...streams: { [K in keyof I]: StreamSource.NonEmpty<I[K]> } & unknown[]
): Stream.NonEmpty<R>;
<I extends readonly unknown[], O, R>(
): <O, R>(
fillValue: OptLazy<O>,
zipFun: (...values: { [K in keyof I]: I[K] | O }) => R,
zipFun: (...values: { [K in keyof I]: I[K] | O }) => R
) => Stream.NonEmpty<R>;
<I extends readonly unknown[]>(
...streams: { [K in keyof I]: StreamSource<I[K]> } & unknown[]
): Stream<R>;
} = (fillValue, zipFun, ...streams) => {
if (streams.every(StreamSource.isEmptyInstance)) {
return Stream.empty();
}
): <O, R>(
fillValue: OptLazy<O>,
zipFun: (...values: { [K in keyof I]: I[K] | O }) => R
) => Stream<R>;
} = (...streams) => {
return (fillValue, zipFun: any): any => {
if (streams.every(StreamSource.isEmptyInstance)) {
return Stream.empty();
}

return new FromStream(
(): FastIterator<any> =>
new ZipAllWithItererator(fillValue, streams, zipFun as any)
) as any;
return new FromStream(
(): FastIterator<any> =>
new ZipAllWithItererator(fillValue, streams, zipFun)
);
};
};

/**
Expand All @@ -660,7 +664,7 @@ export const zipAll: {
fillValue: OptLazy<O>,
...streams: { [K in keyof I]: StreamSource<I[K]> } & unknown[]
): Stream<{ [K in keyof I]: I[K] | O }>;
} = (fillValue, ...streams) => zipAllWith(fillValue, Array, ...streams) as any;
} = (fillValue, ...streams) => zipAllWith(...streams)(fillValue, Array) as any;

class ArrayIterator<T> extends FastIteratorBase<T> {
i: number;
Expand Down
Loading

0 comments on commit 915f9f2

Please sign in to comment.