You cannot extend an existing module. You can redefine a module but then it will lose all original functions. So I don’t suggest it, instead you should e.g. write your own MyProject.EnumUtils module.
The strength could be to compose a number of functions that need to run on individual items without having to iterate through the list twice, first to flatten then enumerate. Something to have in the pocket for performance issues?
That looks more complex to my beginner eyes but I’ll have to look into why the original function increases the stack size. I though erlang would optimize passing the fragments of nested lists around without copying, or is it something to do with tail recursion? Also not sure what you mean by TCO, but this is looking like a topic for another thread.
I appreciate your input and taking the time to educate.
Passing lists around and TCO are different things. TCO stands for “Tail Call Optimization”, and roughly happens when your recursive call is the last expression in your function. This way, the runtime does not need to keep the stack around until your recursion ends but discards the stack after each call. This way, your recursion is not limited by available memory regarding stack (your accumulator may grow if you don’t care).