如何将私有函数抽象到实用程序库中?

Low*_*ong 3 elixir

说我有一堆代码如下:

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
Run Code Online (Sandbox Code Playgroud)

我想在代码的另一部分中使用normalize_path,将normalize_path函数抽象为实用程序模块或库的最佳方法是什么?我仍然希望将该函数保持为仅在内部使用,而不是作为公共函数使用.

wha*_*ide 6

可能你最好的镜头是在一个单独的模块中抽象这些函数并将其隐藏在文档中@moduledoc false.这些函数不会是私有的,您的库的用户仍然可以访问这些函数,但是如果不记录它们,则表示它们不是库的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
Run Code Online (Sandbox Code Playgroud)

  • +1.这正是我在将复杂的私有函数提取到其他模块以帮助测试时使用的模式. (2认同)