View Source MPTree.Node (MPTree v0.1.0)

A Tree is made of many nodes. Here is one.

A valid MPTree node is a map with a :__mptree_meta__ key holding a value of meta/0. Here is the absolute minimal node you can build:

my_node = %{__mptree_meta__: init()}

Here's a node implemented as a struct:

defmodule MyNode do
  defstruct [:name, __mptree_meta__: init()]

  @type t :: %__MODULE__{
    name: String.t(),
    __mptree_meta__: MPTree.Node.meta()
  }

  new(name), do: %__MODULE__{name: name}
end

iex> MyNode.new("hello")
%MyNode{name: "hello", __mptree_meta__: _}

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_meta__: _}

But it would be easier if you just used the

Link to this section Summary

Types

Instantiated by MPTree.Node.init/0 and maintained by MPTree

t()

Functions

When creating your nodes, and before inserting into a tree, ensure it has a :__mptree_meta__ key with the value from init/0 assigned to it.

Link to this section Types

Specs

meta()

Instantiated by MPTree.Node.init/0 and maintained by MPTree

Specs

t() :: %{:__mptree_meta__ => meta(), optional(any()) => any()}

Link to this section Functions

When creating your nodes, and before inserting into a tree, ensure it has a :__mptree_meta__ key with the value from init/0 assigned to it.