StoatPower

StoatPower

ExRoseTree - a Rose Tree and Zipper in Elixir with a slew of navigation primitives

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!

First Post!

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.

Where Next?

Popular in Libraries Top

oltarasenko
Dear Elixir community, After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. Most of the changes are around JavaScript interop now that Elixirscript uses the ...
New
OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

Sub Categories:

We're in Beta

About us Mission Statement