The predicate to compare elements.
The range to count.
The element or sub-range to count in haystack.
The number of matches in haystack.
// count elements in range int[] a = [ 1, 2, 4, 3, 2, 5, 3, 2, 4 ]; assert(count(a, 2) == 3); assert(count!("a > b")(a, 2) == 5);
import std.uni : toLower; // count range in range assert(count("abcadfabf", "ab") == 2); assert(count("ababab", "abab") == 1); assert(count("ababab", "abx") == 0); // fuzzy count range in range assert(count!((a, b) => toLower(a) == toLower(b))("AbcAdFaBf", "ab") == 2);
Counts matches of needle in haystack.
The first overload counts each element e in haystack for which pred(e, needle) is true. pred defaults to equality. Performs O(haystack.length) evaluations of pred.
The second overload counts the number of times needle was matched in haystack. pred compares elements in each range. Throws an exception if needle.empty is true, as the count of the empty range in any range would be infinite. Overlapped counts are *not* considered, for example count("aaa", "aa") is 1, not 2.
Note: Regardless of the overload, count will not accept infinite ranges for haystack.