Compare commits

..

No commits in common. "main" and "v0.0.3" have entirely different histories.
main ... v0.0.3

14 changed files with 66 additions and 215 deletions

View File

@ -1,22 +0,0 @@
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
}

View File

@ -1,15 +0,0 @@
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
View File

@ -1,11 +0,0 @@
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)
}
}

View File

@ -1,38 +0,0 @@
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
}

View File

@ -1,9 +0,0 @@
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
View File

@ -2,4 +2,4 @@ module git.milar.in/milarin/slices
go 1.19
require git.milar.in/milarin/gmath v0.0.3
require git.milar.in/milarin/channel v0.0.7

4
go.sum
View File

@ -1,2 +1,2 @@
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=
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=

21
map.go
View File

@ -1,21 +0,0 @@
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
View File

@ -1,28 +0,0 @@
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
}

View File

@ -1,15 +0,0 @@
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
}

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

11
search.go Normal file
View 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
}

52
slices.go Normal file
View File

@ -0,0 +1,52 @@
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
View File

@ -1,42 +0,0 @@
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{}{} })
}