std.counter

Occurrence counting over dictionaries.

Importuse std.counter

Functions

fn counts<T>(arr: [T]) -> {T: int}#

Counts each element of arr: {element: occurrences} in first-seen order.

  • arr: Elements to tally, in order.
let tally = std.counter.counts(["moon", "sun", "moon"])
println(tally["moon"]) // 2
println(tally.keys())  // ["moon", "sun"]
fn increment<T>(counter: {T: int}, key: T)#

Adds one to key's count (a missing key starts at zero).

  • counter: Counter to update in place.
  • key: Key whose count rises by one.
let hits: {string: int} = {:}
std.counter.increment(hits, "home")
std.counter.increment(hits, "home")
println(hits["home"]) // 2
fn total<T>(counter: {T: int}) -> int#

The sum of all counts (wrapping i64 addition).

  • counter: Counter whose values are added.
let tally = std.counter.counts(["a", "b", "a"])
println(std.counter.total(tally)) // 3
fn most_common<T>(counter: {T: int}, count: int) -> [(T, int)]#

The count highest-count (key, count) pairs, count descending with ties broken by ascending key. count beyond the entry count returns everything; zero or negative returns [].

  • counter: Counter to rank.
  • count: How many pairs to return; more than the counter holds returns everything, and zero or negative returns nothing.
let tally = std.counter.counts(["a", "b", "a", "c", "a", "b"])
println(std.counter.most_common(tally, 2)) // [("a", 3), ("b", 2)]