From e5791bc5f6e8130e4d41287c7856c3e7f4da4d34 Mon Sep 17 00:00:00 2001 From: Mike J Innes Date: Sat, 17 Feb 2018 11:19:14 +0000 Subject: [PATCH] frequencies utility --- src/utils.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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)