// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package moremaps contains more functions for working with maps.
package moremaps import ( ) // Arbitrary returns an arbitrary (key, value) entry from the map and ok is true, if // the map is not empty. Otherwise, it returns zero values for K and V, and false. func [ comparable, any]( map[]) ( , , bool) { for , := range { return , , true } return } // Group returns a new non-nil map containing the elements of s grouped by the // keys returned from the key func. func [ comparable, any]( [], func() ) map[][] { := make(map[][]) for , := range { := () [] = append([], ) } return } // KeySlice returns the keys of the map M, like slices.Collect(maps.Keys(m)). func [ ~map[], comparable, any]( ) [] { := make([], 0, len()) for := range { = append(, ) } return } // ValueSlice returns the values of the map M, like slices.Collect(maps.Values(m)). func [ ~map[], comparable, any]( ) [] { := make([], 0, len()) for , := range { = append(, ) } return } // SameKeys reports whether x and y have equal sets of keys. func [ comparable, , any]( map[], map[]) bool { := func(, ) bool { return true } return maps.EqualFunc(, , ) } // Sorted returns an iterator over the entries of m in key order. func [ ~map[], cmp.Ordered, any]( ) iter.Seq2[, ] { // TODO(adonovan): use maps.Sorted if proposal #68598 is accepted. return func( func(, ) bool) { := KeySlice() slices.Sort() for , := range { if !(, []) { break } } } } // SortedFunc returns an iterator over the entries of m in the key order determined by cmp. func [ ~map[], comparable, any]( , func(, ) int) iter.Seq2[, ] { // TODO(adonovan): use maps.SortedFunc if proposal #68598 is accepted. return func( func(, ) bool) { := KeySlice() slices.SortFunc(, ) for , := range { if !(, []) { break } } } } // Delete is like delete(m, k) but reports whether deletion occurred. func [ ~map[], comparable, any]( , ) bool { := len() delete(, ) return != len() } // Entry is a key-value pair obtained from a map. type Entry[ comparable, any] struct { Key Value } // Entries returns a new unordered array of the entries of a map. func [ ~map[], comparable, any]( ) []Entry[, ] { := make([]Entry[, ], 0, len()) for , := range { = append(, Entry[, ]{, }) } return } // FromEntries returns a new map into which the entries have been inserted in order. func [ comparable, any]( []Entry[, ]) map[] { := make(map[], len()) for , := range { [.Key] = .Value } return }