scats
Version:
Useful scala classes in typescript
62 lines (61 loc) • 1.68 kB
JavaScript
import { HashMap } from './hashmap.js';
import { AbstractSet } from './abstract-set.js';
import * as mutable from './mutable/hashset.js';
export class HashSet extends AbstractSet {
constructor(items) {
super(items);
}
static empty = new HashSet(new Set());
static of(...items) {
return new HashSet(new Set(items));
}
static from(elements) {
return new HashSet(new Set(elements));
}
fromArray(array) {
return HashSet.of(...array);
}
toMap(mapper) {
return HashMap.of(...this.map(mapper).toArray);
}
map(f) {
return HashSet.of(...Array.from(this.items).map(i => f(i)));
}
flatMap(f) {
const res = new Set();
this.items.forEach(i => f(i).foreach(e => res.add(e)));
return new HashSet(res);
}
concat(that) {
return this.appendedAll(that);
}
union(other) {
return this.concat(other);
}
appended(item) {
return this.fromArray(this.toArray.concat([item]));
}
appendedAll(other) {
const res = Array.from(this.items);
res.push(...Array.from(other));
return this.fromArray(res);
}
removed(item) {
const res = new Set(Array.from(this.items));
res.delete(item);
return new HashSet(res);
}
removedAll(other) {
const res = new Set(Array.from(this.items));
for (const element of other) {
res.delete(element);
}
return new HashSet(res);
}
intersect(other) {
return this.filter(x => other.contains(x));
}
get toMutable() {
return mutable.HashSet.of(...this.items);
}
}