From cc2ead6c4ede9288f00860fddd28ba17a3cd8dae Mon Sep 17 00:00:00 2001 From: Tordarus Date: Sun, 22 Jun 2025 19:35:57 +0200 Subject: [PATCH] bumped up golang version to 1.23 --- comparator.go | 2 -- each.go | 4 ++-- filter.go | 37 +++++++++++++++++++++++++++++++++---- go.mod | 2 +- map.go | 4 ++-- of.go | 8 ++++---- reduce.go | 12 ++++++++++-- reduce_test.go | 11 ----------- reverse.go | 2 +- to.go | 2 +- types.go | 14 ++++++++++++++ 11 files changed, 68 insertions(+), 30 deletions(-) delete mode 100644 reduce_test.go create mode 100644 types.go diff --git a/comparator.go b/comparator.go index a648866..699d6cc 100644 --- a/comparator.go +++ b/comparator.go @@ -1,7 +1,5 @@ package slices -type EqualityComparator[T comparable] func(a, b T) bool - func DefaultEqualityComparator[T comparable](a, b T) bool { return a == b } diff --git a/each.go b/each.go index fced363..809cbab 100644 --- a/each.go +++ b/each.go @@ -1,10 +1,10 @@ package slices -func Each[T any](slice []T, f func(T)) { +func Each[T any](slice []T, f Consumer[T]) { EachIndex(slice, func(_ int, v T) { f(v) }) } -func EachIndex[T any](slice []T, f func(int, T)) { +func EachIndex[T any](slice []T, f IndexedConsumer[T]) { for i, v := range slice { f(i, v) } diff --git a/filter.go b/filter.go index 3a816f0..d153d4e 100644 --- a/filter.go +++ b/filter.go @@ -1,6 +1,6 @@ package slices -func Filter[T any](slice []T, f func(T) bool) []T { +func Filter[T any](slice []T, f FilterFunc[T]) []T { ret := make([]T, 0, len(slice)) for _, v := range slice { if f(v) { @@ -10,7 +10,7 @@ func Filter[T any](slice []T, f func(T) bool) []T { return ret } -func FindFirst[T any](slice []T, f func(T) bool) (T, bool) { +func FindFirst[T any](slice []T, f FilterFunc[T]) (T, bool) { for _, v := range slice { if f(v) { return v, true @@ -19,7 +19,7 @@ func FindFirst[T any](slice []T, f func(T) bool) (T, bool) { return *new(T), false } -func FindFirstIndex[T any](slice []T, f func(T) bool) (int, bool) { +func FindFirstIndex[T any](slice []T, f FilterFunc[T]) (int, bool) { for i, v := range slice { if f(v) { return i, true @@ -28,7 +28,7 @@ func FindFirstIndex[T any](slice []T, f func(T) bool) (int, bool) { return -1, false } -func FindLastIndex[T any](slice []T, f func(T) bool) (int, bool) { +func FindLastIndex[T any](slice []T, f FilterFunc[T]) (int, bool) { for i := len(slice); i >= 0; i-- { if f(slice[i]) { return i, true @@ -36,3 +36,32 @@ func FindLastIndex[T any](slice []T, f func(T) bool) (int, bool) { } return -1, false } + +func Not[T any](filter func(v T) bool) FilterFunc[T] { + return func(v T) bool { + return !filter(v) + } +} + +func And[T any](filters ...func(v T) bool) FilterFunc[T] { + return func(v T) bool { + for _, filter := range filters { + if !filter(v) { + return false + } + } + return true + } +} + +func Or[T any](filters ...func(v T) bool) FilterFunc[T] { + return func(v T) bool { + for _, filter := range filters { + if filter(v) { + return true + } + } + + return false + } +} diff --git a/go.mod b/go.mod index 9c2e922..87f4aed 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module git.tordarus.net/tordarus/slices -go 1.19 +go 1.23 require git.tordarus.net/tordarus/gmath v0.0.7 diff --git a/map.go b/map.go index e99d1de..f63df25 100644 --- a/map.go +++ b/map.go @@ -1,6 +1,6 @@ package slices -func Map[I, O any](slice []I, mapper func(I) O) []O { +func Map[I, O any](slice []I, mapper Mapper[I, O]) []O { ret := make([]O, 0, len(slice)) for _, v := range slice { ret = append(ret, mapper(v)) @@ -8,7 +8,7 @@ func Map[I, O any](slice []I, mapper func(I) O) []O { return ret } -func MapError[I, O any](slice []I, mapper func(I) (O, error)) ([]O, error) { +func MapError[I, O any](slice []I, mapper ErrorMapper[I, O]) ([]O, error) { ret := make([]O, 0, len(slice)) for _, old := range slice { new, err := mapper(old) diff --git a/of.go b/of.go index df2f912..bcbaef9 100644 --- a/of.go +++ b/of.go @@ -4,10 +4,10 @@ func Of[T any](values ...T) []T { return values } -// OfMap returns a slice containing the return values of the unmapper function +// OfMap returns a slice containing the return values of the Unmapper // applied to any key-value pair in m // The order is random -func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) []T { +func OfMap[K comparable, V, T any](m map[K]V, unmapper MapUnmapper[K, V, T]) []T { out := make([]T, 0, len(m)) for k, v := range m { out = append(out, unmapper(k, v)) @@ -15,13 +15,13 @@ func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) []T { return out } -// UnmapKey is an unmapper function which returns the map key only +// UnmapKey is an Unmapper 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 +// UnmapValue is an Unmapper 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 diff --git a/reduce.go b/reduce.go index a19573e..835ba65 100644 --- a/reduce.go +++ b/reduce.go @@ -2,8 +2,8 @@ package slices import "git.tordarus.net/tordarus/gmath" -func Reduce[T, R any](slice []T, reducer func(current R, v T) R) R { - res := new(R) +func Reduce[T, A any](slice []T, reducer Reducer[A, T]) A { + res := new(A) Each(slice, func(v T) { *res = reducer(*res, v) }) @@ -13,3 +13,11 @@ func Reduce[T, R any](slice []T, reducer func(current R, v T) R) R { func SumReducer[N gmath.Number](a, b N) N { return a + b } + +func MinReducer[N gmath.Number](a, b N) N { + return gmath.Min(a, b) +} + +func MaxReducer[N gmath.Number](a, b N) N { + return gmath.Max(a, b) +} diff --git a/reduce_test.go b/reduce_test.go deleted file mode 100644 index 14df6d4..0000000 --- a/reduce_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package slices - -import ( - "fmt" - "testing" -) - -func TestReduce(t *testing.T) { - s := Of(1, 2, 3) - fmt.Println(Reduce(s, SumReducer[int])) -} diff --git a/reverse.go b/reverse.go index a4f0b29..ec0e1e0 100644 --- a/reverse.go +++ b/reverse.go @@ -2,7 +2,7 @@ package slices func Reverse[T any](slice []T) []T { s := make([]T, len(slice)) - for i := 0; i < len(slice); i++ { + for i := range slice { ri := len(slice) - 1 - i s[ri] = slice[i] } diff --git a/to.go b/to.go index e358bce..b6db90f 100644 --- a/to.go +++ b/to.go @@ -36,7 +36,7 @@ func ToList[T any](s []T) *list.List { // ToMap returns a map containing all values of s. // The map key-value pairs are determined by mapper -func ToMap[T any, K comparable, V any](s []T, mapper func(T) (K, V)) map[K]V { +func ToMap[T any, K comparable, V any](s []T, mapper MapMapper[T, K, V]) map[K]V { m := map[K]V{} Each(s, func(value T) { k, v := mapper(value) diff --git a/types.go b/types.go new file mode 100644 index 0000000..52f313d --- /dev/null +++ b/types.go @@ -0,0 +1,14 @@ +package slices + +type EqualityComparator[T comparable] = func(a, b T) bool +type FilterFunc[T any] = func(T) bool +type Consumer[T any] = func(T) +type IndexedConsumer[T any] = func(int, T) + +type Mapper[I, O any] = func(I) O +type ErrorMapper[I, O any] = func(I) (O, error) + +type MapMapper[T any, K comparable, V any] = func(T) (K, V) +type MapUnmapper[K comparable, V, T any] = func(K, V) T + +type Reducer[A, T any] = func(acc A, value T) A