github.com/go-playground/pkg/v5@v5.29.1/map/map.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  package mapext
     5  
     6  // Retain retains only the elements specified by the function and removes others.
     7  func Retain[K comparable, V any](m map[K]V, fn func(key K, value V) bool) {
     8  	for k, v := range m {
     9  		if fn(k, v) {
    10  			continue
    11  		}
    12  		delete(m, k)
    13  	}
    14  }
    15  
    16  // Map allows mapping of a map[K]V -> U.
    17  func Map[K comparable, V any, U any](m map[K]V, init U, fn func(accum U, key K, value V) U) U {
    18  	accum := init
    19  	for k, v := range m {
    20  		accum = fn(accum, k, v)
    21  	}
    22  	return accum
    23  }