Add value generators

This commit is contained in:
2025-05-30 21:41:38 +04:00
parent f248c2af96
commit 79668e4ece
8 changed files with 561 additions and 26 deletions

View File

@@ -10,14 +10,11 @@ func Flatten[T any](nested [][]*T) []*T {
return flattened
}
// RandomValueCycle returns a function that cycles through the provided slice of values
// in a random order. Each call to the returned function will yield a value from the slice.
// The order of values is determined by the provided random number generator.
//
// The returned function will cycle through the values in a random order until all values
// have been returned at least once. After all values have been returned, the function will
// reset and start cycling through the values in a random order again.
// The returned function isn't thread-safe and should be used in a single-threaded context.
// RandomValueCycle returns a function that cycles through the provided values in a pseudo-random order.
// Each value in the input slice will be returned before any value is repeated.
// If the input slice is empty, the returned function will always return the zero value of type T.
// If the input slice contains only one element, that element is always returned.
// This function is not thread-safe and should not be called concurrently.
func RandomValueCycle[T any](values []T, localRand *rand.Rand) func() T {
switch valuesLen := len(values); valuesLen {
case 0: