dictionary
Hash tables and dictionaries have achieved a hash of behavior Dict. Keyword module basically implements, except that it supports a duplicate key.
One type of collection Eunm.into mapping may be converted to another.
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 related 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"}
example:
people = [ % {name: "Grumpy, height: 1.24}, % {name:" Dave ", height: 1.88 }, % {name:" Dopey ", height: 1.32 }, % {name:" Shaquille ", height : of 2.16 }, % {name: "sneezy", height: 1.28 } ] for Person = {% height: height} <- people, # hashtable list bound to the person, and the height of the height value binding height > 1.5 , # selected height above 1.5 do: IO.inspect person
Example 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)
Update hash table
new_map =% {old_map | key => value, ...}, create a new hash table, which is a copy of the old hash list, but with the right key pipeline operator corresponding value will be updated.
m =% {A:. 1, B: 2, C:. 3 }
M1 =% {m | B: "TWO", C: "Three"} #% {A:. 1, B: "TWO, C:" Three "}
# to add a new key is required Dict.put_new / 3 function
Structure
Structure is the module that encapsulates a limited form of hash table. It is limited because the bond must be atomic, and the hash table does not have the characteristics and Access Dict. Use defstruct to define the nature of the hash table.
Use structure is equivalent to the hash table is the same, but the structure has default parameters.
defmodule for Subscriber do defstruct name: "", Paid: to false , over_18: to true End S1 =% for Subscriber {} # =>% for Subscriber {name: "", over_18: to true , Paid: to false } S2 =% for Subscriber {name: " Mary ", Paid: to true } # =>% Subscriber {name:" Mary ", over_18: to true , Paid: to true } matching s2.name # point marked by the access key must be atomic, consistent characteristic structure % Subscriber { name: a_name} = S3 a_name # => "Mary"
update
s3 =% Subscriber {s2 | name : "Marie"}
example:
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 protocol implements the hash table, you can use [] access. We can add this functionality to the structure.
defmodule Attendee do @derive Access defstruct name: "", over_18: false end a = %Attendee{name: "Sally", over_18: true} a[:name] #=> "Sally
Nested dictionary structure
Dictionary type allows keys and values associated with these values themselves may also be a dictionary type.
defmodule the Customer do defstruct name: "", Company: "" End defmodule BugReport do defstruct owner: % {}, Details: "", Serverity:. 1 End Report =% BugReport {owner:% the Customer {name: "Dave", Company : "Pragati"}, Detail: "Broken" } # access report.owner.company # update / modify Report = {% BugReport Report | owner:% report.owner the Customer {| Company: "PragProg" }} #put_in may be provided value nested structure inside put_in (report.owner.company, "PargProg" ) #update_in allows us to perform a function at a certain value on the structure update_in (report.owner.name, &( "Mr." <> & 1)) # connection "Mr." and name