mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-25 13:12:11 +02:00
fn: Added map functions
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
This commit is contained in:
44
fn/map.go
Normal file
44
fn/map.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package fn
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
// KeySet converts a map into a Set containing the keys of the map.
|
||||
func KeySet[K comparable, V any](m map[K]V) Set[K] {
|
||||
return NewSet(maps.Keys(m)...)
|
||||
}
|
||||
|
||||
// NewSubMapIntersect returns a sub-map of `m` containing only the keys found in
|
||||
// both `m` and the `keys` slice.
|
||||
func NewSubMapIntersect[K comparable, V any](m map[K]V, keys []K) map[K]V {
|
||||
result := make(map[K]V)
|
||||
for _, k := range keys {
|
||||
v, ok := m[k]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
result[k] = v
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// NewSubMap creates a sub-map from a given map using specified keys. It errors
|
||||
// if any of the keys is not found in the map.
|
||||
func NewSubMap[K comparable, V any](m map[K]V, keys []K) (map[K]V, error) {
|
||||
result := make(map[K]V, len(keys))
|
||||
for _, k := range keys {
|
||||
v, ok := m[k]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("NewSubMap: missing key %v", k)
|
||||
}
|
||||
|
||||
result[k] = v
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
Reference in New Issue
Block a user