diff --git a/src/utils.jl b/src/utils.jl index 464d9295..facba05a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -26,6 +26,26 @@ chunk(xs, n) = collect(Iterators.partition(xs, ceil(Int, length(xs)/n))) batchindex(xs, i) = (reverse(Base.tail(reverse(indices(xs))))..., i) +""" + frequencies(xs) + +Count the number of times that each element of `xs` appears. + +```julia +julia> frequencies(['a','b','b']) +Dict{Char,Int64} with 2 entries: + 'b' => 2 + 'a' => 1 +``` +""" +function frequencies(xs) + fs = Dict{eltype(xs),Int}() + for x in xs + fs[x] = get(fs, x, 0) + 1 + end + return fs +end + """ batch(xs)