代码之家  ›  专栏  ›  技术社区  ›  Alan

中没有匹配的函数子句枚举唯一列表/3

  •  0
  • Alan  · 技术社区  · 5 年前

    在我们运行的代码中:

    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 是枚举代码中的私有函数( 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 可枚举的。从这个错误我们知道它有一些值(8607886079,…)。

    枚举中有什么可以使参数最终为零?

    0 回复  |  直到 5 年前
        1
  •  2
  •   sbacarob    5 年前

    我能重现你的错误。

    似乎发生的是,你可能正在某处建立一个不合适的列表。例如,当您尝试执行以下操作时,可能会发生这种情况:

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

    这个 | 此上下文中的运算符只能用于前置,例如: [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
    

    所以,也许你需要检查一下你是否正在使用 | 就在某个地方把它修好

        2
  •  1
  •   GavinBrelstaff    5 年前
    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] )
    

    有可能你的 某物 是可枚举的,但不是列表- 例如 1..3 -那样的话你可以用 Enum.to_list(1..3) 先拿到你的名单。 看到了吗 Enum.to_list