scats 1.0.32 → 1.2.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.
Files changed (72) hide show
  1. package/README.md +82 -3
  2. package/coverage/clover.xml +937 -0
  3. package/coverage/coverage-final.json +15 -0
  4. package/coverage/lcov-report/array-iterable.ts.html +1709 -0
  5. package/coverage/lcov-report/base.css +224 -0
  6. package/coverage/lcov-report/block-navigation.js +79 -0
  7. package/coverage/lcov-report/collection.ts.html +1475 -0
  8. package/coverage/lcov-report/either.ts.html +1934 -0
  9. package/coverage/lcov-report/favicon.png +0 -0
  10. package/coverage/lcov-report/hashmap.ts.html +527 -0
  11. package/coverage/lcov-report/hashset.ts.html +392 -0
  12. package/coverage/lcov-report/index.html +126 -0
  13. package/coverage/lcov-report/index.ts.html +101 -0
  14. package/coverage/lcov-report/option.ts.html +758 -0
  15. package/coverage/lcov-report/prettify.css +1 -0
  16. package/coverage/lcov-report/prettify.js +2 -0
  17. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  18. package/coverage/lcov-report/sorter.js +170 -0
  19. package/coverage/lcov-report/src/abstract-map.ts.html +317 -0
  20. package/coverage/lcov-report/src/abstract-set.ts.html +200 -0
  21. package/coverage/lcov-report/src/array-iterable.ts.html +1751 -0
  22. package/coverage/lcov-report/src/collection.ts.html +1778 -0
  23. package/coverage/lcov-report/src/either.ts.html +1934 -0
  24. package/coverage/lcov-report/src/hashmap.ts.html +428 -0
  25. package/coverage/lcov-report/src/hashset.ts.html +482 -0
  26. package/coverage/lcov-report/src/index.html +276 -0
  27. package/coverage/lcov-report/src/index.ts.html +110 -0
  28. package/coverage/lcov-report/src/mutable/hashmap.ts.html +821 -0
  29. package/coverage/lcov-report/src/mutable/hashset.ts.html +611 -0
  30. package/coverage/lcov-report/src/mutable/index.html +126 -0
  31. package/coverage/lcov-report/src/mutable.ts.html +89 -0
  32. package/coverage/lcov-report/src/option.ts.html +758 -0
  33. package/coverage/lcov-report/src/try.ts.html +923 -0
  34. package/coverage/lcov-report/src/util.ts.html +518 -0
  35. package/coverage/lcov-report/try.ts.html +923 -0
  36. package/coverage/lcov-report/util.ts.html +518 -0
  37. package/coverage/lcov.info +2223 -0
  38. package/dist/abstract-map.d.ts +25 -0
  39. package/dist/abstract-map.js +62 -0
  40. package/dist/abstract-set.d.ts +13 -0
  41. package/dist/abstract-set.js +33 -0
  42. package/dist/array-iterable.d.ts +1 -3
  43. package/dist/array-iterable.js +21 -23
  44. package/dist/collection.d.ts +59 -20
  45. package/dist/collection.js +254 -62
  46. package/dist/hashmap.d.ts +12 -26
  47. package/dist/hashmap.js +39 -58
  48. package/dist/hashset.d.ts +12 -17
  49. package/dist/hashset.js +25 -35
  50. package/dist/index.d.ts +2 -0
  51. package/dist/index.js +3 -0
  52. package/dist/mutable/hashmap.d.ts +24 -0
  53. package/dist/mutable/hashmap.js +118 -0
  54. package/dist/mutable/hashset.d.ts +20 -0
  55. package/dist/mutable/hashset.js +88 -0
  56. package/dist/mutable.d.ts +3 -0
  57. package/dist/mutable.js +9 -0
  58. package/dist/option.js +1 -1
  59. package/package.json +12 -12
  60. package/src/abstract-map.ts +79 -0
  61. package/src/abstract-set.ts +40 -0
  62. package/src/array-iterable.ts +26 -12
  63. package/src/collection.ts +394 -85
  64. package/src/either.ts +2 -2
  65. package/src/hashmap.ts +74 -73
  66. package/src/hashset.ts +82 -42
  67. package/src/index.ts +3 -0
  68. package/src/mutable/hashmap.ts +247 -0
  69. package/src/mutable/hashset.ts +177 -0
  70. package/src/mutable.ts +3 -0
  71. package/src/option.ts +2 -2
  72. package/src/try.ts +9 -9
