Radix.prune

You're seeing just the function prune, go back to Radix module for more information.
Link to this function

prune(tree, fun, opts \\ [])

View Source

Specs

prune(tree(), (tuple() -> nil | {:ok, value()}), Keyword.t()) :: tree()

Prunes given tree by invoking fun on adjacent keys.

The callback fun is called with a 5- or 6-element tuple:

  • {k0, k1, v1, k2, v2}, for two adjacent keys k1 and k2 and absent parent k0
  • {k0, v0, k1, v1, k2, v2}, for two adjacent keys k1 and k2 with v0 as parent k0's value

If fun returns {:ok, value} the children k1 and k2 are deleted from tree and value is stored under the parent key k0, overwriting any existing value.

Optionally specify recurse: true to keep pruning as long as pruning changes the tree.

Examples

iex> adder = fn {_k0, _k1, v1, _k2, v2} -> {:ok, v1 + v2}
...>            {_k0, v0, _k1, v1, _k2, v2} -> {:ok, v0 + v1 + v2}
...>         end
iex> t = new()
...> |> put(<<1, 1, 1, 0::1>>, 1)
...> |> put(<<1, 1, 1, 1::1>>, 2)
...> |> put(<<1, 1, 0>>, 3)
iex> # prune, once
iex> prune(t, adder)
{0, {23, [{<<1, 1, 0>>, 3}], [{<<1, 1, 1>>, 3}]}, nil}
iex>  # prune, recursively
iex> prune(t, adder, recurse: true)
{0, [{<<1, 1, 0::size(7)>>, 6}], nil}

iex> adder = fn {_k0, _k1, v1, _k2, v2} -> {:ok, v1 + v2}
...>            {_k0, v0, _k1, v1, _k2, v2} -> {:ok, v0 + v1 + v2}
...>         end
iex> new(for x <- 0..255, do: {<<x>>, x})
...> |> prune(adder, recurse: true)
{0, [{<<>>, 32640}], nil}
iex> Enum.sum(0..255)
32640