Open hackwaly opened 6 days ago
defaultmap is?
And our map can have four versions: sorted_multimap hash_multimap immut/sorted_multimap immut/hash_multimap, which one is more necessary to do?
What about multiset?
A defaultmap is useful in this case:
let map = DefaultMap::new(fn (_key) { 0 })
map[non_exist_key] += 1
Programming languages with extension methods support is easy to solve the A x B problem.
trait Map[K, V] {
get(Self, K) -> V
set(Self, K, V) -> Unit
}
trait MultiValue[T] {
empty() -> Self
add(Self, T) -> Self
}
pub fn add[S: Map[K, M], K, V, M: MultiValue[V]](self: S, key: K, value: V) {
self.set(key, self.get(key).or(M::empty()).add(value))
}
A defaultmap is useful in this case:
let map = DefaultMap::new(fn (_key) { 0 }) map[non_exist_key] += 1
Programming languages with extension methods support is easy to solve the A x B problem.
trait Map[K, V] { get(Self, K) -> V set(Self, K, V) -> Unit } trait MultiValue[T] { empty() -> Self add(Self, T) -> Self } pub fn add[S: Map[K, M], K, V, M: MultiValue[V]](self: S, key: K, value: V) { self.set(key, self.get(key).or(M::empty()).add(value)) }
I agree ur idea! But a funny thing is that the map's API is inconsistent, so maybe Map trait need to do but not now until this issue resolved.
The usages is so widely and missing them makes developers have to write boilerplates repeatedly.