Testing Elixir用于测试比较的最佳数据结构

Testing Elixir用于测试比较的最佳数据结构,testing,elixir,Testing,Elixir,我有两个数组输出,需要迭代每个结构,并比较源匹配的计数。比较需要小于或等于。我的输出源如下所示: output_1: [%{source: "facebook", count: 3}, %{count: 1, source: "linkedin"}] output_2: [%{source: "facebook", count: 2}, %{count: 1, source: "linkedin"}] [facebook: 3, linkedin: 1] [facebook: 2, link

我有两个数组输出,需要迭代每个结构,并比较源匹配的计数。比较需要小于或等于。我的输出源如下所示:

output_1: [%{source: "facebook", count: 3}, %{count: 1, source: "linkedin"}]

output_2: [%{source: "facebook", count: 2}, %{count: 1, source: "linkedin"}]
[facebook: 3, linkedin: 1]
[facebook: 2, linkedin: 1]

为了使可枚举项最容易和最有效地进行比较,最好实现什么样的数据结构?

最容易的可能是与一起使用。像下面这样的方法应该可以奏效

output_1 = Enum.sort(output_1, fn a, b -> a.source <= b.source end)
output_2 = Enum.sort(output_2, fn a, b -> a.source <= b.source end)

output_1
|> Enum.zip(output_2)
|> Enum.all?(fn a, b -> a.count == b.count end)
output\u 1=Enum.sort(output\u 1,fn a,b->a.source a.source Enum.zip(output\u 2)
|>全部枚举?(fn a,b->a.count==b.count结束)

如果您的订单没有保证,我的首选方法是将参考列表转换为地图,并按来源进行比较

iex>output_1=[%{source:“facebook”,count:3},%{count:1,source:“linkedin”}]
[%{count:3,来源:“facebook”},%{count:1,来源:“linkedin”}]
iex>output_2=[%{source:“facebook”,count:2},%{count:1,source:“linkedin”}]
[%{count:2,来源:“facebook”},%{count:1,来源:“linkedin”}]
iex>limits=Map.new(输出_1,&{&1.source,&1.count})
%{“facebook”=>3,“linkedin”=>1}

iex>Enum.all?(output_2,&&1.count使用以下代码,您当前的输出格式应该非常有效。您没有说明您预期的输出是什么,也没有说明应该在哪个方向进行比较:
output2两个列表中是否始终有相同的源?或者
output_1
是否可能有一个源为n在
output_2
中的ot?是的,它们总是在任何一侧匹配。您的问题中没有任何结构——这些是映射。
defmodule A do
  def compare(list1, list2), do: _compare(list1, list2, [])

  defp _compare([%{count: count1}|maps1], [%{count: count2}|maps2], acc) do
    _compare(maps1, maps2, [count1 <= count2 | acc])      
  end
  defp _compare([], [], acc) do
    Enum.reverse(acc)
  end

end
~/elixir_programs$ iex a.ex
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.8.2) - press Ctrl+C to exit (type h() ENTER for help)

iex(1)> out1 = [%{source: "facebook", count: 3}, %{count: 1, source: "linkedin"}]
[
  %{count: 3, source: "facebook"},
  %{count: 1, source: "linkedin"}
]

iex(2)> out2 = [%{source: "facebook", count: 2}, %{count: 1, source: "linkedin"}]
[
  %{count: 2, source: "facebook"},
  %{count: 1, source: "linkedin"}
]

iex(3)> A.compare(out1, out2)                               
[false, true]        
defmodule A do
  def compare(list1, list2), do: _compare(list1, list2, true)

  defp _compare([%{count: count1}|maps1], [%{count: count2}|maps2], true) do
    _compare(maps1, maps2, count1 <= count2)

  end
  defp _compare(_, _, false), do: false  #If you find a false comparison, stop and return false
  defp _compare([], [], _), do: true

end
iex(22)> c "a.ex"
warning: redefining module A (current version defined in memory)
  a.ex:1
[A]

iex(23)> A.compare(out1, out2)
false
defmodule A do
  def compare(list1, list2) do
    List.first(list1)[:count] <= List.first(list2)[:count]
    and
    List.last(list1)[:count] <= List.last(list2)[:count]
  end
end
[facebook: 3, linkedin: 1]
[facebook: 2, linkedin: 1]