Skip to content

Commit

Permalink
reorder things
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jul 24, 2022
1 parent bc62c3c commit 03b4947
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@ func KeyBy[K comparable, V any](collection []V, iteratee func(V) K) map[K]V {
return result
}

// Associate returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
// If any of two pairs would have the same key the last one gets added to the map.
// The returned map preserves the entry iteration order of the original array.
func Associate[T any, K comparable, V any](collection []T, transform func(T) (K, V)) map[K]V {
result := make(map[K]V)

for _, t := range collection {
k, v := transform(t)
result[k] = v
}

return result
}

// Drop drops n elements from the beginning of a slice or array.
func Drop[T any](collection []T, n int) []T {
if len(collection) <= n {
Expand Down Expand Up @@ -446,17 +460,3 @@ func Compact[T comparable](collection []T) []T {

return result
}

// Associate returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
// If any of two pairs would have the same key the last one gets added to the map.
// The returned map preserves the entry iteration order of the original array.
func Associate[T any, K comparable, V any](collection []T, transform func(T) (K, V)) map[K]V {
result := make(map[K]V)

for _, t := range collection {
k, v := transform(t)
result[k] = v
}

return result
}

0 comments on commit 03b4947

Please sign in to comment.