semantic-typescript 0.3.8 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,6 +12,9 @@ export declare class Optional<T> {
12
12
  isEmpty(): boolean;
13
13
  isPresent(): boolean;
14
14
  map<R>(mapper: Functional<T, R>): Optional<R>;
15
+ flat(mapper: Functional<T, Optional<T>>): Optional<T>;
16
+ flatMap<R>(mapper: Functional<T, Optional<R>>): Optional<R>;
17
+ orElse(other: MaybeInvalid<T>): MaybeInvalid<T>;
15
18
  semantic(): Semantic<T>;
16
19
  static empty<T>(): Optional<T>;
17
20
  static of<T>(value: MaybeInvalid<T>): Optional<T>;
package/dist/optional.js CHANGED
@@ -8,14 +8,14 @@ export class Optional {
8
8
  constructor(value) {
9
9
  this.value = value;
10
10
  Object.defineProperties(this, {
11
- "value": {
12
- value: this.value,
11
+ "Optional": {
12
+ value: OptionalSymbol,
13
13
  writable: false,
14
- enumerable: true,
14
+ enumerable: false,
15
15
  configurable: false
16
16
  },
17
- "Optional": {
18
- value: this.Optional,
17
+ "value": {
18
+ value: value,
19
19
  writable: false,
20
20
  enumerable: false,
21
21
  configurable: false
@@ -44,20 +44,10 @@ export class Optional {
44
44
  }
45
45
  ifPresent(action, elseAction) {
46
46
  if (this.isPresent() && isFunction(action)) {
47
- try {
48
- action(this.value);
49
- }
50
- catch (error) {
51
- throw new Error("Uncaught error on ifPresent.");
52
- }
47
+ action(this.value);
53
48
  }
54
49
  else if (isFunction(elseAction)) {
55
- try {
56
- elseAction();
57
- }
58
- catch (error) {
59
- throw new Error("Uncaught error on ifPresent elseAction.");
60
- }
50
+ elseAction();
61
51
  }
62
52
  }
63
53
  isEmpty() {
@@ -68,15 +58,28 @@ export class Optional {
68
58
  }
69
59
  map(mapper) {
70
60
  if (this.isPresent() && isFunction(mapper)) {
71
- try {
72
- return new Optional(mapper(this.value));
73
- }
74
- catch (error) {
75
- throw new Error("Uncaught error on map.");
76
- }
61
+ return new Optional(mapper(this.value));
77
62
  }
78
63
  return new Optional(null);
79
64
  }
65
+ flat(mapper) {
66
+ if (this.isPresent() && isFunction(mapper)) {
67
+ return mapper(this.value);
68
+ }
69
+ return new Optional(null);
70
+ }
71
+ flatMap(mapper) {
72
+ if (this.isPresent() && isFunction(mapper)) {
73
+ return mapper(this.value);
74
+ }
75
+ return new Optional(null);
76
+ }
77
+ orElse(other) {
78
+ if (this.isPresent()) {
79
+ return this.value;
80
+ }
81
+ return other;
82
+ }
80
83
  semantic() {
81
84
  if (this.isPresent()) {
82
85
  return generate(() => {
@@ -1,8 +1,6 @@
1
- import { Collectable, OrderedCollectable, UnorderedCollectable } from "./collectable";
2
- import { BigIntStatistics, NumericStatistics } from "./statistics";
1
+ import { BigIntStatistics, Collectable, NumericStatistics, OrderedCollectable, UnorderedCollectable, WindowCollectable } from "./collectable";
3
2
  import { type Predicate } from "./utility";
4
3
  import type { Generator, Functional, BiFunctional, Consumer, BiConsumer, Comparator, BiPredicate } from "./utility";
5
- import { WindowCollectable } from "./window";
6
4
  export declare class Semantic<E> {
7
5
  protected generator: Generator<E>;
8
6
  protected readonly Semantic: Symbol;
package/dist/semantic.js CHANGED
@@ -1,24 +1,23 @@
1
- import { Collectable, OrderedCollectable, UnorderedCollectable } from "./collectable";
1
+ import { BigIntStatistics, Collectable, NumericStatistics, OrderedCollectable, UnorderedCollectable, WindowCollectable } from "./collectable";
2
2
  import { isBigInt, isCollectable, isFunction, isIterable, isNumber, isSemantic } from "./guard";
3
- import { useCompare, useRandom } from "./hook";
4
- import { BigIntStatistics, NumericStatistics } from "./statistics";
3
+ import { useHash } from "./hash";
4
+ import { useCompare } from "./hook";
5
5
  import { SemanticSymbol } from "./symbol";
6
6
  import { validate } from "./utility";
7
- import { WindowCollectable } from "./window";
8
7
  export class Semantic {
9
8
  generator;
10
9
  Semantic = SemanticSymbol;
11
10
  constructor(generator) {
12
11
  this.generator = generator;
13
12
  Object.defineProperties(this, {
14
- "generator": {
15
- value: generator,
13
+ "Semantic": {
14
+ value: SemanticSymbol,
16
15
  writable: false,
17
- enumerable: true,
16
+ enumerable: false,
18
17
  configurable: false
19
18
  },
20
- "Semantic": {
21
- value: SemanticSymbol,
19
+ "generator": {
20
+ value: generator,
22
21
  writable: false,
23
22
  enumerable: false,
24
23
  configurable: false
@@ -269,7 +268,7 @@ export class Semantic {
269
268
  }
270
269
  });
271
270
  }
272
- throw new TypeError("Invalid arguments on redirect.");
271
+ throw new TypeError("Invalid arguments.");
273
272
  }
274
273
  reverse() {
275
274
  return new Semantic((accept, interrupt) => {
@@ -299,7 +298,7 @@ export class Semantic {
299
298
  return new Semantic((accept, interrupt) => {
300
299
  try {
301
300
  this.generator((element, index) => {
302
- accept(element, useRandom(index));
301
+ accept(element, useHash(element, index));
303
302
  }, interrupt);
304
303
  }
305
304
  catch (error) {
@@ -345,7 +344,7 @@ export class Semantic {
345
344
  }
346
345
  });
347
346
  }
348
- throw new TypeError("Invalid arguments on skip.");
347
+ throw new TypeError("Invalid arguments.");
349
348
  }
350
349
  sorted(comparator) {
351
350
  if (isFunction(comparator)) {
@@ -364,44 +363,38 @@ export class Semantic {
364
363
  }
365
364
  }
366
365
  sub(start, end) {
367
- if (isBigInt(start) && isBigInt(end)) {
368
- return new Semantic((accept, interrupt) => {
369
- try {
370
- let count = 0n;
371
- this.generator((element, index) => {
372
- if (count < end) {
373
- count++;
374
- if (count >= start) {
375
- accept(element, index);
376
- }
366
+ return new Semantic((accept, interrupt) => {
367
+ try {
368
+ let count = 0n;
369
+ this.generator((element, index) => {
370
+ if (count < end) {
371
+ count++;
372
+ if (count >= start) {
373
+ accept(element, index);
377
374
  }
378
- }, interrupt);
379
- }
380
- catch (error) {
381
- throw new Error("Uncaught error on sub.");
382
- }
383
- });
384
- }
385
- throw new TypeError("Invalid arguments on sub.");
375
+ }
376
+ }, interrupt);
377
+ }
378
+ catch (error) {
379
+ throw new Error("Uncaught error on sub.");
380
+ }
381
+ });
386
382
  }
387
383
  takeWhile(predicate) {
388
- if (isFunction(predicate)) {
389
- return new Semantic((accept, interrupt) => {
390
- try {
391
- this.generator((element, index) => {
392
- if (!predicate(element, index)) {
393
- interrupt(element, index);
394
- return;
395
- }
396
- accept(element, index);
397
- }, interrupt);
398
- }
399
- catch (error) {
400
- throw new Error("Uncaught error on takeWhile.");
401
- }
402
- });
403
- }
404
- throw new TypeError("Invalid arguments.");
384
+ return new Semantic((accept, interrupt) => {
385
+ try {
386
+ this.generator((element, index) => {
387
+ if (!predicate(element, index)) {
388
+ interrupt(element, index);
389
+ return;
390
+ }
391
+ accept(element, index);
392
+ }, interrupt);
393
+ }
394
+ catch (error) {
395
+ throw new Error("Uncaught error on takeWhile.");
396
+ }
397
+ });
405
398
  }
406
399
  toCollectable(mapper) {
407
400
  if (isFunction(mapper)) {
@@ -476,7 +469,7 @@ export class Semantic {
476
469
  }
477
470
  });
478
471
  }
479
- if (isBigInt(argument1)) {
472
+ else if (isBigInt(argument1)) {
480
473
  let offset = argument1;
481
474
  return new Semantic((accept, interrupt) => {
482
475
  try {
@@ -489,7 +482,7 @@ export class Semantic {
489
482
  }
490
483
  });
491
484
  }
492
- if (isFunction(argument1)) {
485
+ else if (isFunction(argument1)) {
493
486
  let translator = argument1;
494
487
  return new Semantic((accept, interrupt) => {
495
488
  try {
@@ -508,4 +501,4 @@ export class Semantic {
508
501
  ;
509
502
  Object.freeze(Semantic);
510
503
  Object.freeze(Semantic.prototype);
511
- Object.freeze(Object.getPrototypeOf(Semantic.prototype));
504
+ Object.freeze(Object.getPrototypeOf(Semantic));
package/dist/set.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { HashMap } from "./map";
2
+ import type { BiConsumer, Consumer, TriConsumer } from "./utility";
3
+ export declare class HashSet<E> implements Set<E> {
4
+ protected map: HashMap<E, boolean>;
5
+ size: number;
6
+ constructor();
7
+ add(value: E): this;
8
+ clear(): void;
9
+ delete(value: E): boolean;
10
+ entries(): SetIterator<[E, E]>;
11
+ forEach(consumer: Consumer<E>): void;
12
+ forEach(consumer: BiConsumer<E, E>): void;
13
+ forEach(consumer: TriConsumer<E, E, Set<E>>): void;
14
+ has(value: E): boolean;
15
+ keys(): IterableIterator<E>;
16
+ values(): IterableIterator<E>;
17
+ [Symbol.iterator](): IterableIterator<E>;
18
+ [Symbol.toStringTag]: string;
19
+ }
package/dist/set.js ADDED
@@ -0,0 +1,65 @@
1
+ import { HashMap } from "./map";
2
+ export class HashSet {
3
+ map = new HashMap();
4
+ size = 0;
5
+ constructor() {
6
+ Object.defineProperty(this, "size", {
7
+ get: () => this.map.size,
8
+ set: (value) => {
9
+ this.map.size = value;
10
+ },
11
+ enumerable: true,
12
+ configurable: true
13
+ });
14
+ Object.freeze(this);
15
+ }
16
+ add(value) {
17
+ this.map.set(value, true);
18
+ return this;
19
+ }
20
+ clear() {
21
+ this.map.clear();
22
+ }
23
+ delete(value) {
24
+ return this.map.delete(value);
25
+ }
26
+ *entries() {
27
+ for (let [key, value] of this.map.entries()) {
28
+ if (value) {
29
+ yield [key, key];
30
+ }
31
+ }
32
+ }
33
+ forEach(consumer) {
34
+ for (let [key, value] of this.map.entries()) {
35
+ if (value) {
36
+ consumer(key, key);
37
+ }
38
+ }
39
+ }
40
+ has(value) {
41
+ return this.map.has(value);
42
+ }
43
+ *keys() {
44
+ for (let [key, value] of this.map.entries()) {
45
+ if (value) {
46
+ yield key;
47
+ }
48
+ }
49
+ }
50
+ *values() {
51
+ for (let [key, value] of this.map.entries()) {
52
+ if (value) {
53
+ yield key;
54
+ }
55
+ }
56
+ }
57
+ [Symbol.iterator]() {
58
+ return this.values();
59
+ }
60
+ [Symbol.toStringTag] = "HashSet";
61
+ }
62
+ ;
63
+ Object.freeze(HashSet);
64
+ Object.freeze(HashSet.prototype);
65
+ Object.freeze(Object.getPrototypeOf(HashSet));
@@ -7,11 +7,13 @@ export class Statistics extends OrderedCollectable {
7
7
  Statistics = StatisticsSymbol;
8
8
  constructor(parameter, comparator) {
9
9
  super(parameter, comparator || useCompare);
10
- Object.defineProperty(this, "Statistics", {
11
- value: StatisticsSymbol,
12
- enumerable: false,
13
- writable: false,
14
- configurable: false
10
+ Object.defineProperties(this, {
11
+ "Statistics": {
12
+ value: StatisticsSymbol,
13
+ enumerable: false,
14
+ writable: false,
15
+ configurable: false
16
+ }
15
17
  });
16
18
  Object.freeze(this);
17
19
  }
@@ -48,11 +50,13 @@ export class NumericStatistics extends Statistics {
48
50
  NumericStatistics = NumericStatisticsSymbol;
49
51
  constructor(parameter, comparator) {
50
52
  super(parameter, comparator || useCompare);
51
- Object.defineProperty(this, "NumericStatistics", {
52
- value: NumericStatisticsSymbol,
53
- enumerable: false,
54
- writable: false,
55
- configurable: false
53
+ Object.defineProperties(this, {
54
+ "NumericStatistics": {
55
+ value: NumericStatisticsSymbol,
56
+ enumerable: false,
57
+ writable: false,
58
+ configurable: false
59
+ }
56
60
  });
57
61
  Object.freeze(this);
58
62
  }
@@ -265,11 +269,13 @@ export class BigIntStatistics extends Statistics {
265
269
  BigIntStatistics = BigIntStatisticsSymbol;
266
270
  constructor(parameter, comparator) {
267
271
  super(parameter, comparator || useCompare);
268
- Object.defineProperty(this, "BigIntStatistics", {
269
- value: BigIntStatisticsSymbol,
270
- enumerable: false,
271
- writable: false,
272
- configurable: false
272
+ Object.defineProperties(this, {
273
+ "BigIntStatistics": {
274
+ value: BigIntStatisticsSymbol,
275
+ enumerable: false,
276
+ writable: false,
277
+ configurable: false
278
+ }
273
279
  });
274
280
  Object.freeze(this);
275
281
  }
package/dist/symbol.d.ts CHANGED
@@ -8,3 +8,17 @@ export declare let StatisticsSymbol: symbol;
8
8
  export declare let NumericStatisticsSymbol: symbol;
9
9
  export declare let BigIntStatisticsSymbol: symbol;
10
10
  export declare let UnorderedCollectableSymbol: symbol;
11
+ export declare let SemanticMapSymbol: symbol;
12
+ export declare let HashMapSymbol: symbol;
13
+ export declare let HashSetSymbol: symbol;
14
+ export declare let TreeMapSymbol: symbol;
15
+ export declare let TreeSetSymbol: symbol;
16
+ export declare let NodeSymbol: symbol;
17
+ export declare let LinearNodeSymbol: symbol;
18
+ export declare let BinaryNodeSymbol: symbol;
19
+ export declare let RedBlackNodeSymbol: symbol;
20
+ export declare let AverageLevelNodeSymbol: symbol;
21
+ export declare let TreeSymbol: symbol;
22
+ export declare let BinaryTreeSymbol: symbol;
23
+ export declare let RedBlackTreeSymbol: symbol;
24
+ export declare let AVLTreeSymbol: symbol;
package/dist/symbol.js CHANGED
@@ -8,3 +8,17 @@ export let StatisticsSymbol = Symbol.for("Statistics");
8
8
  export let NumericStatisticsSymbol = Symbol.for("NumericStatistics");
9
9
  export let BigIntStatisticsSymbol = Symbol.for("BigIntStatistics");
10
10
  export let UnorderedCollectableSymbol = Symbol.for("UnorderedCollectable");
11
+ export let SemanticMapSymbol = Symbol.for("SemanticMap");
12
+ export let HashMapSymbol = Symbol.for("HashMap");
13
+ export let HashSetSymbol = Symbol.for("HashSet");
14
+ export let TreeMapSymbol = Symbol.for("TreeMap");
15
+ export let TreeSetSymbol = Symbol.for("TreeSet");
16
+ export let NodeSymbol = Symbol.for("Node");
17
+ export let LinearNodeSymbol = Symbol.for("LinearNode");
18
+ export let BinaryNodeSymbol = Symbol.for("BinaryNode");
19
+ export let RedBlackNodeSymbol = Symbol.for("RedBlackNode");
20
+ export let AverageLevelNodeSymbol = Symbol.for("AverageLevelNode");
21
+ export let TreeSymbol = Symbol.for("Tree");
22
+ export let BinaryTreeSymbol = Symbol.for("BinaryTree");
23
+ export let RedBlackTreeSymbol = Symbol.for("RedBlackTree");
24
+ export let AVLTreeSymbol = Symbol.for("AVLTree");
package/dist/tree.d.ts ADDED
@@ -0,0 +1,82 @@
1
+ import { BinaryNode, RedBlackNode, type Node } from './node';
2
+ import { Optional } from './optional';
3
+ import { type Comparator } from './utility';
4
+ export interface Tree<E, N extends Node<E, N>, T extends Tree<E, N, T>> extends Iterable<E> {
5
+ root(): Optional<N>;
6
+ isEmpty(): boolean;
7
+ size(): bigint;
8
+ height(): bigint;
9
+ depth(): bigint;
10
+ width(): bigint;
11
+ level(): bigint;
12
+ add(value: E): void;
13
+ remove(value: E): void;
14
+ contains(value: E): boolean;
15
+ find(value: E): Optional<N>;
16
+ preorder(): Generator<E>;
17
+ inorder(): Generator<E>;
18
+ postorder(): Generator<E>;
19
+ breadth(): Generator<E>;
20
+ toArray(): Array<E>;
21
+ toSet(): Set<E>;
22
+ sub(node: N): T;
23
+ prune(node: Node<E, N>): T;
24
+ merge(other: T): T;
25
+ [Symbol.iterator](): Iterator<E>;
26
+ }
27
+ export declare abstract class AbstractTree<E, N extends Node<E, N>, T extends Tree<E, N, T>> implements Tree<E, N, T> {
28
+ protected readonly Tree: symbol;
29
+ protected internal: bigint;
30
+ constructor();
31
+ abstract root(): Optional<N>;
32
+ isEmpty(): boolean;
33
+ size(): bigint;
34
+ abstract add(value: E): void;
35
+ abstract remove(value: E): void;
36
+ contains(value: E): boolean;
37
+ abstract find(value: E): Optional<N>;
38
+ abstract preorder(): Generator<E>;
39
+ abstract inorder(): Generator<E>;
40
+ abstract postorder(): Generator<E>;
41
+ abstract breadth(): Generator<E>;
42
+ toArray(): Array<E>;
43
+ toSet(): Set<E>;
44
+ abstract sub(node: N): T;
45
+ abstract prune(node: Node<E, N>): T;
46
+ abstract merge(other: T): T;
47
+ height(): bigint;
48
+ width(): bigint;
49
+ depth(): bigint;
50
+ level(): bigint;
51
+ abstract [Symbol.iterator](): Iterator<E>;
52
+ }
53
+ export declare abstract class BinaryTree<E, N extends BinaryNode<E, N>, T extends BinaryTree<E, N, T>> extends AbstractTree<E, N, T> {
54
+ protected readonly BinaryTree: symbol;
55
+ protected first: Optional<N>;
56
+ constructor();
57
+ constructor(root: Optional<N>);
58
+ root(): Optional<N>;
59
+ find(value: E): Optional<N>;
60
+ preorder(): Generator<E>;
61
+ inorder(): Generator<E>;
62
+ postorder(): Generator<E>;
63
+ isBalanced(): boolean;
64
+ isComplete(): boolean;
65
+ isFull(): boolean;
66
+ isPerfect(): boolean;
67
+ [Symbol.iterator](): Iterator<E>;
68
+ }
69
+ export declare class RedBlackTree<E> extends BinaryTree<E, RedBlackNode<E>, RedBlackTree<E>> {
70
+ protected readonly RedBlackTree: symbol;
71
+ protected comparator: Comparator<E>;
72
+ constructor();
73
+ constructor(root: Optional<RedBlackNode<E>>);
74
+ constructor(comparator: Comparator<E>);
75
+ constructor(root: Optional<RedBlackNode<E>>, comparator: Comparator<E>);
76
+ add(value: E): void;
77
+ remove(value: E): void;
78
+ breadth(): Generator<E>;
79
+ sub(node: RedBlackNode<E>): RedBlackTree<E>;
80
+ prune(node: Node<E, RedBlackNode<E>>): RedBlackTree<E>;
81
+ merge(other: RedBlackTree<E>): RedBlackTree<E>;
82
+ }