Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
abeecfd797 | |||
5735640ab6 | |||
26ea846e93 | |||
0592add8ca |
27
filter.go
27
filter.go
@ -9,3 +9,30 @@ func Filter[T any](slice []T, f func(T) bool) []T {
|
|||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FindFirst[T any](slice []T, f func(T) bool) (T, bool) {
|
||||||
|
for _, v := range slice {
|
||||||
|
if f(v) {
|
||||||
|
return v, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *new(T), false
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindFirstIndex[T any](slice []T, f func(T) bool) (int, bool) {
|
||||||
|
for i, v := range slice {
|
||||||
|
if f(v) {
|
||||||
|
return i, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindLastIndex[T any](slice []T, f func(T) bool) (int, bool) {
|
||||||
|
for i := len(slice); i >= 0; i-- {
|
||||||
|
if f(slice[i]) {
|
||||||
|
return i, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1, false
|
||||||
|
}
|
||||||
|
12
map.go
12
map.go
@ -7,3 +7,15 @@ func Map[I, O any](slice []I, mapper func(I) O) []O {
|
|||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MapError[I, O any](slice []I, mapper func(I) (O, error)) ([]O, error) {
|
||||||
|
ret := make([]O, 0, len(slice))
|
||||||
|
for _, old := range slice {
|
||||||
|
new, err := mapper(old)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ret = append(ret, new)
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
12
of.go
12
of.go
@ -14,3 +14,15 @@ func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) []T {
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmapKey is an unmapper function which returns the map key only
|
||||||
|
// and discards its value. It is supposed to be used with OfMap
|
||||||
|
func UnmapKey[K comparable, V any](key K, _ V) K {
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmapValue is an unmapper function which returns the map value only
|
||||||
|
// and discards its key. It is supposed to be used with OfMap
|
||||||
|
func UnmapValue[K comparable, V any](_ K, value V) V {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user