View Source MPTree.Node (MPTree v0.2.0)
A Tree is made of many nodes. Here is one.
A valid MPTree
node is a map with a :__mptree_node__
key holding a value of meta/0
.
Here is the absolute minimal node you can build:
my_node = %{__mptree_node__: init()}
Here's a node implemented as a struct:
defmodule MyNode do
defstruct [:name, __mptree_node__: init()]
@type t :: %__MODULE__{
name: String.t(),
__mptree_node__: MPTree.Node.meta()
}
new(name), do: %__MODULE__{name: name}
end
iex> MyNode.new("hello")
%MyNode{name: "hello", __mptree_node__: _}
If you're a fan of the excellent little TypedStruct
library,
you can just use MPTree.Node
as a plugin.
The below module compiles to the exact same result as above,
but is cleaner, more expressive,
and hides more MPTree
implementation details.
defmodule MyNode do
use TypedStruct
typedstruct do
plugin MPTree.Node
field :name, String.t()
end
new(name), do: %__MODULE__{name: name}
end
iex> MyNode.new("hello")
%MyNode{name: "hello", __mptree_node__: _}
But it would be easier if you just used the
Link to this section Summary
Link to this section Types
@opaque meta()
Instantiated by MPTree.Node.init/0
and maintained by MPTree
Link to this section Functions
When creating your nodes, and before inserting into a tree,
ensure it has a :__mptree_node__
key with the value from init/0
assigned to it.