Documentation ¶
Overview ¶
Package reservoir samples values uniformly at random, without replacement, from an unbounded sequence of inputs. It provides a representative sample when the sequence has unknown length or is too big to store in its entirety.
Example (Errors) ¶
package main import ( "errors" "fmt" "kr.dev/reservoir" ) func main() { // Make & populate a new reservoir of errors with capacity 5. rs := reservoir.New[error](5) rs.Add(errors.New("first")) rs.Add(errors.New("second")) rs.Add(errors.New("third")) // Read a sample into errs. errs := make([]error, rs.Cap()) n := rs.Read(errs) fmt.Println("our sample:", errs[:n]) }
Output: our sample: [first second third]
Example (Many) ¶
package main import ( "fmt" "math/rand" "kr.dev/reservoir" ) func main() { rand.Seed(0) // Make & populate a new reservoir of ints with capacity 3. rs := reservoir.New[int](3) for i := 0; i < 10_000; i++ { rs.Add(i) } // Read a sample into ints. ints := make([]int, rs.Cap()) n := rs.Read(ints) fmt.Println("our sample:", ints[:n]) }
Output: our sample: [9539 5555 7160]
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Sample ¶
type Sample[T any] struct { // contains filtered or unexported fields }
A Sample collects a fixed number of items, chosen uniformly at random, from an unbounded input sequence. The number of items collected is its capacity.
The zero value is a valid Sample with a capacity of 0. (It will not sample any items.)
func (*Sample[T]) Add ¶
func (s *Sample[T]) Add(v T)
Add adds v to s with a probability adjusted so that the contents of s at any time are chosen uniformly at random from the inputs so far.
Click to show internal directories.
Click to hide internal directories.