package/src/hashmap.ts CHANGED
@@ -1,76 +1,73 @@
1
- import {option, Option} from './option';
2
- import {Collection} from './collection';
3
- import {HashSet} from './hashset';
4
- import {ArrayIterable} from './array-iterable';
1
+ import {mutable, Option} from './index';
2
+ import {AbstractMap, Tuple2} from './abstract-map';
5
3
 
6
- export type Tuple2<K, V> = [K, V];
7
4
 
8
- export class HashMap<K, V> extends ArrayIterable<Tuple2<K, V>, HashMap<K, V>> {
5
+
6
+ export class HashMap<K, V> extends AbstractMap<K, V, HashMap<K, V>> {
9
7
 
10
8
  protected fromArray(array: Tuple2<K, V>[]): HashMap<K, V> {
11
9
  return HashMap.of(...array);
12
10
  }
13
11
 
14
- constructor(private readonly map: Map<K, V>) {
15
- super();
12
+ protected constructor(protected readonly map: Map<K, V>) {
13
+ super(map);
16
14
  }
17
15
 
18
16
  static of<K, V>(...values: Tuple2<K, V>[]): HashMap<K, V> {
19
17
  return new HashMap<K, V>(new Map(values));
20
18
  }
21
19
 
22
- static empty = new HashMap(new Map());
23
-
24
- get size(): number {
25
- return this.map.size;
26
- }
27
-
28
- get isEmpty(): boolean {
29
- return this.map.size <= 0;
30
- }
31
-
32
- get(key: K): Option<V> {
33
- return option(this.map.get(key));
34
- }
35
-
36
- getOrElse(key: K, defaultValue: () => V): V {
37
- return this.get(key).getOrElse(defaultValue);
38
- }
39
-
40
- getOrElseValue(key: K, defaultValue: V): V {
41
- return this.get(key).getOrElseValue(defaultValue);
20
+ static from<K, V>(values: Iterable<Tuple2<K, V>>): HashMap<K, V> {
21
+ return HashMap.of(...Array.from(values));
42
22
  }
43
23
 
44
- get keySet(): HashSet<K> {
45
- return HashSet.of(...Array.from(this.map.keys()));
46
- }
47
-
48
- get keyIterator(): IterableIterator<K> {
49
- return this.map.keys();
50
- }
51
-
52
- get keys(): Collection<K> {
53
- return new Collection(Array.from(this.map.keys()));
54
- }
24
+ static empty = new HashMap(new Map());
55
25
 
56
- get values(): Collection<V> {
57
- return new Collection(Array.from(this.map.values()));
26
+ appendedAll(map: HashMap<K, V>): HashMap<K, V> {
27
+ return this.concat(map);
58
28
  }
59
29
 
60
- get valueIterator(): IterableIterator<V> {
61
- return this.map.values();
30
+ appended(key: K, value: V): HashMap<K, V> {
31
+ return this.set(key, value);
62
32
  }
63
33
 
64
- get entries(): Collection<Tuple2<K, V>> {
65
- return new Collection(Array.from(this.map.entries()));
34
+ /**
35
+ * Creates a new map obtained by updating this map with a given key/value pair.
36
+ * @param key the key
37
+ * @param value the value
38
+ * @tparam V1 the type of the added value
39
+ * @return A new map with the new key/value mapping added to this map.
40
+ */
41
+ updated(key: K, value: V): HashMap<K, V> {
42
+ return this.set(key, value);
66
43
  }
67
44
 
68
- get entriesIterator(): IterableIterator<Tuple2<K, V>> {
69
- return this.map.entries();
45
+ /**
46
+ * Removes a key from this map, returning a new map.
47
+ *
48
+ * @param key the key to be removed
49
+ * @return a new map without a binding for ''key''
50
+ */
51
+ removed(key: K): HashMap<K, V> {
52
+ const next = new Map(this.map);
53
+ next.delete(key);
54
+ return new HashMap<K, V>(next);
70
55
  }
71
56
 
72
- appendedAll(map: HashMap<K, V>): HashMap<K, V> {
73
- return this.concat(map);
57
+ /**
58
+ * Creates a new map from this map by removing all elements of another
59
+ * collection.
60
+ *
61
+ * @param keys the collection containing the removed elements.
62
+ * @return a new map that contains all elements of the current map
63
+ * except one less occurrence of each of the elements of `elems`.
64
+ */
65
+ removedAll(keys: Iterable<K>): HashMap<K, V> {
66
+ const next = new Map(this.map);
67
+ for (const key of keys) {
68
+ next.delete(key);
69
+ }
70
+ return new HashMap<K, V>(next);
74
71
  }
75
72
 
76
73
  concat(map: HashMap<K, V>): HashMap<K, V> {
@@ -87,29 +84,33 @@ export class HashMap<K, V> extends ArrayIterable<Tuple2<K, V>, HashMap<K, V>> {
87
84
  return new HashMap<K, V>(new Map(next));
88
85
  }
89
86
 
90
- remove(key: K): HashMap<K, V> {
91
- const next = new Map(this.map);
92
- next.delete(key);
93
- return new HashMap<K, V>(next);
94
- }
95
-
96
- containsKey(key: K): boolean {
97
- return this.map.has(key);
98
- }
99
-
100
- updated(key: K, value: V): HashMap<K, V> {
101
- return this.set(key, value);
102
- }
103
-
104
- get toCollection(): Collection<Tuple2<K, V>> {
105
- return new Collection<Tuple2<K, V>>(Array.from(this.map.entries()));
106
- }
107
-
108
- get toMap(): Map<K, V> {
109
- return this.map;
110
- }
111
-
112
- get toArray(): Array<Tuple2<K, V>> {
113
- return Array.from(this.map.entries());
87
+ /**
88
+ * Update a mapping for the specified key and its current optionally-mapped value
89
+ * (`Some` if there is current mapping, `None` if not).
90
+ *
91
+ * If the remapping function returns `Some(v)`, the mapping is updated with the new value `v`.
92
+ * If the remapping function returns `None`, the mapping is removed (or remains absent if initially absent).
93
+ * If the function itself throws an exception, the exception is rethrown, and the current mapping is left unchanged.
94
+ *
95
+ * @param key the key value
96
+ * @param remappingFunction a partial function that receives current optionally-mapped value and return a new mapping
97
+ * @return A new map with the updated mapping with the key
98
+ */
99
+ updatedWith(key: K): (remappingFunction: (maybeValue: Option<V>) => Option<V>) => HashMap<K, V> {
100
+ const previousValue = this.get(key);
101
+ return (remappingFunction: (maybeValue: Option<V>) => Option<V>) => {
102
+ const nextValue = remappingFunction(previousValue);
103
+ if (previousValue.isEmpty && nextValue.isEmpty) {
104
+ return this;
105
+ } else if (previousValue.isDefined && nextValue.isEmpty) {
106
+ return this.removed(key);
107
+ } else {
108
+ return this.updated(key, nextValue.get);
109
+ }
110
+ };
111
+ }
112
+
113
+ get toMutable(): mutable.HashMap<K, V> {
114
+ return mutable.HashMap.of(...Array.from(this.map.entries()));
114
115
  }
115
116
  }
package/src/hashset.ts CHANGED
@@ -1,15 +1,11 @@
1
- import {Collection} from './collection';
2
- import {ArrayIterable} from './array-iterable';
3
1
  import {HashMap} from './hashmap';
2
+ import {AbstractSet} from './abstract-set';
3
+ import * as mutable from './mutable/hashset';
4
4
 
5
- export class HashSet<T> extends ArrayIterable<T, HashSet<T>>{
5
+ export class HashSet<T> extends AbstractSet<T, HashSet<T>> {
6
6
 
7
- protected fromArray(array: T[]): HashSet<T> {
8
- return HashSet.of(...array);
9
- }
10
-
11
- constructor(private readonly items: Set<T>) {
12
- super();
7
+ protected constructor(items: Set<T>) {
8
+ super(items);
13
9
  }
14
10
 
15
11
  static empty = new HashSet<any>(new Set());
@@ -18,56 +14,88 @@ export class HashSet<T> extends ArrayIterable<T, HashSet<T>>{
18
14
  return new HashSet<T>(new Set(items));
19
15
  }
20
16
 
21
- get toCollection(): Collection<T> {
22
- return new Collection(Array.from(this.items.keys()));
17
+ static from<T>(elements: Iterable<T>): HashSet<T> {
18
+ return new HashSet<T>(new Set(elements));
23
19
  }
24
20
 
25
- get toSet(): Set<T> {
26
- return this.items;
21
+ protected fromArray(array: T[]): HashSet<T> {
22
+ return HashSet.of(...array);
27
23
  }
28
24
 
29
- contains(item: T): boolean {
30
- return this.items.has(item);
25
+
26
+ toMap<K, V>(mapper: (item: T) => [K, V]): HashMap<K, V> {
27
+ return HashMap.of(...this.map(mapper).toArray);
31
28
  }
32
29
 
30
+
31
+ /**
32
+ * Builds a new HashSet by applying a function to all elements of this $coll.
33
+ *
34
+ * @param f the function to apply to each element.
35
+ * @tparam B the element type of the returned $coll.
36
+ * @return a new HashSet resulting from applying the given function
37
+ * `f` to each element of this HashSet and collecting the results.
38
+ */
33
39
  map<B>(f: (item: T) => B): HashSet<B> {
34
40
  return HashSet.of(...Array.from(this.items).map(i => f(i)));
35
41
  }
36
42
 
37
- flatMap<B>(f: (item: T) => HashSet<B>): HashSet<B> {
38
- let res = HashSet.empty ;
39
- this.items.forEach(i => {
40
- res = res.union(f(i));
41
- });
42
- return res;
43
- }
44
43
 
45
- get toArray(): Array<T> {
46
- return Array.from(this.items.keys());
47
- }
48
-
49
- get isEmpty(): boolean {
50
- return this.items.size <= 0;
44
+ /** Builds a new HashSet by applying a function to all elements of this HashSet
45
+ * and using the elements of the resulting collections.
46
+ *
47
+ * For example:
48
+ *
49
+ * ```
50
+ * getWords(lines: HashSet<string>): HashSet<string> {
51
+ * return lines.flatMap(line => HashSet.from(line.split("\\W+")))
52
+ * }
53
+ * ```
54
+ *
55
+ *
56
+ * @param f the function to apply to each element.
57
+ * @tparam B the element type of the returned collection.
58
+ * @return a new HashSet resulting from applying the given collection-valued function
59
+ * `f` to each element of this HashSet and concatenating the results.
60
+ */
61
+ flatMap<B>(f: (item: T) => HashSet<B>): HashSet<B> {
62
+ const res = new Set<B>();
63
+ this.items.forEach(i =>
64
+ f(i).foreach(e => res.add(e))
65
+ );
66
+ return new HashSet(res);
51
67
  }
52
68
 
53
- get size(): number {
54
- return this.items.size;
55
- }
56
69
 
57
- concat(other: ArrayIterable<T, any>): HashSet<T> {
58
- return this.appendedAll(other);
70
+ /** Creates a new $coll by adding all elements contained in another collection to this $coll, omitting duplicates.
71
+ *
72
+ * This method takes a collection of elements and adds all elements, omitting duplicates, into $coll.
73
+ *
74
+ * Example:
75
+ * {{{
76
+ * scala> val a = Set(1, 2) concat Set(2, 3)
77
+ * a: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
78
+ * }}}
79
+ *
80
+ * @param that the collection containing the elements to add.
81
+ * @return a new $coll with the given elements added, omitting duplicates.
82
+ */
83
+ concat(that: Iterable<T>): HashSet<T> {
84
+ return this.appendedAll(that);
59
85
  }
60
86
 
61
- union(other: ArrayIterable<T, any>): HashSet<T> {
87
+ union(other: Iterable<T>): HashSet<T> {
62
88
  return this.concat(other);
63
89
  }
64
90
 
65
91
  appended(item: T): HashSet<T> {
66
- return HashSet.of(...this.toArray.concat([item]));
92
+ return this.fromArray(this.toArray.concat([item]));
67
93
  }
68
94
 
69
- appendedAll(other: ArrayIterable<T, any>): HashSet<T> {
70
- return HashSet.of(...this.toArray.concat(other.toArray));
95
+ appendedAll(other: Iterable<T>): HashSet<T> {
96
+ const res = Array.from(this.items);
97
+ res.push(...Array.from(other));
98
+ return this.fromArray(res);
71
99
  }
72
100
 
73
101
  removed(item: T): HashSet<T> {
@@ -76,19 +104,31 @@ export class HashSet<T> extends ArrayIterable<T, HashSet<T>>{
76
104
  return new HashSet(res);
77
105
  }
78
106
 
79
- removedAll(other: HashSet<T>): HashSet<T> {
107
+ removedAll(other: Iterable<T>): HashSet<T> {
80
108
  const res = new Set(Array.from(this.items));
81
- other.foreach(i => res.delete(i));
109
+ for (const element of other) {
110
+ res.delete(element);
111
+ }
82
112
  return new HashSet(res);
83
113
  }
84
114
 
115
+ /**
116
+ * Computes the intersection between this set and another set.
117
+ *
118
+ * @param other the set to intersect with.
119
+ * @return a new set consisting of all elements that are both in this
120
+ * set and in the given set `that`.
121
+ */
85
122
  intersect(other: HashSet<T>): HashSet<T> {
86
123
  return this.filter(x => other.contains(x));
87
124
  }
88
125
 
89
- toMap<K, V>(mapper: (item: T) => [K, V]): HashMap<K, V> {
90
- return HashMap.of(...this.map(mapper).toArray);
126
+ /**
127
+ * Creates the immutable HashMap with the contents from this HashMap.
128
+ * @return immutable HashMap
129
+ */
130
+ get toMutable(): mutable.HashSet<T> {
131
+ return mutable.HashSet.of(...this.items);
91
132
  }
92
133
 
93
-
94
134
  }
package/src/index.ts CHANGED
@@ -5,3 +5,6 @@ export * from './hashset';
5
5
  export * from './option';
6
6
  export * from './try';
7
7
  export * from './util';
8
+
9
+ import * as mutable from './mutable';
10
+ export {mutable};
@@ -0,0 +1,247 @@
1
+ import {Option} from '../option';
2
+ import {AbstractMap, Tuple2} from '../abstract-map';
3
+ import * as immutable from '../hashmap';
4
+
5
+ export class HashMap<K, V> extends AbstractMap<K, V, HashMap<K, V>> {
6
+
7
+ constructor(map: Map<K, V> = new Map()) {
8
+ super(map);
9
+ }
10
+
11
+ protected fromArray(array: Tuple2<K, V>[]): HashMap<K, V> {
12
+ return HashMap.of(...array);
13
+ }
14
+
15
+ static of<K, V>(...values: Tuple2<K, V>[]): HashMap<K, V> {
16
+ return new HashMap<K, V>(new Map(values));
17
+ }
18
+
19
+ static from<K, V>(values: Iterable<Tuple2<K, V>>): HashMap<K, V> {
20
+ return HashMap.of(...Array.from(values));
21
+ }
22
+
23
+ /** Adds all elements produced by an Iterable to this HashMap.
24
+ *
25
+ * @param values the Iterable producing the elements to HashMap.
26
+ * @return the HashMap itself.
27
+ */
28
+ addAll(values: Iterable<Tuple2<K, V>>): this {
29
+ for (const [key, value] of values) {
30
+ this.map.set(key, value);
31
+ }
32
+ return this;
33
+ }
34
+
35
+
36
+ /**
37
+ * Update a mapping for the specified key and its current optionally-mapped value
38
+ * (`Some` if there is current mapping, `None` if not).
39
+ *
40
+ * If the remapping function returns `Some(v)`, the mapping is updated with the new value `v`.
41
+ * If the remapping function returns `None`, the mapping is removed (or remains absent if initially absent).
42
+ * If the function itself throws an exception, the exception is rethrown, and the current mapping is left unchanged.
43
+ *
44
+ * @param key the key value
45
+ * @param remappingFunction a partial function that receives current optionally-mapped value and return a new mapping
46
+ * @return the new value associated with the specified key
47
+ */
48
+ updateWith(key: K): (remappingFunction: (maybeValue: Option<V>) => Option<V>) => Option<V> {
49
+ const previousValue = this.get(key);
50
+
51
+ return (remappingFunction: (maybeValue: Option<V>) => Option<V>) => {
52
+ const nextValue = remappingFunction(previousValue);
53
+ if (previousValue.isEmpty && nextValue.isEmpty) {
54
+ return nextValue;
55
+ } else if (previousValue.isDefined && nextValue.isEmpty) {
56
+ this.remove(key);
57
+ return nextValue;
58
+ } else {
59
+ this.update(key, nextValue.get);
60
+ return nextValue;
61
+ }
62
+
63
+ };
64
+ }
65
+
66
+ /**
67
+ * Removes all elements produced by an iterator from this $coll.
68
+ *
69
+ * @param values the iterator producing the elements to remove.
70
+ * @return the hashmap itself
71
+ */
72
+ subtractAll(values: Iterable<K>): this {
73
+ if (this.isEmpty) {
74
+ return this;
75
+ }
76
+
77
+ for (const key of values) {
78
+ this.map.delete(key);
79
+ if (this.isEmpty) {
80
+ return this;
81
+ }
82
+ }
83
+
84
+ return this;
85
+ }
86
+
87
+
88
+ /** Clears the map's contents. After this operation, the
89
+ * map is empty.
90
+ */
91
+ clear(): void {
92
+ this.map.clear();
93
+ }
94
+
95
+ /**
96
+ * Creates new Hashmap with the values from this HashMap
97
+ * @return new Hashmap
98
+ */
99
+ clone(): HashMap<K, V> {
100
+ const contentClone = new Map(this.map);
101
+ return new HashMap<K, V>(contentClone);
102
+ }
103
+
104
+ /**
105
+ * Retains only those mappings for which the predicate
106
+ * `p` returns `true`.
107
+ *
108
+ * @param p The test predicate
109
+ */
110
+ filterInPlace(p: (entry: Tuple2<K, V>) => boolean): this {
111
+ if (this.nonEmpty) {
112
+ const entries = this.entries;
113
+ entries.foreach(e => {
114
+ if (!p(e)) {
115
+ this.remove(e[0]);
116
+ }
117
+ });
118
+ }
119
+ return this;
120
+ }
121
+
122
+ /**
123
+ * Applies a transformation function to all values contained in this map.
124
+ * The transformation function produces new values from existing keys
125
+ * associated values.
126
+ *
127
+ * @param f the transformation to apply
128
+ * @return the map itself.
129
+ */
130
+ mapValuesInPlace(f: (entry: Tuple2<K, V>) => V): this {
131
+ if (this.nonEmpty) {
132
+ const entries = this.entries;
133
+ entries.foreach(e => {
134
+ this.update(e[0], f(e));
135
+ });
136
+ }
137
+ return this;
138
+ }
139
+
140
+ /**
141
+ * If given key is already in this map, returns associated value.
142
+ *
143
+ * Otherwise, computes value from given expression `op`, stores with key
144
+ * in map and returns that value.
145
+ *
146
+ * Concurrent map implementations may evaluate the expression `op`
147
+ * multiple times, or may evaluate `op` without inserting the result.
148
+ *
149
+ * @param key the key to test
150
+ * @param defaultValue the computation yielding the value to associate with `key`, if
151
+ * `key` is previously unbound.
152
+ * @return the value associated with key (either previously or as a result
153
+ * of executing the method).
154
+ */
155
+ getOrElseUpdate(key: K, defaultValue: () => V): V {
156
+ return this.get(key).getOrElse(() => {
157
+ const newValue = defaultValue();
158
+ this.map.set(key, newValue);
159
+ return newValue;
160
+ });
161
+ }
162
+
163
+ /**
164
+ * Adds a single element to this map.
165
+ *
166
+ * @param key the element to add.
167
+ * @param value the element's value.
168
+ * @return the map itself
169
+ */
170
+ set(key: K, value: V): this {
171
+ this.map.set(key, value);
172
+ return this;
173
+ }
174
+
175
+ /**
176
+ * Adds a new key/value pair to this map and optionally returns previously bound value.
177
+ * If the map already contains a
178
+ * mapping for the key, it will be overridden by the new value.
179
+ *
180
+ * @param key the key to update
181
+ * @param value the new value
182
+ * @return an option value containing the value associated with the key
183
+ * before the `put` operation was executed, or `None` if `key`
184
+ * was not defined in the map before.
185
+ */
186
+ put(key: K, value: V): Option<V> {
187
+ const res = this.get(key);
188
+ this.map.set(key, value);
189
+ return res;
190
+ }
191
+
192
+ /**
193
+ * Removes a key from this map, returning the value associated previously
194
+ * with that key as an option.
195
+ * @param key the key to be removed
196
+ * @return an option value containing the value associated previously with `key`,
197
+ * or `None` if `key` was not defined in the map before.
198
+ */
199
+ remove(key: K): Option<V> {
200
+ const res = this.get(key);
201
+ this.subtractOne(key);
202
+ return res;
203
+ }
204
+
205
+ /**
206
+ * Adds a new key/value pair to this map.
207
+ * If the map already contains a
208
+ * mapping for the key, it will be overridden by the new value.
209
+ *
210
+ * @param key The key to update
211
+ * @param value The new value
212
+ */
213
+ update(key: K, value: V): void {
214
+ this.map.set(key, value);
215
+ }
216
+
217
+ /**
218
+ * Adds a single element to this map.
219
+ *
220
+ * @param elem the element to $=add.
221
+ * @return the map itself
222
+ */
223
+ addOne(elem: Tuple2<K, V>): this {
224
+ this.map.set(elem[0], elem[1]);
225
+ return this;
226
+ }
227
+
228
+ /**
229
+ * Removes a single element from this $coll.
230
+ *
231
+ * @param key the element to remove.
232
+ * @return the map itself
233
+ */
234
+ subtractOne(key: K): this {
235
+ this.map.delete(key);
236
+ return this;
237
+ }
238
+
239
+ /**
240
+ * Creates the immutable HashMap with the contents from this HashMap.
241
+ * @return immutable HashMap
242
+ */
243
+ get toImmutable(): immutable.HashMap<K, V> {
244
+ return immutable.HashMap.of(...this.map.entries());
245
+ }
246
+
247
+ }