Skip to content

Instantly share code, notes, and snippets.

@WimJongeneel
Last active October 27, 2022 22:58
Show Gist options
  • Save WimJongeneel/9b033b7ce801aa7686c73b1f97fc4c32 to your computer and use it in GitHub Desktop.
Save WimJongeneel/9b033b7ce801aa7686c73b1f97fc4c32 to your computer and use it in GitHub Desktop.
class BetterMap<k, v> extends Map<k, v> {
update(key:k, updater: (v:v, k:k) => v, notset:v = undefined) {
if(this.has(key)) this.set(key, updater(this.get(key), key))
else this.set(key, notset)
}
filter(predicate: (v:v, k:k) => boolean) {
const newMap = new BetterMap<k, v>()
const entries = Array.from(this.entries())
for(const [key, value] of entries) {
if(predicate(value, key)) newMap.set(key, value)
}
return newMap
}
merge(map: Map<k, v>, resolve: (k:k, a:v, b:v) => v = (k, a, b) => b) {
const entries = Array.from(map.entries())
for(const [key, value] of entries) {
if(this.has(key)) this.set(key, resolve(key, this.get(key), value))
else this.set(key, value)
}
}
}
class BetterSet<v> extends Set<v> {
filter(predicate: (v: v) => boolean) {
const newSet = new BetterSet<v>()
const entries = Array.from(this.entries())
for(const [value] of entries) {
if (predicate(value)) newSet.add(value)
}
return newSet
}
merge(set: Set<v>) {
const entries = Array.from(set.entries())
for (const kv of entries) {
this.add(kv[0])
}
}
except(set: Set<v>) {
const newSet = new Set<v>()
const entries = Array.from(set.entries())
for(const [value] of entries) {
if(!this.has(value)) newSet.add(value)
}
return newSet
}
both(set: Set<v>) {
const newSet = new Set<v>()
const entries = Array.from(set.entries())
for(const [value] of entries) {
if (this.has(value)) newSet.add(value)
}
return newSet
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment