View Source TreeMap (tree_map v0.1.1)
A module to maintain a tree of the structure
%Node{ key: ..., value: ..., children: [%TreeMap{},...]}
and functions to traverse such a tree.
Examples
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
Summary
Functions
Prepend an exisitng child to the children of this node.
Prepend a new child to the children of this node.
Return the children of this node.
Kill all RootServers.
Find a node by it's key or returns false
in all root nodes.
Find a node by it's key or returns false
in a given tree.
List all root nodes from all RootServers.
Examples
iex> TreeMap.new()
%TreeMap.Node{}
iex> TreeMap.new(123, "hundretandtwentythree")
%TreeMap.Node{ key: 123, value: "hundretandtwentythree"}
Start a root_node for the given %Node{}
.
Create a Node and start a RootServer for it.
Traverse the tree map and apply the given function to each node and a reducer-function to it's children.
Traverse the tree map and apply the given function to each node.
Retreive the value of a given root node.
Functions
Prepend an exisitng child to the children of this node.
Example:
iex> child = TreeMap.new("0", "I exist")
iex> TreeMap.new("1", "Root") |> TreeMap.add_child(child)
%TreeMap.Node{key: "1", value: "Root", children: [%TreeMap.Node{key: "0", value: "I exist"}]}
Prepend a new child to the children of this node.
Example:
iex> TreeMap.new("1", "Root") |> TreeMap.add_child("1.1", "Sub One")
%TreeMap.Node{key: "1", value: "Root", children: [%TreeMap.Node{key: "1.1", value: "Sub One"}]}
Return the children of this node.
Kill all RootServers.
Find a node by it's key or returns false
in all root nodes.
Find a node by it's key or returns false
in a given tree.
List all root nodes from all RootServers.
Examples
iex> TreeMap.new()
%TreeMap.Node{}
iex> TreeMap.new(123, "hundretandtwentythree")
%TreeMap.Node{ key: 123, value: "hundretandtwentythree"}
Start a root_node for the given %Node{}
.
Create a Node and start a RootServer for it.
Traverse the tree map and apply the given function to each node and a reducer-function to it's children.
Example:
iex> 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")])
...> TreeMap.transduce(root, 38, fn a, x -> a + Enum.count(x.children) end, fn x, acc -> acc + x end)
42
iex> 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")])
...> TreeMap.transduce(root, [], fn a, x -> a ++ [x.value] end, fn n, acc -> [n | acc] end)
["Root", "Sub One", "SomeSubSub", "Sub Two", "Some other SubSub"]
iex> 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")])
...> TreeMap.transduce(root, [], fn a, x -> a ++ [x] end, fn n, acc -> [n | acc] end)
...> |> Enum.map(& &1.key)
["1", "1.1", "1.1.1", "1.2", "1.2.1"]
Traverse the tree map and apply the given function to each node.
Example:
iex> root = TreeMap.new("1", "Root") |> TreeMap.add_child("1.1", "Sub One") |> TreeMap.add_child("1.2", "Sub Two")
...> TreeMap.traverse(root, fn x -> x.key end)
["1", ["1.1", [], "1.2", []]]
Retreive the value of a given root node.