Files
lo/docs/data/it-sample.md
T
2025-10-08 19:35:53 +02:00

1.2 KiB

name, slug, sourceRef, category, subCategory, signatures, playUrl, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures playUrl variantHelpers similarHelpers position
Sample sample it/find.go#L455 it find
func Sample[T any](collection iter.Seq[T]) T
https://go.dev/play/p/4VpIM8-zu
it#find#sample
core#slice#sample
it#find#samples
it#find#sampleBy
it#find#samplesBy
160

Returns a random item from collection.

Example:

seq := func(yield func(string) bool) {
    _ = yield("apple")
    _ = yield("banana")
    _ = yield("cherry")
}
item := it.Sample(seq)
// item is randomly one of: "apple", "banana", "cherry"

// Example with integers
numbers := func(yield func(int) bool) {
    _ = yield(10)
    _ = yield(20)
    _ = yield(30)
    _ = yield(40)
}
randomNum := it.Sample(numbers)
// randomNum is randomly one of: 10, 20, 30, 40

// Example with empty sequence - returns zero value
empty := func(yield func(string) bool) {
    // no yields
}
emptyResult := it.Sample(empty)
// emptyResult: "" (zero value for string)

// Example with single item
single := func(yield func(int) bool) {
    _ = yield(42)
}
singleResult := it.Sample(single)
// singleResult: 42 (always returns 42 since it's the only option)