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:

  1. branch?/1 returns whether a node is a branch (a node that can have children, even if it currently has none)
  2. children/1 returns the children of a branch node, in order
  3. make_node/2 returns a new node built from an existing node and a new list of children. For any branch node, children(make_node(node, kids)) must equal kids, 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.

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.

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.

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)

Helper functions

License

This library is released under the UNLICENSE.