channel/of.go

70 lines
1.5 KiB
Go

package channel
import (
"context"
"time"
)
// Of returns a channel containing all values
func Of[T any](values ...T) <-chan T {
return OfDelayed(0, values...)
}
// OfDelayed behaves like Of but with a pre-defined delay between each value
func OfDelayed[T any](delay time.Duration, values ...T) <-chan T {
return OfDelayedFunc(func(value T) time.Duration { return delay }, values...)
}
// OfDelayedFunc behaves like OfDelayed but accepts a function to determine the delay
func OfDelayedFunc[T any](delayFunc func(value T) time.Duration, values ...T) <-chan T {
out := make(chan T, len(values))
go func(out chan T, values []T) {
defer close(out)
for i, value := range values {
out <- value
if i < len(values)-1 {
time.Sleep(delayFunc(value))
}
}
}(out, values)
return out
}
// OfFunc returns a channel containing the return values of successively calling f
// It closes the channel as soon as ctx is done
func OfFunc[T any](ctx context.Context, buffer int, f func() T) <-chan T {
out := make(chan T, buffer)
go func() {
defer close(out)
for ctx.Err() == nil {
select {
case out <- f():
case <-ctx.Done():
return
}
}
}()
return out
}
// OfMap returns a channel 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) <-chan T {
out := make(chan T, len(m))
defer func() {
defer close(out)
for k, v := range m {
out <- unmapper(k, v)
}
}()
return out
}