Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 如何将私有函数抽象到实用程序库中?_Elixir - Fatal编程技术网

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

Elixir 如何将私有函数抽象到实用程序库中?,elixir,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 ch

假设我有一堆代码,如下所示:

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

我想在代码的另一部分中使用normalize_路径,将normalize_路径函数抽象到实用模块或库中的最佳方法是什么?我仍然希望保持该功能只在内部使用,而不是作为公共功能使用

最好的办法可能是将这些函数抽象到一个单独的模块中,并用
@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

+1.这正是我在将复杂的私有函数提取到其他模块以帮助测试时使用的模式。@JoséValim我一定是从某个地方得到的,对吗?:)在实用程序模块/库中包含某些内容,但将其隐藏似乎是两个相反的目标。