1
0

refactor modules into namespaces

This commit is contained in:
2024-05-09 18:11:50 +02:00
parent ab11074744
commit fccebc61b6
5 changed files with 336 additions and 332 deletions

View File

@@ -1,4 +1,4 @@
import { join } from "./collector.js"; import { Collector } from "./collector.js";
import { Enumerable } from "./sync.js"; import { Enumerable } from "./sync.js";
import { asArray, isDefined } from "./utils.js"; import { asArray, isDefined } from "./utils.js";
@@ -255,7 +255,7 @@ class BitArrayImpl implements BitArray {
} }
public toString() { public toString() {
return Enumerable.sequence(this).select(bit => bit ? '1' : '0').collect(join()); return Enumerable.sequence(this).select(bit => bit ? '1' : '0').collect(Collector.join());
} }
} }
@@ -497,7 +497,7 @@ class BitArraySlice implements BitArray {
} }
public toString() { public toString() {
return Enumerable.sequence(this).select(bit => bit ? '1' : '0').collect(join()); return Enumerable.sequence(this).select(bit => bit ? '1' : '0').collect(Collector.join());
} }
} }

View File

@@ -6,7 +6,8 @@ export interface Collector<TElement, TAccumulator, TResult> {
finalize(accumulator: TAccumulator): TResult; finalize(accumulator: TAccumulator): TResult;
} }
class SimpleCollector<TElement, TAccumulator, TResult> implements Collector<TElement, TAccumulator, TResult> { export namespace Collector {
class SimpleCollector<TElement, TAccumulator, TResult> implements Collector<TElement, TAccumulator, TResult> {
readonly #initialize: () => TAccumulator; readonly #initialize: () => TAccumulator;
readonly #accumulate: (accumulator: TAccumulator, element: TElement) => void; readonly #accumulate: (accumulator: TAccumulator, element: TElement) => void;
readonly #finalize: (accumulator: TAccumulator) => TResult; readonly #finalize: (accumulator: TAccumulator) => TResult;
@@ -29,14 +30,14 @@ class SimpleCollector<TElement, TAccumulator, TResult> implements Collector<TEle
finalize(accumulator: TAccumulator): TResult { finalize(accumulator: TAccumulator): TResult {
return this.#finalize(accumulator); return this.#finalize(accumulator);
} }
} }
export function create<TElement, TAccumulator, TResult>(initialize: () => TAccumulator, accumulate: (accumulator: TAccumulator, element: TElement) => void, export function create<TElement, TAccumulator, TResult>(initialize: () => TAccumulator, accumulate: (accumulator: TAccumulator, element: TElement) => void,
finalize: (accumulator: TAccumulator) => TResult): Collector<TElement, TAccumulator, TResult> { finalize: (accumulator: TAccumulator) => TResult): Collector<TElement, TAccumulator, TResult> {
return new SimpleCollector(initialize, accumulate, finalize); return new SimpleCollector(initialize, accumulate, finalize);
} }
class ToArrayCollector<TElement> implements Collector<TElement, TElement[], TElement[]> { class ToArrayCollector<TElement> implements Collector<TElement, TElement[], TElement[]> {
initialize(): TElement[] { initialize(): TElement[] {
return []; return [];
} }
@@ -48,15 +49,15 @@ class ToArrayCollector<TElement> implements Collector<TElement, TElement[], TEle
finalize(accumulator: TElement[]) { finalize(accumulator: TElement[]) {
return accumulator; return accumulator;
} }
} }
const toArrayCollector = new ToArrayCollector<any>(); const toArrayCollector = new ToArrayCollector<any>();
export function toArray<TElement>(): Collector<TElement, any, TElement[]> { export function toArray<TElement>(): Collector<TElement, any, TElement[]> {
return toArrayCollector; return toArrayCollector;
} }
class ToObjectCollector<TElement, TKey extends PropertyKey, TValue> implements Collector<TElement, Record<TKey, TValue>, Record<TKey, TValue>> { class ToObjectCollector<TElement, TKey extends PropertyKey, TValue> implements Collector<TElement, Record<TKey, TValue>, Record<TKey, TValue>> {
readonly #keySelector: Converter<TElement, TKey>; readonly #keySelector: Converter<TElement, TKey>;
readonly #valueSelector: Converter<TElement, TValue>; readonly #valueSelector: Converter<TElement, TValue>;
@@ -79,13 +80,13 @@ class ToObjectCollector<TElement, TKey extends PropertyKey, TValue> implements C
finalize(accumulator: Record<TKey, TValue>) { finalize(accumulator: Record<TKey, TValue>) {
return accumulator; return accumulator;
} }
} }
export function toObject<TElement, TKey extends PropertyKey, TValue>(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>): Collector<TElement, any, Record<TKey, TValue>> { export function toObject<TElement, TKey extends PropertyKey, TValue>(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>): Collector<TElement, any, Record<TKey, TValue>> {
return new ToObjectCollector<TElement, TKey, TValue>(keySelector, valueSelector); return new ToObjectCollector<TElement, TKey, TValue>(keySelector, valueSelector);
} }
class ToMapCollector<TElement, TKey, TValue> implements Collector<TElement, Map<TKey, TValue>, Map<TKey, TValue>> { class ToMapCollector<TElement, TKey, TValue> implements Collector<TElement, Map<TKey, TValue>, Map<TKey, TValue>> {
readonly #keySelector: Converter<TElement, TKey>; readonly #keySelector: Converter<TElement, TKey>;
readonly #valueSelector: Converter<TElement, TValue>; readonly #valueSelector: Converter<TElement, TValue>;
@@ -108,13 +109,13 @@ class ToMapCollector<TElement, TKey, TValue> implements Collector<TElement, Map<
finalize(accumulator: Map<TKey, TValue>) { finalize(accumulator: Map<TKey, TValue>) {
return accumulator; return accumulator;
} }
} }
export function toMap<TElement, TKey, TValue>(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>): Collector<TElement, any, Map<TKey, TValue>> { export function toMap<TElement, TKey, TValue>(keySelector: Converter<TElement, TKey>, valueSelector: Converter<TElement, TValue>): Collector<TElement, any, Map<TKey, TValue>> {
return new ToMapCollector<TElement, TKey, TValue>(keySelector, valueSelector); return new ToMapCollector<TElement, TKey, TValue>(keySelector, valueSelector);
} }
class ToSetCollector<TElement> implements Collector<TElement, Set<TElement>, Set<TElement>> { class ToSetCollector<TElement> implements Collector<TElement, Set<TElement>, Set<TElement>> {
initialize() { initialize() {
return new Set<TElement>(); return new Set<TElement>();
} }
@@ -126,15 +127,15 @@ class ToSetCollector<TElement> implements Collector<TElement, Set<TElement>, Set
finalize(accumulator: Set<TElement>) { finalize(accumulator: Set<TElement>) {
return accumulator; return accumulator;
} }
} }
const toSetCollector = new ToSetCollector<any>(); const toSetCollector = new ToSetCollector<any>();
export function toSet<TElement>(): Collector<TElement, any, Set<TElement>> { export function toSet<TElement>(): Collector<TElement, any, Set<TElement>> {
return toSetCollector; return toSetCollector;
} }
class JoinCollector implements Collector<any, any[], string> { class JoinCollector implements Collector<any, any[], string> {
readonly #delimiter: string; readonly #delimiter: string;
readonly #prefix: string; readonly #prefix: string;
readonly #suffix: string; readonly #suffix: string;
@@ -156,13 +157,13 @@ class JoinCollector implements Collector<any, any[], string> {
finalize(accumulator: any[]) { finalize(accumulator: any[]) {
return this.#prefix + accumulator.join(this.#delimiter) + this.#suffix; return this.#prefix + accumulator.join(this.#delimiter) + this.#suffix;
} }
} }
export function join(delimiter?: string, prefix?: string, suffix?: string): Collector<any, any, string> { export function join(delimiter?: string, prefix?: string, suffix?: string): Collector<any, any, string> {
return new JoinCollector(delimiter, prefix, suffix); return new JoinCollector(delimiter, prefix, suffix);
} }
class SumCollector implements Collector<number, { sum: number }, number> { class SumCollector implements Collector<number, { sum: number }, number> {
initialize() { initialize() {
return { sum: 0 }; return { sum: 0 };
} }
@@ -174,15 +175,15 @@ class SumCollector implements Collector<number, { sum: number }, number> {
finalize(accumulator: { sum: number }) { finalize(accumulator: { sum: number }) {
return accumulator.sum; return accumulator.sum;
} }
} }
const sumCollector = new SumCollector(); const sumCollector = new SumCollector();
export function sum(): Collector<number, any, number> { export function sum(): Collector<number, any, number> {
return sumCollector; return sumCollector;
} }
class BigIntSumCollector implements Collector<bigint, { sum: bigint }, bigint> { class BigIntSumCollector implements Collector<bigint, { sum: bigint }, bigint> {
initialize() { initialize() {
return { sum: 0n, }; return { sum: 0n, };
} }
@@ -194,15 +195,15 @@ class BigIntSumCollector implements Collector<bigint, { sum: bigint }, bigint> {
finalize(accumulator: { sum: bigint }) { finalize(accumulator: { sum: bigint }) {
return accumulator.sum; return accumulator.sum;
} }
} }
const bigintSumCollector = new BigIntSumCollector(); const bigintSumCollector = new BigIntSumCollector();
export function bigintSum(): Collector<bigint, any, bigint> { export function bigintSum(): Collector<bigint, any, bigint> {
return bigintSumCollector; return bigintSumCollector;
} }
class AverageCollector implements Collector<number, { count: number, sum: number }, number> { class AverageCollector implements Collector<number, { count: number, sum: number }, number> {
initialize() { initialize() {
return { count: 0, sum: 0 }; return { count: 0, sum: 0 };
} }
@@ -215,15 +216,15 @@ class AverageCollector implements Collector<number, { count: number, sum: number
finalize(accumulator: { count: number, sum: number }) { finalize(accumulator: { count: number, sum: number }) {
return accumulator.count === 0 ? 0 : accumulator.sum / accumulator.count; return accumulator.count === 0 ? 0 : accumulator.sum / accumulator.count;
} }
} }
const averageCollector = new AverageCollector(); const averageCollector = new AverageCollector();
export function average(): Collector<number, any, number> { export function average(): Collector<number, any, number> {
return averageCollector; return averageCollector;
} }
class BigIntAverageCollector implements Collector<bigint, { count: number, sum: bigint }, bigint> { class BigIntAverageCollector implements Collector<bigint, { count: number, sum: bigint }, bigint> {
initialize() { initialize() {
return { count: 0, sum: 0n }; return { count: 0, sum: 0n };
} }
@@ -236,10 +237,11 @@ class BigIntAverageCollector implements Collector<bigint, { count: number, sum:
finalize(accumulator: { count: number, sum: bigint }) { finalize(accumulator: { count: number, sum: bigint }) {
return accumulator.count === 0 ? 0n : accumulator.sum / BigInt(accumulator.count); return accumulator.count === 0 ? 0n : accumulator.sum / BigInt(accumulator.count);
} }
} }
const bigintAverageCollector = new BigIntAverageCollector(); const bigintAverageCollector = new BigIntAverageCollector();
export function bigintAverage(): Collector<bigint, any, bigint> { export function bigintAverage(): Collector<bigint, any, bigint> {
return bigintAverageCollector; return bigintAverageCollector;
}
} }

View File

@@ -1,4 +1,4 @@
export * from "./sync.js"; export * from "./sync.js";
export * from "./async.js"; export * from "./async.js";
export * as collectors from "./collector.js"; export * from "./collector.js";
export * as random from "./random.js"; export * from "./random.js";

View File

@@ -11,17 +11,18 @@ export interface RandomOptions<T = any> {
random?: RandomGenerator; random?: RandomGenerator;
}; };
export const alwaysTrue: ElementPredicate = () => true; export namespace Random {
export const weightOfOne: ElementWeight = () => 1.0; export const alwaysTrue: ElementPredicate = () => true;
export const mathRandom: RandomGenerator = () => Math.random(); export const weightOfOne: ElementWeight = () => 1.0;
export const mathRandom: RandomGenerator = () => Math.random();
const defaultOptions = Object.freeze<Required<RandomOptions>>({ const defaultOptions = Object.freeze<Required<RandomOptions>>({
predicate: alwaysTrue, predicate: alwaysTrue,
weight: weightOfOne, weight: weightOfOne,
random: mathRandom random: mathRandom
}); });
function mergeOptions<T>(first: RandomOptions<T> | undefined, second: RandomOptions<T> | undefined): RandomOptions<T> | undefined { function mergeOptions<T>(first: RandomOptions<T> | undefined, second: RandomOptions<T> | undefined): RandomOptions<T> | undefined {
if (!first) { if (!first) {
return second; return second;
} }
@@ -38,9 +39,9 @@ function mergeOptions<T>(first: RandomOptions<T> | undefined, second: RandomOpti
weight: first.weight ?? second.weight, weight: first.weight ?? second.weight,
random: first.random ?? second.random random: first.random ?? second.random
}; };
} }
function withDefaultOptions<T>(options: RandomOptions<T> | undefined): Required<RandomOptions<T>> { function withDefaultOptions<T>(options: RandomOptions<T> | undefined): Required<RandomOptions<T>> {
if (!options || options === defaultOptions) { if (!options || options === defaultOptions) {
return defaultOptions; return defaultOptions;
} }
@@ -50,9 +51,9 @@ function withDefaultOptions<T>(options: RandomOptions<T> | undefined): Required<
weight: options.weight ?? defaultOptions.weight, weight: options.weight ?? defaultOptions.weight,
random: options.random ?? defaultOptions.random random: options.random ?? defaultOptions.random
}; };
} }
function _getRandomElement<T>(sequence: Iterable<T>, options: Required<RandomOptions<T>>) { function _getRandomElement<T>(sequence: Iterable<T>, options: Required<RandomOptions<T>>) {
const { predicate, weight, random } = options; const { predicate, weight, random } = options;
let result: T | undefined = undefined; let result: T | undefined = undefined;
@@ -80,13 +81,13 @@ function _getRandomElement<T>(sequence: Iterable<T>, options: Required<RandomOpt
} }
return { element: result, index: resultIndex }; return { element: result, index: resultIndex };
} }
export function getRandomElement<T>(sequence: Iterable<T>, options?: RandomOptions<T>) { export function getRandomElement<T>(sequence: Iterable<T>, options?: RandomOptions<T>) {
return _getRandomElement(sequence, withDefaultOptions(options)); return _getRandomElement(sequence, withDefaultOptions(options));
} }
export class RandomPicker<T> { export class RandomPicker<T> {
readonly #elements: Iterable<T>; readonly #elements: Iterable<T>;
readonly #flags: BitArray; readonly #flags: BitArray;
readonly #options: Required<RandomOptions<T>>; readonly #options: Required<RandomOptions<T>>;
@@ -131,4 +132,5 @@ export class RandomPicker<T> {
return result.element; return result.element;
} }
}
} }

