ExZipper
An Elixir implementation of Huet's Zipper, with gratitude to Rich Hickey's Clojure implementation.
Installation
@deps [
ex_zipper: "~> 1.0"
]
Usage
Below is an outline of the API provided by ExZipper.Zipper. See the full
documentation here for usage examples
Creating zippers
ExZipper.Zipper provides one zipper construction function for working with plain lists,
ExZipper.Zipper.list_zipper/1
iex> Zipper.list_zipper([1,[],2,[3,[4,5]]])
For other tree shapes, define a module that implements the ExZipper.Tree
behaviour and pass it, along with your root data structure, to
ExZipper.Zipper.new/2. The behaviour has three callbacks:
branch?/1returns whether a node is a branch (a node that can have children, even if it currently has none)children/1returns the children of a branch node, in ordermake_node/2returns a new node built from an existing node and a new list of children. For any branchnode,children(make_node(node, kids))must equalkids, or edits made through the zipper will be lost when the tree is rebuilt
For example, to create a zipper for a data structure of nested maps,
where a branch's children are defined under the :kids key:
defmodule MapTree do
@behaviour ExZipper.Tree
@impl true
def branch?(node), do: is_map(node) and Map.has_key?(node, :kids)
@impl true
def children(%{kids: kids}), do: kids
@impl true
def make_node(node, kids), do: %{node | kids: kids}
end
Zipper.new(
MapTree,
%{value: "top", kids: [%{value: "first inner"}, %{value: "last inner"}]}
)
Declaring @behaviour ExZipper.Tree is optional, but it lets the compiler
warn about missing callbacks. Zipper.new/2 does not check the module, so
without it a missing callback raises an UndefinedFunctionError the first
time the zipper calls it.
For a one-off tree that doesn't warrant a module, ExZipper.Zipper.zipper/4
takes the same three functions as arguments, followed by the root.
Navigating zippers
The following functions move through a zipper and return a new zipper. If the
move is impossible (for example, moving down from a leaf), they raise an
ExZipper.Error whose :reason field describes what went wrong.
Zipper.downmoves to the first (leftmost) child of the current nodeZipper.upmoves to the parent of the current nodeZipper.rightmoves to the next sibling on the right of the current nodeZipper.leftmoves to the next sibling on the left of the current nodeZipper.rightmostmoves to the furthest right sibling of the current node, or remains in place if already focused on the rightmost siblingZipper.leftmostmoves to the furthest left sibling of the current node, or remains in place if already focused on the leftmost siblingZipper.roottraverses the zipper all the way back to the root node, or remains in place if already focused on the root
The following functions move through a zipper via a depth-first walk. That is, moving as deep down one branch of the tree before moving laterally.
Zipper.nextmoves to the next node, preferring to move to a child over a siblingZipper.prevmoves back up through a depth-first ordering of the treeZipper.end?returnstrueif the zipper has reached the end of a depth-first walk. At this point the walk has been exhausted:Zipper.next,Zipper.prev, and any other function that moves or changes the zipper (including the editing functions andZipper.map) raise anExZipper.Error. CallZipper.rootto start again.
Searching and transforming zippers
The following functions walk the zipper depth-first, like Zipper.next. Each
walk starts at the current focus, not at the root, and visits the current
focus, its descendants, and every node after it in the walk.
Zipper.find/2returns a zipper focused on the first node for which the given function returns a truthy value, ornilif nothing matches. The current focus is checked first, so callZipper.nextbefore searching again to find the following matchZipper.find_all/2returns a list of zippers, one focused on each matching node, in walk order. Each is a separate snapshot, so editing one does not change the othersZipper.map/2replaces every node in the walk with the result of calling the given function on it, and returns a zipper focused on the root. The function is called on branches as well as leaves, and a branch is updated before its children are visited
iex> zipper = Zipper.list_zipper([1, [], [2, 3, [4, 5]]])
iex> zipper |> Zipper.find(&(is_integer(&1) and &1 > 2)) |> Zipper.node
3
iex> zipper |> Zipper.find_all(&is_integer/1) |> Enum.map(&Zipper.node/1)
[1, 2, 3, 4, 5]
iex> zipper |> Zipper.map(fn n when is_integer(n) -> -n; list -> list end) |> Zipper.node()
[-1, [], [-2, -3, [-4, -5]]]
Editing zippers
The following functions modify the zipper without shifting focus away
from the current node. They return a zipper, or raise an ExZipper.Error if
the edit is impossible (for example, trying to insert a child into a leaf node)
Zipper.insert_childinserts a new child as the first (leftmost) child of the current node, without moving focus from the current nodeZipper.append_childinserts a new child as the last (rightmost) child of the current node, without moving focus from the current nodeZipper.insert_leftinserts a new node as the sibling immediately to the left of the current node, without moving focus from the current nodeZipper.insert_rightinserts a new node as the sibling immediately to the right of the current node, without moving focus from the current nodeZipper.replace/2replaces the zipper's current node with the new node passed to this function as the second argumentZipper.edit/2replaces the zipper's current node with the result of calling the function passed as a second argument on the current nodeZipper.remove/1removes the current node from the zipper, returning a zipper focused on the previous node (via a depth-first walk)
Helper functions
-
Zipper.node/1returns the zipper's current node -
Zipper.lefts/1returns a list of the current node's left siblings -
Zipper.rights/1returns a list of the current node's right siblings -
Zipper.path/1returns a list of nodes creating a path from the root down to, but excluding, the current node -
Zipper.to_list/1returns a depth-first walk ordered list of a zipper's elements -
Zipper.branch?returns true if the current node is a branch (that is, if it can have children, even if it currently does not have any) -
Zipper.childrenreturns a list of the current node's children, or raises anExZipper.Errorif called on a leaf -
Zipper.make_nodetakes a zipper, a node, and a list of children and returns a new node constructed from the second and third arguments. The first argument, the zipper, is passed only to provide context on how to construct the new node.
License
This library is released under the UNLICENSE.