Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I abstract private functions into a utility library?

Tags:

elixir

Say I have a bunch of code like below :

def dirs(path, regex_dir \\ ".+") do
  path
  |> normalize_path
  |> do_dirs([], regex_dir)
end

# list of bitstrings
defp normalize_path([path | rest]) when is_bitstring(path) do
  [path | normalize_path(rest)]
end

# list of character lists
defp normalize_path([path | rest]) when is_list(path) do
  [to_string(path) | normalize_path(rest)]
end

defp normalize_path([]) do
  []
end

# bitstring
defp normalize_path(path) when is_bitstring(path) do
  [path]
end

# character list
defp normalize_path(path) when is_list(path) do
  [to_string(path)]
end

I want to use the normalize_path in another part of the code, what's the best way to abstract away the normalize_path function into a utility module or library ? I still want to maintain the function to only be used internally and not available as a public function.

like image 646
Muhammad Lukman Low Avatar asked Nov 30 '25 02:11

Muhammad Lukman Low


1 Answers

Probably your best shot is abstracting those functions in a separate module and hide it in the documentation with @moduledoc false. Those functions won't be private and will still be accessible to users of your library, but by not documenting them you're flagging them as not part of the library's API.

defmodule Helpers do
  @moduledoc false

  @doc """
  You can still provide per-function docs for documenting how the code works;
  these docs won't be public anyways since `@moduledoc false` hides them.
  """
  def helper(...), do: ...
end
like image 90
whatyouhide Avatar answered Dec 04 '25 13:12

whatyouhide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!