View File

@@ -1,6 +1,6 @@
import { createEqualitySet } from "./equality-set.js"; import { createEqualitySet } from "./equality-set.js";
import { createEqualityMap } from "./equality-map.js"; import { createEqualityMap } from "./equality-map.js";
import { RandomGenerator, RandomOptions, getRandomElement, mathRandom } from "./random.js"; import { RandomGenerator, RandomOptions, Random } from "./random.js";
import { createQueue } from "./queue.js"; import { createQueue } from "./queue.js";
import { Collector } from "./collector.js"; import { Collector } from "./collector.js";
import { combineComparers, defaultArrayComparer, identity, operatorCompare, reverseComparer, strictEquals, wrapAsIterable } from "./utils.js"; import { combineComparers, defaultArrayComparer, identity, operatorCompare, reverseComparer, strictEquals, wrapAsIterable } from "./utils.js";
@@ -279,7 +279,7 @@ export namespace Enumerable {
} }
export function randomSequence(random?: RandomGenerator): Enumerable<number> { export function randomSequence(random?: RandomGenerator): Enumerable<number> {
return new FunctionEnumerable(random ?? mathRandom); return new FunctionEnumerable(random ?? Random.mathRandom);
} }
export function concat<T>(...enumerables: Enumerable<T>[]): Enumerable<T> { export function concat<T>(...enumerables: Enumerable<T>[]): Enumerable<T> {
@@ -899,7 +899,7 @@ export abstract class BaseEnumerable<TElement> extends EnumerableMarker implemen
} }
random(options?: RandomOptions<TElement>) { random(options?: RandomOptions<TElement>) {
return getRandomElement(this, options).element; return Random.getRandomElement(this, options).element;
} }
cached(): Enumerable<TElement> { cached(): Enumerable<TElement> {