effect-io-ai

Package: effect
Module: Array

Array.countBy

Computes the number of elements in an iterable that satisfy a predicate.

When to use

Use when you need to count how many elements of an iterable satisfy a predicate.

Details

The predicate receives both the element and its index. Empty iterables return 0.

Example (Counting even numbers)

import { Array } from "effect"

const result = Array.countBy([1, 2, 3, 4, 5], (n) => n % 2 === 0)
console.log(result) // 2

See

Signature

declare const countBy: { <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => number; <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): number; }

Source

Since v3.16.0