count

Counts all elements or elements satisfying a predicate in haystack.

The first overload counts each element e in haystack for which pred(e) is true. Performs O(haystack.length) evaluations of pred.

The second overload counts the number of elements in a range. If the given range has the length property, that is returned right away, otherwise performs O(haystack.length) to walk the range.

Parameters

haystack R

The range to count.

Return Value

Type: size_t

The number of elements in haystack (for which pred returned true).

Examples

// count elements in range
int[] a = [ 1, 2, 4, 3, 2, 5, 3, 2, 4 ];
assert(count(a) == 9);
// count predicate in range
assert(count!("a > 2")(a) == 5);

Meta