A module to maintain a tree of the structure TreeMap.Node
and to start root-nodes as
supervised children of TreeMap.RootServer
.
%Node{ key: ..., value: ..., children: [%Node{},...]}
root =
TreeMap.new("1", "Root")
|> TreeMap.add_child("1.1", "Sub One", [TreeMap.new("1.1.1", "SomeSubSub")])
|> TreeMap.add_child("1.2", "Sub Two", [TreeMap.new("1.2.1", "Some other SubSub")])
expected = ["1", "1.1", "1.1.1", "1.2", "1.2.1"]
received =
TreeMap.transduce(root, [], fn a, x -> a ++ [x] end, fn n, acc -> [n | acc] end)
|> Enum.map(& &1.key)
assert expected == received
TreeMap.RootServer
is a DynamicSupervisor
that can be used to maintain a tree of TreeMap
nodes.
iex> TreeMap.start_root_node("A", "Root")
{:ok, pid}
iex> TreeMap.list_roots()
[%Node{}, ...]
iex> TreeMap.find("key")
%Node{}
iex> TreeMap.drop_all()
[:ok, ...]
Available in Hex, the package can be
installed by adding tree_map
to your list of dependencies in mix.exs
:
def deps do
[
{:tree_map, "~> 0.1.0"}
]
end