bumped up golang version to 1.23

This commit is contained in:
2025-06-22 19:35:57 +02:00
parent efc99a9658
commit cc2ead6c4e
11 changed files with 68 additions and 30 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}
}

2
go.mod
View File

@ -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

4
map.go
View File

@ -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)

8
of.go
View File

@ -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

View File

@ -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)
}

View File

@ -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]))
}

View File

@ -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]
}

2
to.go
View File

@ -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)

14
types.go Normal file
View File

@ -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