Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
abeecfd797 | |||
5735640ab6 | |||
26ea846e93 | |||
0592add8ca | |||
ab6e6684f8 | |||
![]() |
55928776b4 | ||
![]() |
84cc7a9e05 | ||
![]() |
cdb1cf3e03 | ||
![]() |
4f3cf3f537 | ||
![]() |
2124bff9de | ||
![]() |
2fb5ea542d |
22
contains.go
Normal file
22
contains.go
Normal file
@ -0,0 +1,22 @@
|
||||
package slices
|
||||
|
||||
func IndexOf[T comparable](slice []T, value T) int {
|
||||
return IndexOfCmp(slice, value, DefaultEqualityComparator[T])
|
||||
}
|
||||
|
||||
func IndexOfCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) int {
|
||||
for i, v := range slice {
|
||||
if cmp(v, value) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func Contains[T comparable](slice []T, value T) bool {
|
||||
return ContainsCmp(slice, value, DefaultEqualityComparator[T])
|
||||
}
|
||||
|
||||
func ContainsCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) bool {
|
||||
return IndexOfCmp(slice, value, cmp) != -1
|
||||
}
|
15
count.go
Normal file
15
count.go
Normal file
@ -0,0 +1,15 @@
|
||||
package slices
|
||||
|
||||
func Count[T comparable](slice []T, value T) int {
|
||||
return CountCmp(slice, value, DefaultEqualityComparator[T])
|
||||
}
|
||||
|
||||
func CountCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) int {
|
||||
c := 0
|
||||
for _, v := range slice {
|
||||
if cmp(v, value) {
|
||||
c++
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
11
each.go
Normal file
11
each.go
Normal file
@ -0,0 +1,11 @@
|
||||
package slices
|
||||
|
||||
func Each[T any](slice []T, f func(T)) {
|
||||
EachIndex(slice, func(_ int, v T) { f(v) })
|
||||
}
|
||||
|
||||
func EachIndex[T any](slice []T, f func(int, T)) {
|
||||
for i, v := range slice {
|
||||
f(i, v)
|
||||
}
|
||||
}
|
38
filter.go
Normal file
38
filter.go
Normal file
@ -0,0 +1,38 @@
|
||||
package slices
|
||||
|
||||
func Filter[T any](slice []T, f func(T) bool) []T {
|
||||
ret := make([]T, 0, len(slice))
|
||||
for _, v := range slice {
|
||||
if f(v) {
|
||||
ret = append(ret, v)
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
9
flat.go
Normal file
9
flat.go
Normal file
@ -0,0 +1,9 @@
|
||||
package slices
|
||||
|
||||
func Flat[T any](s ...[]T) []T {
|
||||
out := make([]T, 0)
|
||||
for _, v := range s {
|
||||
out = append(out, v...)
|
||||
}
|
||||
return out
|
||||
}
|
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module git.milar.in/milarin/slices
|
||||
|
||||
go 1.19
|
||||
|
||||
require git.milar.in/milarin/channel v0.0.7
|
||||
require git.milar.in/milarin/gmath v0.0.3
|
||||
|
4
go.sum
4
go.sum
@ -1,2 +1,2 @@
|
||||
git.milar.in/milarin/channel v0.0.7 h1:cVKtwgH/EE7U+XTHcoFCClJ4LR349KanzjX9xKwRcNg=
|
||||
git.milar.in/milarin/channel v0.0.7/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM=
|
||||
git.milar.in/milarin/gmath v0.0.3 h1:ii6rKNItS55O/wtIFhD1cTN2BMwDZjTBmiOocKURvxM=
|
||||
git.milar.in/milarin/gmath v0.0.3/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE=
|
||||
|
21
map.go
Normal file
21
map.go
Normal file
@ -0,0 +1,21 @@
|
||||
package slices
|
||||
|
||||
func Map[I, O any](slice []I, mapper func(I) O) []O {
|
||||
ret := make([]O, 0, len(slice))
|
||||
for _, v := range slice {
|
||||
ret = append(ret, mapper(v))
|
||||
}
|
||||
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
|
||||
}
|
28
of.go
Normal file
28
of.go
Normal file
@ -0,0 +1,28 @@
|
||||
package slices
|
||||
|
||||
func Of[T any](values ...T) []T {
|
||||
return values
|
||||
}
|
||||
|
||||
// OfMap returns a slice containing the return values of the unmapper function
|
||||
// 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 {
|
||||
out := make([]T, 0, len(m))
|
||||
for k, v := range m {
|
||||
out = append(out, unmapper(k, v))
|
||||
}
|
||||
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
Normal file
15
reduce.go
Normal file
@ -0,0 +1,15 @@
|
||||
package slices
|
||||
|
||||
import "git.milar.in/milarin/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
|
||||
}
|
11
reduce_test.go
Normal file
11
reduce_test.go
Normal file
@ -0,0 +1,11 @@
|
||||
package slices
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReduce(t *testing.T) {
|
||||
s := Of(1, 2, 3)
|
||||
fmt.Println(Reduce(s, SumReducer[int]))
|
||||
}
|
@ -4,7 +4,7 @@ func Reverse[T any](slice []T) []T {
|
||||
s := make([]T, len(slice))
|
||||
for i := 0; i < len(slice); i++ {
|
||||
ri := len(slice) - 1 - i
|
||||
s[ri] = s[i]
|
||||
s[ri] = slice[i]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
52
slices.go
52
slices.go
@ -1,52 +0,0 @@
|
||||
package slices
|
||||
|
||||
import (
|
||||
"git.milar.in/milarin/channel"
|
||||
)
|
||||
|
||||
func IndexOf[T comparable](slice []T, value T) int {
|
||||
return IndexOfCmp(slice, value, DefaultEqualityComparator[T])
|
||||
}
|
||||
|
||||
func IndexOfCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) int {
|
||||
for i, v := range slice {
|
||||
if cmp(v, value) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func Contains[T comparable](slice []T, value T) bool {
|
||||
return ContainsCmp(slice, value, DefaultEqualityComparator[T])
|
||||
}
|
||||
|
||||
func ContainsCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) bool {
|
||||
return IndexOfCmp(slice, value, cmp) != -1
|
||||
}
|
||||
|
||||
func Map[I, O any](slice []I, mapper func(I) O) []O {
|
||||
ret := make([]O, 0, len(slice))
|
||||
for _, v := range slice {
|
||||
ret = append(ret, mapper(v))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func MapParallel[I, O any](slice []I, mapper func(I) O) []O {
|
||||
return channel.ToSlice(channel.Map(channel.Of(slice...), mapper))
|
||||
}
|
||||
|
||||
func MapParallelWithRunner[I, O any](slice []I, runner channel.Runner, mapper func(I) O) []O {
|
||||
return channel.ToSlice(channel.MapWithRunner(channel.Of(slice...), runner, mapper))
|
||||
}
|
||||
|
||||
func Each[T any](slice []T, f func(T)) {
|
||||
EachIndex(slice, func(_ int, v T) { f(v) })
|
||||
}
|
||||
|
||||
func EachIndex[T any](slice []T, f func(int, T)) {
|
||||
for i, v := range slice {
|
||||
f(i, v)
|
||||
}
|
||||
}
|
42
to.go
Normal file
42
to.go
Normal file
@ -0,0 +1,42 @@
|
||||
package slices
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
// Deref returns a slice containing all dereferenced values of s.
|
||||
// The returned slice will be a dereferenced and continuous block of memory.
|
||||
// Nil pointers are ignored.
|
||||
func Deref[T any](s []*T) []T {
|
||||
out := make([]T, 0, len(s))
|
||||
Each(s, func(v *T) {
|
||||
if v != nil {
|
||||
out = append(out, *v)
|
||||
}
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
// ToList returns a list.List containing all values of s
|
||||
func ToList[T any](s []T) *list.List {
|
||||
l := list.New()
|
||||
Each(s, func(value T) { l.PushBack(value) })
|
||||
return l
|
||||
}
|
||||
|
||||
// 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 {
|
||||
m := map[K]V{}
|
||||
Each(s, func(value T) {
|
||||
k, v := mapper(value)
|
||||
m[k] = v
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
// ToStructMap returns a struct{} map containing all values of s as keys.
|
||||
// It is a shorthand for ToMap(s, func(value T) (T, struct{}) { return value, struct{}{} })
|
||||
func ToStructMap[T comparable](s []T) map[T]struct{} {
|
||||
return ToMap(s, func(value T) (T, struct{}) { return value, struct{}{} })
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user