oliverandrich

oliverandrich

Stylistic question: How to break out of a recursive tree processing?

I am currently building a small library that has to traverse a tree of nodes and transform this tree into a document. This tree traversal and transformation is done in a recursive function. Now, it might happen, that the tree contains unsupported nodes. And I want to provide an option for the function, that either skips these unsupported nodes or returns an error tuple in this situation. So far, the easy part, the requirements.

What is the idiomatic Elixir way to stop processing a tree in a recursive function? I plan to handle this with one public function that is called by the user of my module, and the recursive processing is done in a private function. Normally, I would raise an exception and catch this in the public function, either return an ok or an error tuple.

But is this idiomatic?

Marked As Solved

LostKobrakai

LostKobrakai

The ideomatic way here would be to use throw+try/catch, not exceptions. It’s not exceptional, but a expected part of your execution flow.

Also Liked

dimitarvp

dimitarvp

I would suggest you support passing a closure to your processing function that must return e.g. {:ok, :keep_going} or {:error, :unsupported_node, the_node_variable}. That way your processing function use e.g. Enum.reduce_while or Enum.take_while or some such.

I wouldn’t recommend exceptions or any of the error throwing mechanisms. Elixir has good mechanisms to do an early stop when processing.

Last Post!

dimitarvp

dimitarvp

I would suggest you support passing a closure to your processing function that must return e.g. {:ok, :keep_going} or {:error, :unsupported_node, the_node_variable}. That way your processing function use e.g. Enum.reduce_while or Enum.take_while or some such.

I wouldn’t recommend exceptions or any of the error throwing mechanisms. Elixir has good mechanisms to do an early stop when processing.

Where Next?

Popular in Questions Top

New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement