Docs and sliceutils from and to channel

This commit is contained in:
Melody Becker 2025-06-17 13:28:25 +02:00
parent 3bb1984f1c
commit 7a09569c03
Signed by: mstar
SSH key fingerprint: SHA256:9VAo09aaVNTWKzPW7Hq2LW+ox9OdwmTSHRoD4mlz1yI
2 changed files with 32 additions and 1 deletions

View file

@ -73,6 +73,8 @@ func CompareMap[K, V comparable](a, b map[K]V) bool {
return true return true
} }
// Compact reduces the keys and values of a map down into one value each.
// The starting value for each is the default value of that type.
func Compact[K comparable, V any]( func Compact[K comparable, V any](
m map[K]V, m map[K]V,
compactor func(accK K, accV V, nextK K, nextV V) (K, V), compactor func(accK K, accV V, nextK K, nextV V) (K, V),

View file

@ -1,7 +1,7 @@
// Package sliceutils contains various generic functions for applying an operation across an entire slice // Package sliceutils contains various generic functions for applying an operation across an entire slice
package sliceutils package sliceutils
// MapS applies a given function to every element of a slice. // Map applies a given function to every element of a slice.
// The return type may be different from the initial type of the slice. // The return type may be different from the initial type of the slice.
func Map[T any, M any](arr []T, apply func(T) M) []M { func Map[T any, M any](arr []T, apply func(T) M) []M {
n := make([]M, len(arr)) n := make([]M, len(arr))
@ -73,6 +73,7 @@ func CompareUnordered[T comparable](a, b []T) bool {
return hits == len(a) return hits == len(a)
} }
// Returns whether b exists inside a using a simple == comparison
func Contains[T comparable](a []T, b T) bool { func Contains[T comparable](a []T, b T) bool {
for _, v := range a { for _, v := range a {
if v == b { if v == b {
@ -82,6 +83,7 @@ func Contains[T comparable](a []T, b T) bool {
return false return false
} }
// Returns whether the given function f returns true for any element in a
func ContainsFunc[T any](a []T, f func(t T) bool) bool { func ContainsFunc[T any](a []T, f func(t T) bool) bool {
for _, v := range a { for _, v := range a {
if f(v) { if f(v) {
@ -91,6 +93,8 @@ func ContainsFunc[T any](a []T, f func(t T) bool) bool {
return false return false
} }
// Compact the slice a using the compactor function.
// For the first call, the accumulator argument will be the default value
func Compact[T any](a []T, compactor func(acc T, next T) T) T { func Compact[T any](a []T, compactor func(acc T, next T) T) T {
var acc T var acc T
for _, v := range a { for _, v := range a {
@ -98,3 +102,28 @@ func Compact[T any](a []T, compactor func(acc T, next T) T) T {
} }
return acc return acc
} }
// Returns a channel that all elements in a will be written to in order.
// Once all values of a have been sent, the channel will be closed.
// The channel must be fully consumed until closed. Otherwise a goroutine will be leaked
func ToChannel[T any](a []T) chan T {
c := make(chan T)
go func() {
for _, v := range a {
c <- v
}
close(c)
}()
return c
}
// FromChannel reads from a channel until closed, appending every element to a slice.
// If you do not know how many elements to expect, use an expectedSize of 0
func FromChannel[T any](c chan T, expectedSize uint) []T {
a := make([]T, expectedSize)
for v := range c {
a = append(a, v)
}
return a
}