ronantakizawa/codereview-bench huggingface dataset:
Repository: samber/lo
File: slice_test.go (Go)

Code before:
	is.True(IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))
	is.True(IsSorted([]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}))

	is.False(IsSorted([]int{0, 1, 4, 3, 2, 5, 6, 7, 8, 9, 10}))
	is.False(IsSorted([]string{"a", "b", "d", "c", "e", "f", "g", "h", "i", "j"}))
}

func TestIsSortedByKey(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	is.True(IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
		return len(s)
	}))

	is.False(IsSortedByKey([]string{"aa", "b", "ccc"}, func(s string) int {
		return len(s)
	}))

	is.True(IsSortedByKey([]string{"1", "2", "3", "11"}, func(s string) int {
		ret, _ := strconv.Atoi(s)
		return ret
	}))
}

func TestInsertSlice(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	inserts := Splice([]string{"a", "d"}, 1, "b", "c")
	is.Equal(inserts, []string{"a", "b", "c", "d"})
	is.True(len(inserts) == 4 && inserts[1] == "b" && inserts[2] == "c")

	inserti := Splice([]int{1, 4}, 1, 2, 3)
	is.Equal(inserti, []int{1, 2, 3, 4})
	is.True(len(inserti) == 4 && inserti[1] == 2 && inserti[2] == 3)
}


Review comment (suggestion):
```suggestion
func TestSplice(t *testing.T) {
```

Code after:
	is.True(IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))
	is.True(IsSorted([]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}))

	is.False(IsSorted([]int{0, 1, 4, 3, 2, 5, 6, 7, 8, 9, 10}))
	is.False(IsSorted([]string{"a", "b", "d", "c", "e", "f", "g", "h", "i", "j"}))
}

func TestIsSortedByKey(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	is.True(IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
		return len(s)
	}))

	is.False(IsSortedByKey([]string{"aa", "b", "ccc"}, func(s string) int {
		return len(s)
	}))

	is.True(IsSortedByKey([]string{"1", "2", "3", "11"}, func(s string) int {
		ret, _ := strconv.Atoi(s)
		return ret
	}))
}

func TestSplice(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	sample := []string{"a", "b", "c", "d", "e", "f", "g"}

	// normal case
	results := Splice(sample, 1, "1", "2")
	is.Equal([]string{"a", "b", "c", "d", "e", "f", "g"}, sample)
	is.Equal([]string{"a", "1", "2", "b", "c", "d", "e", "f", "g"}, results)

	// check there is no side effect
	results = Splice(sample, 1)
	results[0] = "b"
	is.Equal([]string{"a", "b", "c", "d", "e", "f", "g"}, sample)

	// positive overflow
	results = Splice(sample, 42, "1", "2")
	is.Equal([]string{"a", "b", "c", "d", "e", "f", "g"}, sample)
	is.Equal(results, []string{"a", "b", "c", "d", "e", "f", "g", "1", "2"})

	// negative overflow
	results = Splice(sample, -42, "1", "2")
	is.Equal([]string{"a", "b", "c", "d", "e", "f", "g"}, sample)
	is.Equal(results, []string{"1", "2", "a", "b", "c", "d", "e", "f", "g"})
Repository: samber/lo
File: retry.go (Go)

Code before:
}

func (th *throttle) purge(forcePurge bool) {
	th.mu.Lock()
	defer th.mu.Unlock()

	if th.timer != nil {
		th.timer.Stop()
	}

	if th.needInvoke || forcePurge {
		for _, f := range th.callbacks {
			go f()
		}
		th.needInvoke = false
		th.timer = time.AfterFunc(th.interval, func() {
			th.purge(false)
		})
	} else {
		th.timer = nil
	}
}

// NewThrottle creates a throttled instance that invokes given functions only once in every interval.
// This returns 2 functions, First one is throttled function and Second one is purge function which invokes given functions immediately and reset interval timer.
func NewThrottle(interval time.Duration, f ...func()) (func(), func()) {
	th := &throttle{
		mu:        new(sync.Mutex),
		interval:  interval,
		callbacks: f,
	}
	return th.throttledFunc, func() {
		th.purge(true)
	}
}


Review comment (refactor):
I'm also think about a `NewThrottle(100ms, 3, cb)` that would trigger the first 3 calls, then ignore others during 100ms. Like we do for a basic rate limiter.

