lo
lo copied to clipboard
New helper collection channel
Let's talk about a new suite of helpers for manipulating channels.
// ToChannel returns a read-only channels of collection elements.
func ToChannel[T any](collection []T) <-chan T
// Generator implements the generator design pattern.
func Generator[T any](bufferSize int, generator func(int64) T) <-chan T
// Batch creates a slice of n elements from a channel. Returns the slice and the slice length.
func Batch[T any](ch <-chan T, size int) (collection []T, length int)
// BatchWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length.
func BatchWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int)
Some thoughts about BatchXXX functions:
- return channel status:
ok bool
? - return batch time:
duration time.Duration
? - return
<-chan []T
instead of[]T
? - accept a buffer as a parameter instead of repeated allocation?
- allocate channel with buffer size > 1 ?
Some ideas in following PR: #95