View Source TreeMap (tree_map v0.1.0)

A module to maintain a tree of the structure

%TreeMap{ 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.

Find a node by it's key or returns false

Examples

iex> TreeMap.new()
%TreeMap{}

iex> TreeMap.new(123, "hundretandtwentythree")
%TreeMap{ key: 123, value: "hundretandtwentythree"}

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.

Functions

Link to this function

add_child(tree_map, child)

View Source

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{key: "1", value: "Root", children: [%TreeMap{key: "0", value: "I exist"}]}
Link to this function

add_child(tree_map, key, value, children \\ [])

View Source

Prepend a new child to the children of this node.

Example:

iex> TreeMap.new("1", "Root") |> TreeMap.add_child("1.1", "Sub One")
%TreeMap{key: "1", value: "Root", children: [%TreeMap{key: "1.1", value: "Sub One"}]}

Return the children of this node.

Find a node by it's key or returns false

Link to this function

new(key \\ nil, value \\ nil, children \\ [])

View Source

Examples

iex> TreeMap.new()
%TreeMap{}

iex> TreeMap.new(123, "hundretandtwentythree")
%TreeMap{ key: 123, value: "hundretandtwentythree"}
Link to this function

transduce(tree_map, acc, modifier, reducer)

View Source

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", []]]