1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package slice
- func RemoveRepeatElement[T comparable](elements []T) []T {
- if elements == nil || len(elements) == 0 {
- return elements
- }
- result := make([]T, 0)
- tempMap := make(map[T]interface{})
- for _, element := range elements {
- l := len(tempMap)
- tempMap[element] = nil
- if len(tempMap) != l {
- result = append(result, element)
- }
- }
- return result
- }
- func ToMap[S any, K comparable, V any](slice []S, fn func(sliceValue S) (K, V, error)) (map[K]V, error) {
- m := make(map[K]V, len(slice))
- for _, v := range slice {
- key, value, err := fn(v)
- if err != nil {
- return nil, err
- }
- m[key] = value
- }
- return m, nil
- }
- func ExtractValue[T any, R any](s []T, addTo func(v T) (R, bool)) []R {
- retSlice := make([]R, 0)
- for _, v := range s {
- retValue, add := addTo(v)
- if add {
- retSlice = append(retSlice, retValue)
- }
- }
- return retSlice
- }
|