Skip to content

Conversation

@apreifsteck
Copy link

@apreifsteck apreifsteck commented Jul 27, 2023

Proposal

Add a dedicated module to working with Graphs that are Trees.

Details

  • Every item added to a tree is treated as unique
  • A tree can be created with a "virtual root", to convert from data structures that are almost tree-like but are missing a root
  • An is_leaf?/2 function
  • A reducer that passes the ancestry of the current node into the client-provided reducer function
  • a few convenience functions
    • leaf_reduce/3
    • leaf_map/2
    • current_path/2

Rationale

The Graph module provides some very excellent utilities for working with graphs, but is somewhat bare on the tree side of things. I had some use cases come up where a dedicated tree data structure would come in handy. The code that's outlined here is a pretty faithful replica of what I rolled at work to remediate that need. The main use case that I've had for this is creating trees of atoms that can be expanded to serve as a key list for a data structure that implements the access behaviour.

For example, here's the implementation of a function we've used to make sure a particular set of values in a large, deep map are non-empty:

    def all_present?(%{} = data, %Tree{} = tree, empty_values) do
    Tree.reduce(tree, true, fn %Tree.Node{data: field} = node, path_stack, all_present ->
      if Tree.is_leaf?(tree, node) do
        data =
          [field | Enum.map(path_stack, & &1.data)]
          |> Enum.reverse()
          |> then(&get_in(data, &1))

        if data in empty_values do
          {:halt, false}
        else
          {:next, all_present}
        end
      else
        {:next, all_present}
      end
    end)
  end

I think it was actually this function that lead me to write in the leaf_reduce function. This one never got refactored though.

I'm happy to make any changes or additions to it, or release it as it's own package if that's your preference. Some of what in here is probably not very consistent with the API, I know. I just didn't want this code to sit locked inside our application any more 👍 .

@apreifsteck apreifsteck changed the title rfc commit Add stronger support for trees Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant