Elixir中的红宝石和操作员是什么?

Yin*_*gce 4 ruby elixir

像这样:

list1 = [1,2,3,4,5]  
list2 = [2,3,6]  
list1 & list2 = [2,3]
Run Code Online (Sandbox Code Playgroud)

我需要找到重复列表中即常见的物品list1list2.

Gaz*_*ler 5

您正在寻找的功能是Set.intersection/2:

iex> Set.intersection(Enum.into([1, 2, 3 ,4 ,5], HashSet.new), Enum.into([2, 3, 6], HashSet.new))
[2, 3]
Run Code Online (Sandbox Code Playgroud)

请注意,转换为集合意味着不允许重复:

Enum.into([1, 2, 3 ,2 ,5, 3], HashSet.new)
HashSet<[2, 3, 1, 5]>
Run Code Online (Sandbox Code Playgroud)

另请注意,订单未得到维护:

iex>Enum.into([1, 2, 3 ,4 ,5, 6], HashSet.new) |> Set.to_list
[2, 6, 3, 4, 1, 5]
Run Code Online (Sandbox Code Playgroud)