Maybe it is a different helper.

WDYT?


Code after:
		}

	}
	if th.timer == nil {
		th.timer = time.AfterFunc(th.interval, func() {
			th.reset()
		})
	}
}

func (th *throttle) reset() {
	th.mu.Lock()
	defer th.mu.Unlock()

	if th.timer != nil {
		th.timer.Stop()
	}

	th.count = 0
	th.timer = nil

}

// NewThrottle creates a throttled instance that invokes given functions only once in every interval.
// This returns 2 functions, First one is throttled function and Second one is a function to reset interval
func NewThrottle(interval time.Duration, f ...func()) (func(), func()) {
	return NewThrottleWithCount(interval, 1, f...)
}

// NewThrottleWithCount is NewThrottle with count limit, throttled function will be invoked count times in every interval.
func NewThrottleWithCount(interval time.Duration, count int, f ...func()) (func(), func()) {
	if count <= 0 {
		count = 1
	}
	th := &throttle{
		mu:         new(sync.Mutex),
		interval:   interval,
		callbacks:  f,
		countLimit: count,
	}
	return th.throttledFunc, th.reset
}
Repository: samber/lo
File: find.go (Go)

Code before:
	return collection[length-1], nil
}

// Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element
// from the end is returned. An error is returned when nth is out of slice bounds.
func Nth[T any, N constraints.Integer](collection []T, nth N) (T, error) {
	n := int(nth)
	l := len(collection)
	if n >= l || -n > l {
		var t T
		return t, fmt.Errorf("nth: %d out of slice bounds", n)
	}

	if n >= 0 {
		return collection[n], nil
	}
	return collection[l+n], nil
}

// Sample returns a random item from collection.
func Sample[T any](collection []T) T {
	size := len(collection)
	if size == 0 {
		return Empty[T]()
	}

	return collection[rand.Intn(size)]
}

// Samples returns N random unique items from collection.
func Samples[T any](collection []T, count int) []T {
	size := len(collection)

	copy := append([]T{}, collection...)

	results := []T{}

	for i := 0; i < size && i < count; i++ {
		copyLength := size - i

		index := rand.Intn(size - i)
		results = append(results, copy[index])

		// Removes element.
		// It is faster to swap with last element and remove it.
		copy[index] = copy[copyLength-1]
		copy = copy[:copyLength-1]
	}

	return results
}

Review comment (suggestion):
Is there any reason you use for I++ loop and not a range ?


```suggestion
for _, item := range items {
          if item.After(max) { 
                   max = item 
          }
}
```


Code after:

	max = collection[0]

	for i := 1; i < len(collection); i++ {
		item := collection[i]

		if comparison(item, max) {
			max = item
		}
	}

	return max
}

// Latest search the maximum time.Time of a collection.
// Returns zero value when the collection is empty.
func Latest(times ...time.Time) time.Time {
	var max time.Time

	if len(times) == 0 {
		return max
	}

	max = times[0]

	for i := 1; i < len(times); i++ {
		item := times[i]

		if item.After(max) {
			max = item
		}
	}

	return max
}

// Last returns the last element of a collection or error if empty.
func Last[T any](collection []T) (T, error) {
	length := len(collection)

	if length == 0 {
		var t T
		return t, fmt.Errorf("last: cannot extract the last element of an empty slice")
	}

	return collection[length-1], nil
}

// Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element
// from the end is returned. An error is returned when nth is out of slice bounds.
func Nth[T any, N constraints.Integer](collection []T, nth N) (T, error) {
Repository: samber/lo
File: map.go (Go)

Code before:
package lo

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)
	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}
	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in map[K]V) []V {
	result := make([]V, 0, len(in))

	for k := range in {
		result = append(result, in[k])
	}


Review comment (suggestion):
You are introducing a variadic

It has consequences.

Especially if you consider someone may provide nothing. By calling Keys()

So you should either return an error, but it would change the method signature something you cannot.

Or you could return an empty slice

But, in both cases you need to add tests for this

Code after:
package lo

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}

	return result
}

// UniqKeys creates an array of unique keys in the map.
// Play: https://go.dev/play/p/TPKAb6ILdHk
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
	seen := make(map[K]struct{})
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; exists {
				continue
			}
			seen[k] = struct{}{}
			result = append(result, k)
		}
