字典:散列表、散列字典、关键字列表、集合与结构体
字典
散列表和散列字典都实现了Dict的行为。Keyword模块也基本实现了,不同之处在于它支持重复键。
Eunm.into可以将一种类型的收集映射转化成另一种。
defmodule Sum do def values(dict) do dict |> Dict.values |> Enum.sum end end hd = [ one: 1, two: 2, three: 3 ] |> Enum.into HashDict.new IO.puts Sum.values(hd) #=>6
Dict相关API
kw_list = [name: "Dave", likes: "Programming", where: "Dallas"] hashdict = Enum.into kw_list, HashDixt.new map = Enum.into kw_list, Map.new kw_list[:name] #=>"Dave" hashdict[:likes] #=>"Programming" map[:where] #=>"Dallas" hashdict = Dict.drop(hashdict, [:where, :likes]) #=>HashDict<[name: "Dave"]> hashdict = Dict.put(hashdict, :also_likes, "Ruby) #=>HashDict<[name: "Dave", also_likes: "Ruby"]> combo = Dict.merge(map, hashdict) #合并=>%{also_likes: "Ruby", likes: "Programming", name: "Dave", where: "Dallas"}
例子:
people = [ %{ name: "Grumpy, height: 1.24 }, %{ name: "Dave", height: 1.88 }, %{ name: "Dopey", height: 1.32 }, %{ name: "Shaquille", height: 2.16 }, %{ name: "Sneezy", height: 1.28 } ] for person = %{ height: height } <- people, #将列表中的散列表绑定到person ,并将高度值绑定到height height > 1.5, #筛选出高度高于1.5的 do: IO.inspect person
例子2:
def book(%{name: name, height: height}) when height > 1.9 do ... end def book(%{name: name, height: height}) when height < 1.3 do ... end def book(person) do ... end people |> Enum.each(&HotelRoom.book/1)
更新散列表
new_map = %{ old_map | key => value, ...},会创建一个新的散列表,它是旧散列表的拷贝,但是管道运算符右边与键所对应的值会被更新。
m = %{ a: 1, b: 2, c: 3 }
m1 = %{ m | b: "two", c: "three" } #%{ a: 1, b: "two, c: "three" }
#要增加新的键值需要使用Dict.put_new/3函数
结构体
结构体就是模块,它封装了一个有限形式的散列表。有限是因为键必须为原子,并且这些散列表不具备Dict和Access特性。使用defstruct来定义散列表的性质。
结构体的使用就相当于是散列表一样,只不过结构体有默认参数。
defmodule Subscriber do defstruct name: "", paid: false, over_18: true end s1 = %Subscriber{} #=>%Subscriber{name: "", over_18: true, paid: false } s2 = %Subscriber{name: "Mary", paid: true} #=>%Subscriber{name: "Mary", over_18: true, paid: true } 匹配 s2.name #通过点标记访问,键必须为原子,符合结构体的特性 %Subscriber{name: a_name} = s3 a_name #=>"Mary"
更新
s3 = %Subscriber{ s2 | name: "Marie" }
例子:
defmodule Attendee do defstruct name: "", paid: false, over_18: true def may_attend_after_party(attendee = %Attendee{}) do #函数参数使用 %Attendee{} 接受结构体 attendee.paid && attendee.over_18 end def print_vip_badge(%Attendee{name: name}) when name != "" do IO.puts "Very cheap badge for #{name}" end def print_vip_bage(%Attendee{}) fo raise "missing name for badge" end end
散列表实现了Access协议,所以可以使用 [] 访问。我们可以给结构体添加这个功能。
defmodule Attendee do @derive Access defstruct name: "", over_18: false end a = %Attendee{name: "Sally", over_18: true} a[:name] #=> "Sally
嵌套字典结构
字典类型可以让键和值相关联,这些值本身也可以是字典类型。
defmodule Customer do defstruct name: "", company: "" end defmodule BugReport do defstruct owner: %{}, details: "", serverity: 1 end report = %BugReport{owner: %Customer{name: "Dave", company: "Pragmatic"}, detail: "broken"} #访问 report.owner.company #更新/修改 report = %BugReport{ report | owner: %Customer{ report.owner | company: "PragProg" }} #put_in可以设置嵌套结构里面的值 put_in(report.owner.company, "PargProg") #update_in可以让我们在结构体上的某一个值上执行一个函数 update_in(report.owner.name, &("Mr. " <> &1)) #连接 "Mr. "和name