Merge branch 'main' of ssh://git.tordarus.net:2222/Tordarus/channel
This commit is contained in:
commit
dbfd7d6cd3
16
of.go
16
of.go
@ -12,19 +12,7 @@ func Of[T any](values ...T) <-chan T {
|
|||||||
|
|
||||||
// OfDelayed behaves like Of but with a pre-defined delay between each value
|
// OfDelayed behaves like Of but with a pre-defined delay between each value
|
||||||
func OfDelayed[T any](delay time.Duration, values ...T) <-chan T {
|
func OfDelayed[T any](delay time.Duration, values ...T) <-chan T {
|
||||||
out := make(chan T, len(values))
|
return OfDelayedFunc(func(value T) time.Duration { return delay }, values...)
|
||||||
|
|
||||||
go func(out chan T, values []T) {
|
|
||||||
for i, value := range values {
|
|
||||||
out <- value
|
|
||||||
if i < len(values)-1 {
|
|
||||||
time.Sleep(delay)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close(out)
|
|
||||||
}(out, values)
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OfDelayedFunc behaves like OfDelayed but accepts a function to determine the delay
|
// OfDelayedFunc behaves like OfDelayed but accepts a function to determine the delay
|
||||||
@ -32,13 +20,13 @@ func OfDelayedFunc[T any](delayFunc func(value T) time.Duration, values ...T) <-
|
|||||||
out := make(chan T, len(values))
|
out := make(chan T, len(values))
|
||||||
|
|
||||||
go func(out chan T, values []T) {
|
go func(out chan T, values []T) {
|
||||||
|
defer close(out)
|
||||||
for i, value := range values {
|
for i, value := range values {
|
||||||
out <- value
|
out <- value
|
||||||
if i < len(values)-1 {
|
if i < len(values)-1 {
|
||||||
time.Sleep(delayFunc(value))
|
time.Sleep(delayFunc(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(out)
|
|
||||||
}(out, values)
|
}(out, values)
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
Loading…
Reference in New Issue
Block a user