github.com/richardwilkes/toolbox@v1.121.0/collection/dict/map.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package dict
    11  
    12  // Functions in this package are only here because the Go team decided not to bring them over in the Go 1.21 maps
    13  // package when they migrated the existing code from golang.org/x/exp/maps. Why, I'm not sure, since these can be
    14  // useful.
    15  //
    16  // I chose not to use the package name "maps" to avoid collisions with the standard library. Unlike with the "slices"
    17  // package, though, I couldn't use "map", since that's a keyword.
    18  
    19  // Keys returns the keys of the map m. The keys will be in an indeterminate order.
    20  func Keys[M ~map[K]V, K comparable, V any](m M) []K {
    21  	r := make([]K, 0, len(m))
    22  	for k := range m {
    23  		r = append(r, k)
    24  	}
    25  	return r
    26  }
    27  
    28  // Values returns the values of the map m. The values will be in an indeterminate order.
    29  func Values[M ~map[K]V, K comparable, V any](m M) []V {
    30  	r := make([]V, 0, len(m))
    31  	for _, v := range m {
    32  		r = append(r, v)
    33  	}
    34  	return r
    35  }