Prelude API
    Preparing search index...

    Function count

    • Counts the number of elements in an iterable that satisfy an optional predicate function. If no predicate is provided, it counts all elements.

      Type Parameters

      • T

        Type of elements in the iterable

      Parameters

      • predicate: (value: T, index: number) => boolean = ...

        Optional function to test each element, defaults to counting all elements

      Returns (values: Iterable<T>) => number

      A function that takes an iterable and returns the count of matching elements

      // Count all elements
      const total = count()([1, 2, 3, 4, 5])
      // Result: 5
      // Count even numbers
      const evenCount = count((n: number) => n % 2 === 0)([1, 2, 3, 4, 5])
      // Result: 2
      // Count elements at odd indices
      const oddIndices = count((_, index) => index % 2 === 1)([1, 2, 3, 4, 5])
      // Result: 2