Repository: samber/lo
File: map.go (Go)

Code before:
package lo

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)
	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}
	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in map[K]V) []V {
	result := make([]V, 0, len(in))

	for k := range in {
		result = append(result, in[k])
	}

	return result
}

// ValueOr returns the value of the given key or the fallback value if the key is not present.
// Play: https://go.dev/play/p/bAq9mHErB4V

Review comment (refactor):
Your implementation has issues.

If you are providing multiple map with same keys you will have the same key more than once.

It might be OK or not compared to what you wanted to do.

You can take a loot at the issue reported by @samber he was mentioning the possible "collision"

If you want to avoid the duplicates you can either use a map to put the key, then loop on the map. Or use slices.Compact

This has to be covered by tests, no matter what you decide

Code after:
package lo

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}

	return result
}

// UniqKeys creates an array of unique keys in the map.
// Play: https://go.dev/play/p/TPKAb6ILdHk
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
	seen := make(map[K]struct{})
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; exists {
				continue
			}
			seen[k] = struct{}{}
			result = append(result, k)
		}
	}

	return result
}
Repository: samber/lo
File: map.go (Go)

Code before:
package lo

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}

	return result
}

// UniqKeys creates an array of unique keys in the map.
// Play:
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
	seen := make(map[K]bool)
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; !exists {
				seen[k] = true
				result = append(result, k)
			}
		}
	}

	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
	}

Review comment (performance):
The canonical way to do it is to use a map[K]struct{}


```suggestion
	seen := make(map[K]struct{})
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; !exists {
				seen[k] = struct{}{}
```

This is supposed to be the most memory efficient way


Code after:
package lo

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}

	return result
}

// UniqKeys creates an array of unique keys in the map.
// Play: https://go.dev/play/p/TPKAb6ILdHk
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
	seen := make(map[K]struct{})
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; exists {
				continue
			}
			seen[k] = struct{}{}
			result = append(result, k)
		}
	}

	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
Repository: samber/lo
File: map.go (Go)

Code before:
// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}

	return result
}

// UniqKeys creates an array of unique keys in the map.
// Play:
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
	seen := make(map[K]bool)
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; !exists {
				seen[k] = true
				result = append(result, k)
			}
		}
	}

	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
	}

	return result

Review comment (suggestion):
```suggestion
			if _, exists := seen[k]; exists {
				continue
			}
			seen[k] = true
			result = append(result, k)
```


Code after:
// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}

	return result
}

// UniqKeys creates an array of unique keys in the map.
// Play: https://go.dev/play/p/TPKAb6ILdHk
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
	seen := make(map[K]struct{})
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; exists {
				continue
			}
			seen[k] = struct{}{}
			result = append(result, k)
		}
	}

	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
	}
Repository: samber/lo
File: map.go (Go)

Code before:
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
	}

	return result
}

// UniqValues creates an array of unique values in the map.
// Play:
func UniqValues[K comparable, V comparable](in ...map[K]V) []V {
	seen := make(map[V]bool)
	result := make([]V, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[in[i][k]]; !exists {
				seen[in[i][k]] = true
				result = append(result, in[i][k])
			}
		}
	}

	return result
}

// ValueOr returns the value of the given key or the fallback value if the key is not present.
// Play: https://go.dev/play/p/bAq9mHErB4V
func ValueOr[K comparable, V any](in map[K]V, key K, fallback V) V {
	if v, ok := in[key]; ok {
		return v
	}
	return fallback
}

// PickBy returns same map type filtered by given predicate.
// Play: https://go.dev/play/p/kdg8GR_QMmf
func PickBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map {
	r := Map{}
	for k := range in {
		if predicate(k, in[k]) {
			r[k] = in[k]
		}
	}
	return r
}


Review comment (suggestion):
Using a temporary variable may improve the readability

```suggestion
	for i := range in {
		for k := range in[i] {{
			key := in[i][k]
			if _, exists := seen[key]; !exists {
				continue
			}
			seen[key] = true
			result = append(result, key)
		}
 	}
```

Please note the remark on map[K]struct{} may apply

Code after:
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
	}

	return result
}

