spacesaving v0.0.3 Spacesaving

Summary

Functions

Initialize the state

Merge two states together, adding the counts for keys in both counters. The new state will be the minimum of the two states’ sizes

Add the item, which should be an atom or string to the state

Get the top k counts as a descending sorted key list

Types

countable :: atom | String.t
counter :: {%{}, integer}

Functions

init(n)

Specs

init(integer) :: counter

Initialize the state

Examples

iex> Spacesaving.init(2)
{%{}, 2}
merge(arg1, arg2)

Specs

merge(counter, counter) :: counter

Merge two states together, adding the counts for keys in both counters. The new state will be the minimum of the two states’ sizes.

Examples

iex> Spacesaving.merge({%{foo: 3, baz: 2}, 2}, {%{foo: 3, baz: 2}, 2})
{%{foo: 6, baz: 4}, 2}

iex> Spacesaving.merge({%{foo: 3, bar: 1}, 2}, {%{foo: 3, baz: 2}, 2})
{%{foo: 6, baz: 2}, 2}
push(arg, item)

Specs

Add the item, which should be an atom or string to the state

Examples

iex> Spacesaving.init(2) |> Spacesaving.push(:foo) |> Spacesaving.push(:bar)
{%{foo: 1, bar: 1}, 2}
iex> Spacesaving.init(2) |> Spacesaving.push(:foo) |> Spacesaving.push(:foo) |> Spacesaving.push(:bar) |> Spacesaving.push(:baz)
{%{foo: 2, baz: 2}, 2}
top(arg, k)

Specs

top(counter, integer) :: [{countable, integer}]

Get the top k counts as a descending sorted key list

Examples

iex> {%{foo: 3, baz: 2}, 2} |> Spacesaving.top(2)
[foo: 3, baz: 2]
iex> {%{foo: 3, baz: 2}, 2} |> Spacesaving.top(1)
[foo: 3]