Compare commits
No commits in common. "main" and "v0.0.6" have entirely different histories.
27
filter.go
27
filter.go
@ -9,30 +9,3 @@ 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
|
|
||||||
}
|
|
||||||
|
4
go.mod
4
go.mod
@ -1,5 +1,3 @@
|
|||||||
module git.tordarus.net/tordarus/slices
|
module git.milar.in/milarin/slices
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require git.tordarus.net/tordarus/gmath v0.0.7
|
|
||||||
|
4
go.sum
4
go.sum
@ -1,2 +1,2 @@
|
|||||||
git.tordarus.net/tordarus/gmath v0.0.7 h1:tR48idt9AUL0r556ww3ZxByTKJEr6NWCTlhl2ihzYxQ=
|
git.milar.in/milarin/channel v0.0.7 h1:cVKtwgH/EE7U+XTHcoFCClJ4LR349KanzjX9xKwRcNg=
|
||||||
git.tordarus.net/tordarus/gmath v0.0.7/go.mod h1:mO7aPlvNrGVE9UFXEuuACjZgMDsM63l3OcQy6xSQnoE=
|
git.milar.in/milarin/channel v0.0.7/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM=
|
||||||
|
12
map.go
12
map.go
@ -7,15 +7,3 @@ 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,15 +14,3 @@ 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
|
|
||||||
}
|
|
||||||
|
15
reduce.go
15
reduce.go
@ -1,15 +0,0 @@
|
|||||||
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)
|
|
||||||
Each(slice, func(v T) {
|
|
||||||
*res = reducer(*res, v)
|
|
||||||
})
|
|
||||||
return *res
|
|
||||||
}
|
|
||||||
|
|
||||||
func SumReducer[N gmath.Number](a, b N) N {
|
|
||||||
return 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]))
|
|
||||||
}
|
|
11
search.go
Normal file
11
search.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package slices
|
||||||
|
|
||||||
|
func Search[T any](slice []T, f func(a, b T) T) T {
|
||||||
|
if len(slice) == 0 {
|
||||||
|
return *new(T)
|
||||||
|
}
|
||||||
|
|
||||||
|
value := slice[0]
|
||||||
|
Each(slice, func(v T) { value = f(value, v) })
|
||||||
|
return value
|
||||||
|
}
|
10
to.go
10
to.go
@ -17,16 +17,6 @@ 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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user