// UniqValues creates an array of unique values in the map.
// Play: https://go.dev/play/p/nf6bXMh7rM3
func UniqValues[K comparable, V comparable](in ...map[K]V) []V {
	seen := make(map[V]struct{})
	result := make([]V, 0)

	for i := range in {
		for k := range in[i] {
			val := in[i][k]
			if _, exists := seen[val]; exists {
				continue
			}
			seen[val] = struct{}{}
			result = append(result, val)
		}
	}

	return result
}

// ValueOr returns the value of the given key or the fallback value if the key is not present.
// Play: https://go.dev/play/p/bAq9mHErB4V
func ValueOr[K comparable, V any](in map[K]V, key K, fallback V) V {
	if v, ok := in[key]; ok {
		return v
	}
	return fallback
}

// PickBy returns same map type filtered by given predicate.
// Play: https://go.dev/play/p/kdg8GR_QMmf
func PickBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map {
	r := Map{}
	for k := range in {
		if predicate(k, in[k]) {
			r[k] = in[k]
		}
	}
Repository: samber/lo
File: map.go (Go)

Code before:

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}

	return result
}

// UniqKeys creates an array of unique keys in the map.
// Play: https://go.dev/play/p/RyIE6Pb5dVS
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
	seen := make(map[K]struct{})
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; !exists {
				seen[k] = struct{}{}
				result = append(result, k)
			}
		}
	}

	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
	}


Review comment (suggestion):
```suggestion
			_, exists := seen[k]
			if exists {
				continue
			}
			seen[k] = struct{}{}
			result = append(result, k)
```


Code after:

// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			result = append(result, k)
		}
	}

	return result
}

// UniqKeys creates an array of unique keys in the map.
// Play: https://go.dev/play/p/TPKAb6ILdHk
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
	seen := make(map[K]struct{})
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; exists {
				continue
			}
			seen[k] = struct{}{}
			result = append(result, k)
		}
	}

	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
	}
Repository: samber/lo
File: map.go (Go)

Code before:

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in map[K]V) []V {
	result := make([]V, 0, len(in))

	for k := range in {
		result = append(result, in[k])
	}

	return result
}

// ValueOr returns the value of the given key or the fallback value if the key is not present.
// Play: https://go.dev/play/p/bAq9mHErB4V
func ValueOr[K comparable, V any](in map[K]V, key K, fallback V) V {
	if v, ok := in[key]; ok {
		return v
	}
	return fallback
}

// PickBy returns same map type filtered by given predicate.
// Play: https://go.dev/play/p/kdg8GR_QMmf
func PickBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map {
	r := Map{}
	for k := range in {
		if predicate(k, in[k]) {
			r[k] = in[k]
		}
	}
	return r
}

// PickByKeys returns same map type filtered by given keys.
// Play: https://go.dev/play/p/R1imbuci9qU
func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map {
	r := Map{}
	for i := range keys {
		if v, ok := in[keys[i]]; ok {
			r[keys[i]] = v
		}
	}
	return r
}

// PickByValues returns same map type filtered by given values.
// Play: https://go.dev/play/p/1zdzSvbfsJc
func PickByValues[K comparable, V comparable, Map ~map[K]V](in Map, values []V) Map {
	r := Map{}
	for k := range in {

Review comment (suggestion):
need calculate capacity 
```go
count := 0
for i := range in {
    size += len(in[i])
}
result := make([]V, 0, count)
```

Code after:
	result := make([]K, 0)

	for i := range in {
		for k := range in[i] {
			if _, exists := seen[k]; exists {
				continue
			}
			seen[k] = struct{}{}
			result = append(result, k)
		}
	}

	return result
}

// HasKey returns whether the given key exists.
// Play: https://go.dev/play/p/aVwubIvECqS
func HasKey[K comparable, V any](in map[K]V, key K) bool {
	_, ok := in[key]
	return ok
}

// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
	result := make([]V, 0, len(in))

	for i := range in {
		for k := range in[i] {
			result = append(result, in[i][k])
		}
	}

	return result
}

// UniqValues creates an array of unique values in the map.
// Play: https://go.dev/play/p/nf6bXMh7rM3
func UniqValues[K comparable, V comparable](in ...map[K]V) []V {
	seen := make(map[V]struct{})
	result := make([]V, 0)

	for i := range in {
		for k := range in[i] {
			val := in[i][k]
			if _, exists := seen[val]; exists {
				continue
			}
			seen[val] = struct{}{}
			result = append(result, val)
		}