Files
lo/docs/data/it-foreachwhile.md
T
2025-10-06 17:16:33 +02:00

1.3 KiB

name, slug, sourceRef, category, subCategory, signatures, variantHelpers, similarHelpers, position
name slug sourceRef category subCategory signatures variantHelpers similarHelpers position
ForEachWhile foreachwhile it/seq.go#L180 it sequence
func ForEachWhile[T any](collection iter.Seq[T], predicate func(item T) bool)
func ForEachWhileI[T any](collection iter.Seq[T], predicate func(item T, index int) bool)
it#sequence#foreachwhile
it#sequence#foreachwhilei
it#sequence#foreach
160

ForEachWhile iterates over elements of collection and invokes predicate for each element. The predicate return value decides to continue or break, like do while().

collection := func(yield func(int) bool) {
    yield(1)
    yield(2)
    yield(3)
    yield(4)
    yield(5)
}

called := 0
it.ForEachWhile(collection, func(item int) bool {
    called++
    return item < 3
})
// called is 3 (elements 1, 2, 3 were processed)

ForEachWhileI iterates over elements of collection and invokes predicate for each element with index. The predicate return value decides to continue or break, like do while().

collection := func(yield func(string) bool) {
    yield("a")
    yield("b")
    yield("c")
    yield("d")
}

called := 0
it.ForEachWhileI(collection, func(item string, index int) bool {
    called++
    return index < 2
})
// called is 3 (elements at indices 0, 1, 2 were processed)