@@ -938,11 +938,10 @@ end
938938_bool (f) = x-> f (x):: Bool
939939
940940"""
941- count(p, itr) -> Integer
942- count(itr) -> Integer
941+ count([f=identity,] itr; init=0) -> Integer
943942
944- Count the number of elements in `itr` for which predicate `p ` returns `true`.
945- If `p ` is omitted, counts the number of `true` elements in `itr` (which
943+ Count the number of elements in `itr` for which the function `f ` returns `true`.
944+ If `f ` is omitted, counts the number of `true` elements in `itr` (which
946945should be a collection of boolean values).
947946
948947# Examples
@@ -954,30 +953,30 @@ julia> count([true, false, true, true])
9549533
955954```
956955"""
957- count (itr) = count (identity, itr)
956+ count (itr; init = 0 ) = count (identity, itr; init )
958957
959- count (f, itr) = _simple_count (f, itr)
958+ count (f, itr; init = 0 ) = _simple_count (f, itr, init )
960959
961- function _simple_count (pred, itr)
962- n = 0
960+ function _simple_count (pred, itr, init )
961+ n = init
963962 for x in itr
964963 n += pred (x):: Bool
965964 end
966965 return n
967966end
968967
969- function count (:: typeof (identity), x:: Array{Bool} )
970- n = 0
968+ function _simple_count (:: typeof (identity), x:: Array{Bool} , init :: T = 0 ) where {T}
969+ n:: T = init
971970 chunks = length (x) ÷ sizeof (UInt)
972971 mask = 0x0101010101010101 % UInt
973972 GC. @preserve x begin
974973 ptr = Ptr {UInt} (pointer (x))
975974 for i in 1 : chunks
976- n += count_ones (unsafe_load (ptr, i) & mask)
975+ n += count_ones (unsafe_load (ptr, i) & mask) % T
977976 end
978977 end
979978 for i in sizeof (UInt)* chunks+ 1 : length (x)
980- n += x[i]
979+ n += x[i] % T
981980 end
982981 return n
983982end
0 commit comments