A Counter is a device for keeping a count of how often we observe various objects. It is created by giving a type such as c=Counter{String}().

Counts are retrieved with square brackets like a dictionary: c["hello"]. It is safe to retrieve the count of an object never encountered, e.g., c["goodbye"]; in this case 0 is returned.

Counts may be assigned with c[key]=amount, but the more likely use case is using c[key]+=1 to count each time key is encountered.


If c and d are Counters, then c+d creates a new Counter in which the count associated with an object x is c[x]+d[x].


collect(C) for a Counter returns an array containing the elements of C each repeated according to its multiplicty.


Performing hash on a Counter will first apply clean! to the Counter in order that equal Counter objects hash the same.


keys(c::Counter) returns an interator for the things counted by c.


length(c::Counter) gives the number of entries monitored by the Counter. Conceivably, some may have value 0.

See also: nnz.


sum(c::Counter) gives the total of the counts for all things in c.


values(c::Counter) returns an iterator for the counts in c.


clean!(c) removes all keys from c whose value is 0. Generally, it's not necessary to invoke this unless one suspects that c contains a lot of keys associated with a zero value.


Return a list of the elements counted by C in which the element with the highest count is first, the element with the second highest count is second, and so forth. Elements whose count is zero are excluded.


counter(list) creates a Counter whose elements are the members of list with the appropriate multiplicities. This may also be used if list is a Set or an IntSet (in which case multiplicities will all be 1).


csv_print(C::Counter) prints out C in a manner suitable for import into a spreadsheet.


incr!(c,x) increments the count for x by 1. This is equivalent to c[x]+=1.

incr!(c,items) is more useful. Here items is an iterable collection of keys and we increment the count for each element in items.

incr!(c,d) where c and d are counters will increment c by the amounts held in d.


mean(C::Counter) computes the weighted average of the objects in C. Of course, the counted objects must be a Number; their multiplicity (weight) in the average is determined by their C-value.


nnz(c::Counter) gives the number of keys in the Counter with nonzero value.

See also: length.