nephrotoma
Nephrotoma is a library for creating a tree and querying whether a node exists at a path.
The head of a tree contains only a dictionary of child nodes.
Each node contains a boolean that signifies that this is an endpoint and a dictionary of child nodes.
This tree:
+---------------+
| children: |
| 1, 4 |
+-+--+----------+
| |
| +---------------+
| |
+---------------+ +---------------+
| exists: True | | exists: False |
| children: | | children: |
| (none) | | 8 |
+---------------+ +-+-------------+
|
+---------------+
| exists: True |
| children: |
| 11, 12 |
+-+---+---------+
| |
+-------+ +-------+
| |
+---------------+ +---------------+
| exists: True | | exists: True |
| children: | | children: |
| (none) | | (none) |
+---------------+ +---------------+
could be created with:
let tree = nephrotoma.from_list([1])
|> nephrotoma.append_list([4, 8])
|> nephrotoma.append_list([4, 8, 11])
|> nephrotoma.append_list([4, 8, 12])
and could be checked with:
tree |> nephrotoma.exists([4, 8])
// -> True
tree |> nephrotoma.exists([4, 8, 11])
// -> True
tree |> nephrotoma.exists([1, 9])
// -> False
tree |> nephrotoma.exists([4])
// -> False
tree |> nephrotoma.exists([])
// -> False
Types
The start of a tree, containing only a dictionary of child nodes.
pub type OrderedTree(key) {
OrderedTree(children: Dict(key, OrderedTreeNode(key)))
}
Constructors
-
OrderedTree(children: Dict(key, OrderedTreeNode(key)))
A node, containing a dictionary of child nodes and a boolean value signifying whether this, too, is an endpoint.
pub type OrderedTreeNode(key) {
OrderedTreeNode(
children: Dict(key, OrderedTreeNode(key)),
exists: Bool,
)
}
Constructors
-
OrderedTreeNode( children: Dict(key, OrderedTreeNode(key)), exists: Bool, )
Functions
pub fn append_child_to_node(
node: OrderedTreeNode(a),
key: a,
child: OrderedTreeNode(a),
) -> OrderedTreeNode(a)
pub fn append_list(
tree: OrderedTree(a),
list: List(a),
) -> OrderedTree(a)
Add a path as existing to a tree.
new()
|> append_list([1, 2, 3])
|> exists([1, 2, 3])
// -> True
pub fn append_list_to_node(
node: OrderedTreeNode(a),
list: List(a),
) -> OrderedTreeNode(a)
Add a path as existing in a node.
new_node(False)
|> append_list_to_node([1, 2, 3])
|> exists_in_node([1, 2, 3])
// -> True
pub fn exists(tree: OrderedTree(a), path: List(a)) -> Bool
Check whether a path exists in a node. Always returns false if either the node or inputted path are empty.
let tree = from_list([1, 2, 3])
tree |> exists([1, 2, 3])
// -> True
tree |> exists([1, 2])
// -> False
tree |> exists([1, 2, 3, 4])
// -> False
tree |> exists([])
// -> False
pub fn exists_in_node(
node: OrderedTreeNode(a),
path: List(a),
) -> Bool
Check whether a path exists in a node. Always returns false if either the node or inputted path are empty.
let node = node_from_list([1, 2, 3])
node |> exists_in_node([1, 2, 3])
// -> True
node |> exists_in_node([1, 2])
// -> False
node |> exists_in_node([1, 2, 3, 4])
// -> False
node |> exists_in_node([])
// -> False
pub fn from_list(list: List(a)) -> OrderedTree(a)
Equivalent to creating a now tree and adding a path.
from_list([1, 2, 3])
|> exists([1, 2, 3])
// -> True
pub fn from_node(node: OrderedTreeNode(a)) -> OrderedTree(a)
Create a tree given a node.
node_from_list([1, 2, 3])
|> from_node()
|> exists([1, 2, 3])
// -> True
pub fn new_node(exists: Bool) -> OrderedTreeNode(a)
Create an empty node of an ordered tree, specifying whether the node exists.
let node = new_node(True)
node.exists
// -> True
pub fn node_from_list(list: List(a)) -> OrderedTreeNode(a)
Equivalent to creating a new node and adding a path.
node_from_list([1, 2, 3])
|> exists_in_node([1, 2, 3])
// -> True
pub fn node_from_tree(tree: OrderedTree(a)) -> OrderedTreeNode(a)
Create a nonexistant node given a tree.
from_list([1, 2, 3])
|> node_from_tree()
|> exists_in_node([1, 2, 3])
// -> True
pub fn update_children(
node: OrderedTreeNode(a),
f: fn(Dict(a, OrderedTreeNode(a))) ->
Dict(a, OrderedTreeNode(a)),
) -> OrderedTreeNode(a)
Run a function on the children of a node.
node_from_list([1, 2, 3])
|> update_children(fn(children) { children |> dict.insert(4, new_node(True)) })
|> exists_in_node([1, 4])
// -> True