2024-09-19 23:11:12 +00:00
|
|
|
package h
|
|
|
|
|
|
2024-10-29 10:48:13 +00:00
|
|
|
import (
|
|
|
|
|
"github.com/maddalax/htmgo/framework/datastructure/orderedmap"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-26 02:59:17 +00:00
|
|
|
// Unique returns a new slice with only unique items.
|
2024-09-19 23:11:12 +00:00
|
|
|
func Unique[T any](slice []T, key func(item T) string) []T {
|
|
|
|
|
var result []T
|
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
for _, v := range slice {
|
|
|
|
|
k := key(v)
|
|
|
|
|
if _, ok := seen[k]; !ok {
|
|
|
|
|
seen[k] = true
|
|
|
|
|
result = append(result, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 10:48:13 +00:00
|
|
|
// Find returns the first item in the slice that matches the predicate.
|
2024-10-28 23:47:00 +00:00
|
|
|
func Find[T any](slice []T, predicate func(item *T) bool) *T {
|
|
|
|
|
for _, v := range slice {
|
|
|
|
|
if predicate(&v) {
|
|
|
|
|
return &v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 10:48:13 +00:00
|
|
|
// GroupBy groups the items in the slice by the key returned by the key function.
|
|
|
|
|
func GroupBy[T any, K comparable](slice []T, key func(item T) K) map[K][]T {
|
|
|
|
|
grouped := make(map[K][]T)
|
|
|
|
|
for _, item := range slice {
|
|
|
|
|
k := key(item)
|
|
|
|
|
items, ok := grouped[k]
|
|
|
|
|
if !ok {
|
|
|
|
|
items = []T{}
|
|
|
|
|
}
|
|
|
|
|
grouped[k] = append(items, item)
|
|
|
|
|
}
|
|
|
|
|
return grouped
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GroupByOrdered groups the items in the slice by the key returned by the key function, and returns an Map.
|
|
|
|
|
func GroupByOrdered[T any, K comparable](slice []T, key func(item T) K) *orderedmap.Map[K, []T] {
|
|
|
|
|
grouped := orderedmap.New[K, []T]()
|
|
|
|
|
for _, item := range slice {
|
|
|
|
|
k := key(item)
|
|
|
|
|
items, ok := grouped.Get(k)
|
|
|
|
|
if !ok {
|
|
|
|
|
items = []T{}
|
|
|
|
|
}
|
|
|
|
|
grouped.Set(k, append(items, item))
|
|
|
|
|
}
|
|
|
|
|
return grouped
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-26 02:59:17 +00:00
|
|
|
// Filter returns a new slice with only items that match the predicate.
|
2024-09-19 23:11:12 +00:00
|
|
|
func Filter[T any](slice []T, predicate func(item T) bool) []T {
|
|
|
|
|
var result []T
|
|
|
|
|
for _, v := range slice {
|
|
|
|
|
if predicate(v) {
|
|
|
|
|
result = append(result, v)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-26 02:59:17 +00:00
|
|
|
// Map returns a new slice with the results of the mapper function.
|
2024-09-19 23:11:12 +00:00
|
|
|
func Map[T, U any](slice []T, mapper func(item T) U) []U {
|
|
|
|
|
var result []U
|
|
|
|
|
for _, v := range slice {
|
|
|
|
|
result = append(result, mapper(v))
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|