counter

Types

A Counter is a specialized dictionary used for counting the occurrences of items. Counter is implemented using the gleam/dict module.

pub opaque type Counter(a)

Functions

pub fn add(
  counter_1: Counter(a),
  counter_2: Counter(a),
) -> Counter(a)

Add the counts from two Counters into a new Counter.

pub fn elements(counter: Counter(a)) -> List(a)

Returns a non-unique list of all the items in the Counter. Each item will be repeated in the list the number of times it was inserted into the Counter.

pub fn from_dict(d: Dict(a, Int)) -> Counter(a)

Creates a Counter from a gleam/dict of item-count pairs.

pub fn from_list(items: List(a)) -> Counter(a)

Creates a Counter from a list of items.

pub fn get(counter: Counter(a), item: a) -> Int

Returns the number of times an item has been inserted into the Counter.

pub fn insert(counter: Counter(a), item: a) -> Counter(a)

Inserts an item into the Counter.

pub fn keys(counter: Counter(a)) -> List(a)

Returns a list of all the unique items in the Counter.

pub fn most_common(
  counter: Counter(a),
  n: Option(Int),
) -> List(#(a, Int))

Returns a list containing of item-count tuples, sorted by count in descending order. If n is None, the entire list is returned. If n is Some(Int), at most n items are returned.

pub fn new() -> Counter(a)

Creates a new empty Counter.

pub fn size(counter: Counter(a)) -> Int

Returns the number of unique items in the Counter.

pub fn subtract(
  counter_1: Counter(a),
  counter_2: Counter(a),
) -> Counter(a)

Subtracts the counts in the second Counter from the first Counter, returning a new Counter with the results. Only items present in both Counters will be used when subtracting counts from the first Counter and any item with a count of zero or less will be omitted from the new Counter.

pub fn to_dict(counter: Counter(a)) -> Dict(a, Int)

Returns the underlying gleam/dict of the Counter.

pub fn to_list(counter: Counter(a)) -> List(#(a, Int))

Returns a list of item-count tuples.

pub fn total(counter: Counter(a)) -> Int

Returns the total number of items in the Counter.

pub fn update(counter: Counter(a), items: List(a)) -> Counter(a)

Updates the Counter from a list of items.

pub fn values(counter: Counter(a)) -> List(Int)

Returns a list of all the counts in the Counter.

Search Document