Dusty

Dusty

In the course of working on a tree-like data structure, I have designed a struct for each node in the tree. Since there are only 5 fields in each node, and all nodes have the same 5 fields, using a struct made sense to me.

However, one of the %Node{} struct fields contains the children of the node, which is a list of %Node{} structs, each of which can have its own children, and so on.

Given the challenges of accessing nested structs, I am beginning to question my original decision to use a struct instead of a map. I see that there are a number of posts already on how to access nested structs, but I’m still left asking: What does a struct really offer me here? Basically, it offers me the ability to limit the keys for each node. But is that a worthwhile tradeoff, given that the number of nested levels could be very high? Is it really a problem to just use a map?

The nature of the code is such that reaching down many levels at once is unlikely to be needed. For the most part, the tree will be walked one level at a time, but it will frequently be necessary to match on or modify fields in the node’s children (1 level down). It seems that the risk of unintended fields on the struct is perhaps not worth the complexity of using a struct in the first place.

Thoughts?

Showing Posts 1 to 10

MrDoops

MrDoops

Depends - a couple I’d things to try:

You could try wrapping/adapting an existing graph data structure like libgraph or digraph maybe using your existing %Node{} struct as a vertex. There are functions you can you use to access and travel the graph.

But you might get somewhere useful quicker with Kernel.get_in/2.

NobbZ

NobbZ

I don’t see how accessing nested structs is different from nested maps, as structs are just maps with some additional field from this point of view.

Structs, nested or not, enable you to have at least limited compiletime checking of fieldnames.

Using bare maps you won’t have that.

Dusty

Dusty OP

I guess my concern was mostly about behaviours that are missing from structs, like Enumerable and Access. Access seems like the issue for deeply nested structs, as it normally allows arbitrary nesting.

Dusty

Dusty OP

Thanks for your thoughts. I had a good look at libgraph a while back, and I decided that my use case is sufficiently specific that I wouldn’t use any of the provided API out of the box.

I don’t understand why Kernel.get_in/2 would be an option for structs. The docs say that get_in uses the Access module, which I understood not to be an implemented behaviour for structs. What am I missing?

NobbZ

NobbZ

Not beeing able to use Access out of the box for your Node structs is actually a feature in my opinion. This enables you to come up with an implementation of Access that would make even more sense for a tree.

dimitarvp

dimitarvp

You can draw inspiration from my extremely amateurish Trie library which was basically my first ever Elixir code somewhere back in 2016, here: trie/lib/trie.ex at master · dimitarvp/trie · GitHub. This demonstrates a basic Access.get implementation. Once your struct has that you could easily use get_in with instances of the struct as parameters.

Also check out Access docs. You need only implement a few functions to be able to use get_in and put_in.

domvas

domvas

Your struct is a good idea. It’s in fact better than a bare map because you need the field where children are stored. Without it, some of your code can break.
Regarding your node data / properties, you can have anything you want if you specify a filed for them like this:

@type t :: %Node{
  children: [Node.t], # This is mandatory for each node
  id: integer,        # This is an example data that must be defined for each node
  properties: map     # Other data specific to the node
}

Regarding the missing Enumerable, you have two solutions:

But on in all, it can be interesting to code your own traversal to have more control on what it’s done. Something like Node.update(depth, filter_fn, update_fn) could be nice for example.

Additionnally, if you want to go back and forth in your structure while updating it may be useful to investigate Zippers.
Hard to code but pure delight afterwards when using it.

Dusty

Dusty OP

In the beginning, I took a serious look at just using your library, Dimi, but I decided in the end that I really need to match at any point in the string. Don’t call it amateurish, I’ve been treating it like the gold standard! :slight_smile:

Dusty

Dusty OP

Using Map.from_struct/1 as a panic button seems like a good option. I will probably only need to implement Access, if that. Thanks for your thoughts.

dimitarvp

dimitarvp

Thanks for the kind words but I still don’t deserve that much praise for pretty much my first Elixir project. :blush:

I know I’ve seen one much better than mine recently here, casually linked by someone in a thread (which I now can’t find).

In any case, I did my best to utilise best coding practices and tidy it up a bit some months ago and I think that for its very basic use case it does the job well. Glad you found it useful!

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews