Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
3901e3934f | |||
abeecfd797 | |||
5735640ab6 |
18
filter.go
18
filter.go
@ -19,12 +19,20 @@ func FindFirst[T any](slice []T, f func(T) bool) (T, bool) {
|
|||||||
return *new(T), false
|
return *new(T), false
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindLast[T any](slice []T, f func(T) bool) (T, bool) {
|
func FindFirstIndex[T any](slice []T, f func(T) bool) (int, bool) {
|
||||||
for i := len(slice); i >= 0; i-- {
|
for i, v := range slice {
|
||||||
v := slice[i]
|
|
||||||
if f(v) {
|
if f(v) {
|
||||||
return v, true
|
return i, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return *new(T), false
|
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
|
||||||
}
|
}
|
||||||
|
4
go.mod
4
go.mod
@ -1,5 +1,5 @@
|
|||||||
module git.milar.in/milarin/slices
|
module git.tordarus.net/tordarus/slices
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require git.milar.in/milarin/gmath v0.0.3
|
require git.tordarus.net/tordarus/gmath v0.0.3
|
||||||
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user