Skip to content

Instantly share code, notes, and snippets.

@rugyoga
Created September 7, 2022 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rugyoga/632850c5fcb5ae628e80b619c3a9680e to your computer and use it in GitHub Desktop.
Save rugyoga/632850c5fcb5ae628e80b619c3a9680e to your computer and use it in GitHub Desktop.
Tree example
defmodule Tree do
alias __MODULE__
use Audit
defstruct left: nil, item: nil, right: nil, __audit_trail__: nil
def add(nil, item), do: %Tree{item: item}
def add(tree = %Tree{left: l, item: i, right: r}, item) do
cond do
item < i -> %Tree{audit(tree) | left: add(l, item)}
item > i -> %Tree{audit(tree) | right: add(r, item)}
true -> tree
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment