StoatPower

StoatPower

Hello everyone,

I’m excited to announce the release of ExRoseTree, a Rose Tree and Zipper in Elixir with a slew of navigation primitives and traversal functions.

This project began as a generic exploration in designing a Zipper for another project of mine using a much more specialized version of a rose tree, but I enjoyed working on it so much that I decided to flesh it out and hopefully make it useful to others, whether directly in their own projects or simply just through learning more about the data structures and algorithms themselves.

Aside from the obvious goals of implementing a rose tree with a functional zipper, my primary aim was to elevate it above a simple ‘example-style’ implementation, as many one-off data structure implementations remain, by designing a sort of navigational pattern language API on top of the Zipper with the hopes of greatly improving its intuitive usability. Furthermore, I wanted to buttress it with extensive test coverage and example usage.

A few possible use-cases

  • outlines
  • file systems
  • parsing HTML/XML documents
  • abstract syntax trees
  • decision trees
  • data visualization (org chargs, family trees, taxonomies, nested menus)

Basically anything that can be hierarchically represented and has (potentially) arbitrary branching. Combined with the Zipper, you get an efficient and context-aware method of traversing and manipulating the tree.

An overview of the “navigational pattern language” (inspired by gender-neutral family tree taxonomy)

Common prefixes include first, last, next, and previous and, along with the _at suffix, are used in conjunction with several major categories of navigation primitives:

  • Direct ancestors - i.e. parent, grandparent, great_grandparent (7 fns)
  • Direct descendants - i.e. child, grandchild, great_grandchild (9 fns)
  • sibling - both before and after the current focus (21 fns)
  • nibling - gender-neutral for niece/nephew (16 fns)
  • pibling - gender-neutral for aunt/uncle (17 fns)
  • Cousins - first cousins, second cousins, etc (12 fns)

Other, less common, descriptors that are used include ancestral, descendant, and extended.

Many of the navigation functions take an optional predicate, allowing you to do a specialized find within the confines of the navigation.

For example Zipper.first_sibling(zipper) will move the Zipper’s focus to the first sibling if one exists, or return nil. But Zipper.first_sibling(zipper, &(&.term == 5)) will look for the first occurrence of a node with its term equal to 5. This is done starting from the first sibling within the list of all previous siblings of the current focus. If a match is found, the Zipper’s focus is moved there. If none match, or if there are no previous siblings, the function will return nil.

Generic and specialized traversals

In addition to the navigation primitives above, there are a wide range of traversal functions, including pure movement, searching, mapping, and accumulating.

The main, single-move traversal primitives include:

  • rewind - moves the focus back along the Zipper’s path. This is like the actual unzipping of a physical zipper, the path being the zipped together teeth of the zipper. Since unzip and zip are overloaded in other contexts (like Enum), I opted for a different word that still captured the essence of the operation.
  • forward - moves the focus to the “right” in a breadth-first manner, towards the last node
  • backward - moves the focus to the “left” in a breadth-first manner, towards the root
  • descend - moves the focus “down” in a depth-first manner, towards the last node
  • ascend - moves the focus “up” in a depth-first manner, towards the root

These (and the other nav primitives) can be used in several generic traversal functions:

  • move_for/3
  • move_if/3
  • move_until/3
  • move_while/3
  • find/3
  • map/3
  • accumulate/4

The 5 main traversal types provide shortcut functions combining their movement with each of the generic traversals, for example: forward_if/2, forward_find/2 or forward_accumulate/3. Ultimately each traversal type has a total of 8 helper functions related to the main primitive: one for each generic traversal function and one for repeating the movement all the way to either the last node or the root.

While there is enough meat here for it get its first release, it is still a work-in-progress, and I already have several ideas for areas of improvement. If you are so inclined to provide feedback, suggestions, or corrections to the docs, design, or functionality, I’d be much obliged. And if you feel like getting hands on with bug fixes or new features, I’m more than open to proposals and pull-requests.

Hopefully some of you find ExRoseTree useful, or at the very least interesting to peruse. So thanks in advance and happy zipping!

Showing Posts 1 to 2

zachallaun

zachallaun

Zippers are great! This library looks very complete.

Some documentation feedback: highlight unfold and usage of it, since it looks like the main mechanism for turning your tree-like-thing into a rose tree.

StoatPower

StoatPower OP

Thank you! Yes, clarifying documentation is high on my priority list: I’d rather make something that already exists better understood before adding more functionality. In that light, thanks for the pointer; I’ll give some love to unfold and try to come up with some more useful examples!

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New

Other Trending Topics Top

akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews