While learning Elixir I’ve been writing and rewriting some practice functions. Some of the rewriting was to ensure recursive functions are tail-call-optimized (TCO) or to prevent expensive operations like concatenating lists or Enum.count(list)
.
(and yes mr Knuth I know about premature optimization being bad)
Some things I’ve noticed myself doing while rewriting functions is:
- naming: for public function
foo
name the corresponding private function_foo
- the private functions often have an extra parameter (so I can prepend to list for TCO)
- prepended result lists are reversed before they’re returned
- in public function heads pattern match for simple cases (as I can often do that without the need for the extra parameter used in the private function)
And I’d like to get some feedback on whether what I’m doing is idiomatic/common practice.
My code is here: https://gist.github.com/nielsbom/8aaec8838d927cac9f72b0e7c28770b2
Just $ elixir bla.exs
to run the tests.
Thanks!