Back to Lo

Core Countbyerr

docs/data/core-countbyerr.md

1.53.0471 B
Original Source

Counts the number of elements for which the predicate is true. Returns an error if the predicate function fails, stopping iteration immediately.

go
count, err := lo.CountByErr([]int{1, 5, 1}, func(i int) (bool, error) {
    if i == 5 {
        return false, fmt.Errorf("5 not allowed")
    }
    return i < 4, nil
})
// 0, error("5 not allowed")
go
count, err := lo.CountByErr([]int{1, 5, 1}, func(i int) (bool, error) {
    return i < 4, nil
})
// 2, nil