枚举中没有匹配的函数子句。uniq_list/3

no function clause matching in Enum.uniq_list/3

在我们的代码中我们是 运行:

Enum.uniq(something)

我们收到以下错误:

 no function clause matching in Enum.uniq_list/3
 
 lib/enum.ex in Enum.uniq_list/3 at line 3655
 arg0 nil # THIS SHOULD NEVER HAPPEN
 arg1 %{86078 => true, 86079 => true, 86080 => true, 86081 => true, 86082 => true, 86083 => true, 86084 => true}
 arg2 #Function<217.29191728/1 in Enum.uniq/1>

Enum.uniq_list/3是Enum(see here)代码中的私有函数:

defp uniq_list([head | tail], set, fun) do
  value = fun.(head)

  case set do
    %{^value => true} -> uniq_list(tail, set, fun)
    %{} -> [head | uniq_list(tail, Map.put(set, value, true), fun)]
  end
end

defp uniq_list([], _set, _fun) do
  []
end

我们第一次调用该函数时,第一个参数是我们的 something 可枚举的。从错误中我们知道它有一些值(86078、86079、...)。

枚举中可能包含什么,所以参数最终为零?

Enum.uniq( 1, 2, 3 )

产生与您收到的类似的错误消息:

    (UndefinedFunctionError) function Enum.uniq/3 is undefined or private. Did you mean one of:
    
          * uniq/1
          * uniq/2
   (elixir) Enum.uniq(1, 2, 3)

您需要以某种方式将 something 包装在列表中。

Enum.uniq( [1, 2, 3] )

您的 something 可能是一个可枚举的但不是列表 - 例如1..3 - 在这种情况下,您可以先使用 Enum.to_list(1..3) 获取列表。 参见 Enum.to_list

我已经能够重现您的错误。

似乎正在发生的事情是您可能在某处构建了不正确的列表。例如,当您尝试附加执行类似以下操作时,可能会发生这种情况:

iex> l = [1, 2, 3]
[1, 2, 3]
iex> l = [l | 4]
[[1, 2, 3] | 4]

The | operator in this context should only be used to prepend, like: [4 | l]

您可以重现错误:

iex> l = [86078, 86079, 86080, 86081, 86082, 86083, 86084 | nil]
[86078, 86079, 86080, 86081, 86082, 86083, 86084 | nil]
iex> Enum.uniq(l)
** (FunctionClauseError) no function clause matching in Enum.uniq_list/3    
    
    The following arguments were given to Enum.uniq_list/3:
    
        # 1
        nil
    
        # 2
        %{
          86078 => true,
          86079 => true,
          86080 => true,
          86081 => true,
          86082 => true,
          86083 => true,
          86084 => true
        }
    
        # 3
        #Function<217.29191728/1 in Enum.uniq/1>
    
    Attempted function clauses (showing 2 out of 2):
    
        defp uniq_list([head | tail], set, fun)
        defp uniq_list([], _set, _fun)
    
    (elixir 1.10.3) lib/enum.ex:3655: Enum.uniq_list/3
    (elixir 1.10.3) lib/enum.ex:3660: Enum.uniq_list/3
    (elixir 1.10.3) lib/enum.ex:3660: Enum.uniq_list/3

所以,也许你需要检查你是否在某处使用 | 并修复它

在我的例子中,我必须像下面这样检查是否为空:

defp validate_contents([head | tail]) do
if tail != [] do
  validate_contents(tail)
end
end