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

[FEATURE REQUEST] - Introduce functions Keys, Values, AsMap, Clone #146

Open
oarkflow opened this issue Aug 7, 2024 · 2 comments
Open
Labels
enhancement New feature or request question Further information is requested

Comments

@oarkflow
Copy link

oarkflow commented Aug 7, 2024

It would be great if there's support for some functions in MapOf and Map like

  • Keys() []K - Returning keys
  • Values() []V - Returning values
  • AsMap() map[K]V - Returning all data as standard map data
  • Clone

These functions would be helpful

@sujit-baniya
Copy link

// Keys returns a slice of all keys in the map.
func (m *Map[K, V]) Keys() []K {
    keys := make([]K, 0, m.Len())
    m.Range(func(key K, value V) bool {
        keys = append(keys, key)
        return true
    })
    return keys
}

// Values returns a slice of all values in the map.
func (m *Map[K, V]) Values() []V {
    values := make([]V, 0, m.Len())
    m.Range(func(key K, value V) bool {
        values = append(values, value)
        return true
    })
    return values
}

// AsMap returns the entire map as a standard Go map.
func (m *Map[K, V]) AsMap() map[K]V {
    stdMap := make(map[K]V, m.Len())
    m.Range(func(key K, value V) bool {
        stdMap[key] = value
        return true
    })
    return stdMap
}

// Clone creates a deep copy of the map.
func (m *Map[K, V]) Clone() *Map[K, V] {
    clone := NewMap[K, V]()
    m.Range(func(key K, value V) bool {
        clone.Store(key, value)
        return true
    })
    return clone
}

@puzpuzpuz
Copy link
Owner

All of these functions can be easily implemented over Range. I don't like the idea of having them built-in as they're expensive in many aspects and having them will promote their usage. It's better to have a smaller, but more efficient API surface and let people build on top of it if they need something extra.

@puzpuzpuz puzpuzpuz added enhancement New feature or request question Further information is requested labels Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants