Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide multimap and defaultmap #1200

Open
hackwaly opened this issue Nov 6, 2024 · 3 comments
Open

provide multimap and defaultmap #1200

hackwaly opened this issue Nov 6, 2024 · 3 comments

Comments

@hackwaly
Copy link
Contributor

hackwaly commented Nov 6, 2024

The usages is so widely and missing them makes developers have to write boilerplates repeatedly.

@Lampese
Copy link
Collaborator

Lampese commented Nov 6, 2024

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?

@hackwaly
Copy link
Contributor Author

hackwaly commented Nov 6, 2024

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))
}

@Lampese
Copy link
Collaborator

Lampese commented Nov 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants