Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
96e65f3550 | |||
cc2ead6c4e | |||
efc99a9658 | |||
7988676059 | |||
3901e3934f | |||
abeecfd797 | |||
5735640ab6 |
@ -1,7 +1,5 @@
|
|||||||
package slices
|
package slices
|
||||||
|
|
||||||
type EqualityComparator[T comparable] func(a, b T) bool
|
|
||||||
|
|
||||||
func DefaultEqualityComparator[T comparable](a, b T) bool {
|
func DefaultEqualityComparator[T comparable](a, b T) bool {
|
||||||
return a == b
|
return a == b
|
||||||
}
|
}
|
||||||
|
4
each.go
4
each.go
@ -1,10 +1,10 @@
|
|||||||
package slices
|
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) })
|
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 {
|
for i, v := range slice {
|
||||||
f(i, v)
|
f(i, v)
|
||||||
}
|
}
|
||||||
|
51
filter.go
51
filter.go
@ -1,6 +1,6 @@
|
|||||||
package slices
|
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))
|
ret := make([]T, 0, len(slice))
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if f(v) {
|
if f(v) {
|
||||||
@ -10,7 +10,7 @@ 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) {
|
func FindFirst[T any](slice []T, f FilterFunc[T]) (T, bool) {
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if f(v) {
|
if f(v) {
|
||||||
return v, true
|
return v, true
|
||||||
@ -19,12 +19,49 @@ 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 FilterFunc[T]) (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 FilterFunc[T]) (int, bool) {
|
||||||
|
for i := len(slice); i >= 0; i-- {
|
||||||
|
if f(slice[i]) {
|
||||||
|
return i, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func Not[T any](filter FilterFunc[T]) FilterFunc[T] {
|
||||||
|
return func(v T) bool {
|
||||||
|
return !filter(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func And[T any](filters ...FilterFunc[T]) FilterFunc[T] {
|
||||||
|
return func(v T) bool {
|
||||||
|
for _, filter := range filters {
|
||||||
|
if !filter(v) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Or[T any](filters ...FilterFunc[T]) FilterFunc[T] {
|
||||||
|
return func(v T) bool {
|
||||||
|
for _, filter := range filters {
|
||||||
|
if filter(v) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
6
go.mod
6
go.mod
@ -1,5 +1,5 @@
|
|||||||
module git.milar.in/milarin/slices
|
module git.tordarus.net/tordarus/slices
|
||||||
|
|
||||||
go 1.19
|
go 1.23
|
||||||
|
|
||||||
require git.milar.in/milarin/gmath v0.0.3
|
require git.tordarus.net/tordarus/gmath v0.0.7
|
||||||
|
4
go.sum
4
go.sum
@ -1,2 +1,2 @@
|
|||||||
git.milar.in/milarin/gmath v0.0.3 h1:ii6rKNItS55O/wtIFhD1cTN2BMwDZjTBmiOocKURvxM=
|
git.tordarus.net/tordarus/gmath v0.0.7 h1:tR48idt9AUL0r556ww3ZxByTKJEr6NWCTlhl2ihzYxQ=
|
||||||
git.milar.in/milarin/gmath v0.0.3/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE=
|
git.tordarus.net/tordarus/gmath v0.0.7/go.mod h1:mO7aPlvNrGVE9UFXEuuACjZgMDsM63l3OcQy6xSQnoE=
|
||||||
|
4
map.go
4
map.go
@ -1,6 +1,6 @@
|
|||||||
package slices
|
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))
|
ret := make([]O, 0, len(slice))
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
ret = append(ret, mapper(v))
|
ret = append(ret, mapper(v))
|
||||||
@ -8,7 +8,7 @@ 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) {
|
func MapError[I, O any](slice []I, mapper ErrorMapper[I, O]) ([]O, error) {
|
||||||
ret := make([]O, 0, len(slice))
|
ret := make([]O, 0, len(slice))
|
||||||
for _, old := range slice {
|
for _, old := range slice {
|
||||||
new, err := mapper(old)
|
new, err := mapper(old)
|
||||||
|
16
of.go
16
of.go
@ -4,13 +4,25 @@ func Of[T any](values ...T) []T {
|
|||||||
return values
|
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
|
// applied to any key-value pair in m
|
||||||
// The order is random
|
// 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))
|
out := make([]T, 0, len(m))
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
out = append(out, unmapper(k, v))
|
out = append(out, unmapper(k, v))
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 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
|
||||||
|
}
|
||||||
|
14
reduce.go
14
reduce.go
@ -1,9 +1,9 @@
|
|||||||
package slices
|
package slices
|
||||||
|
|
||||||
import "git.milar.in/milarin/gmath"
|
import "git.tordarus.net/tordarus/gmath"
|
||||||
|
|
||||||
func Reduce[T, R any](slice []T, reducer func(current R, v T) R) R {
|
func Reduce[T, A any](slice []T, reducer Reducer[A, T]) A {
|
||||||
res := new(R)
|
res := new(A)
|
||||||
Each(slice, func(v T) {
|
Each(slice, func(v T) {
|
||||||
*res = reducer(*res, v)
|
*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 {
|
func SumReducer[N gmath.Number](a, b N) N {
|
||||||
return a + b
|
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)
|
||||||
|
}
|
||||||
|
@ -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]))
|
|
||||||
}
|
|
@ -2,7 +2,7 @@ package slices
|
|||||||
|
|
||||||
func Reverse[T any](slice []T) []T {
|
func Reverse[T any](slice []T) []T {
|
||||||
s := make([]T, len(slice))
|
s := make([]T, len(slice))
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := range slice {
|
||||||
ri := len(slice) - 1 - i
|
ri := len(slice) - 1 - i
|
||||||
s[ri] = slice[i]
|
s[ri] = slice[i]
|
||||||
}
|
}
|
||||||
|
12
to.go
12
to.go
@ -17,6 +17,16 @@ func Deref[T any](s []*T) []T {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ref returns a slice containing pointers to all values of s.
|
||||||
|
// The order in s is preserved.
|
||||||
|
func Ref[T any](s []T) []*T {
|
||||||
|
out := make([]*T, 0, len(s))
|
||||||
|
Each(s, func(v T) {
|
||||||
|
out = append(out, &v)
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// ToList returns a list.List containing all values of s
|
// ToList returns a list.List containing all values of s
|
||||||
func ToList[T any](s []T) *list.List {
|
func ToList[T any](s []T) *list.List {
|
||||||
l := list.New()
|
l := list.New()
|
||||||
@ -26,7 +36,7 @@ func ToList[T any](s []T) *list.List {
|
|||||||
|
|
||||||
// ToMap returns a map containing all values of s.
|
// ToMap returns a map containing all values of s.
|
||||||
// The map key-value pairs are determined by mapper
|
// 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{}
|
m := map[K]V{}
|
||||||
Each(s, func(value T) {
|
Each(s, func(value T) {
|
||||||
k, v := mapper(value)
|
k, v := mapper(value)
|
||||||
|
14
types.go
Normal file
14
types.go
Normal 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
|
Reference in New Issue
Block a user