Files
lo/docs/data/it-takefilter.md
T
Yuliya c1d11cb658 Add iterator slice helpers (#791)
* add iterator slice helpers and channel dedup/tee

* note unbounded memory growth in Distinct; simplify Take

* avoid extra iteration in Take/TakeWhile/TakeFilter and precompute Sliding step

* avoid reallocations in Sliding overlap shift

* sliding uses ring buffer

* refactor Sliding and expand TestSliding

* Remove Tee; add TakeFilter variant, rewrite Sliding

* remove channel Distinct/DistinctBy

* TakeFilter/TakeFilterI for slice

* Revert "TakeFilter/TakeFilterI for slice"

This reverts commit 6d2fcdc07b.

* doc  add new helpers to llms.txt

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2026-02-23 20:27:51 +01:00

994 B

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
TakeFilter takefilter it/seq.go#L725 it sequence
func TakeFilter[T any, I ~func(func(T) bool)](collection I, n int, predicate func(item T) bool) I
func TakeFilterI[T any, I ~func(func(T) bool)](collection I, n int, predicate func(item T, index int) bool) I
it#sequence#takefilter
it#sequence#takefilteri
core#slice#takefilter
130

Filters elements and takes the first n matches. Stops once n matches are found.

TakeFilter

seq := func(yield func(int) bool) {
    yield(1)
    yield(2)
    yield(3)
    yield(4)
    yield(5)
    yield(6)
}
result := it.TakeFilter(seq, 2, func(x int) bool {
    return x%2 == 0
})
var out []int
for v := range result {
    out = append(out, v)
}
// out contains [2, 4]

TakeFilterI

result := it.TakeFilterI(seq, 2, func(x, index int) bool {
    return x%2 == 0 && index < 4
})
// out contains [2, 4]