Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New helper collection channel #94

Open
samber opened this issue Apr 14, 2022 · 1 comment
Open

New helper collection channel #94

samber opened this issue Apr 14, 2022 · 1 comment

Comments

@samber
Copy link
Owner

samber commented Apr 14, 2022

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 ?
@samber
Copy link
Owner Author

samber commented Apr 14, 2022

Some ideas in following PR: #95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant