count method

int count([
  1. bool test(
    1. T element
    )?
])

Returns the number of elements that matches the test.

If not test is specified it will count every element.

Example:

[1, 2, 3, 13, 14, 15].count();             // 6
[1, 2, 3, 13, 14, 15].count((n) => n > 9); // 3

Implementation

int count([bool Function(T element)? test]) {
  final testFn = test ?? (_) => true;

  if (isEmpty) {
    return 0;
  }

  return map((element) => testFn(element) ? 1 : 0)
      .reduce((value, element) => value + element);
}