2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,7 +1,15 @@
|
|||
empty_map = Map.new #=> %{}
|
||||
map = %{:a => 1, 2 => :b} #=> %{2 => :b, :a => 1}
|
||||
map[:a] #=> 1
|
||||
map[2] #=> :b
|
||||
empty_map = Map.new #=> %{}
|
||||
kwlist = [x: 1, y: 2] # Key Word List
|
||||
Map.new(kwlist) #=> %{x: 1, y: 2}
|
||||
Map.new([{1,"A"}, {2,"B"}]) #=> %{1 => "A", 2 => "B"}
|
||||
map = %{:a => 1, 2 => :b} #=> %{2 => :b, :a => 1}
|
||||
map[:a] #=> 1
|
||||
map[2] #=> :b
|
||||
|
||||
# If you pass duplicate keys when creating a map, the last one wins:
|
||||
%{1 => 1, 1 => 2} #=> %{1 => 2}
|
||||
%{1 => 1, 1 => 2} #=> %{1 => 2}
|
||||
|
||||
# When all the keys in a map are atoms, you can use the keyword syntax for convenience:
|
||||
map = %{:a => 1, :b => 2} #=> %{a: 1, b: 2}
|
||||
map.a #=> 1
|
||||
%{map | :a => 2} #=> %{a: 2, b: 2} update only
|
||||
|
|
|
|||
|
|
@ -1,3 +1,10 @@
|
|||
map = %{:a => 1, :b => 2} #=> %{a: 1, b: 2}
|
||||
map.a #=> 1
|
||||
%{map | :a => 2} #=> %{a: 2, b: 2}
|
||||
empty_set = MapSet.new #=> #MapSet<[]>
|
||||
set1 = MapSet.new(1..4) #=> #MapSet<[1, 2, 3, 4]>
|
||||
MapSet.size(set1) #=> 4
|
||||
MapSet.member?(set1,3) #=> true
|
||||
MapSet.put(set1,9) #=> #MapSet<[1, 2, 3, 4, 9]>
|
||||
set2 = MapSet.new([6,4,2,0]) #=> #MapSet<[0, 2, 4, 6]>
|
||||
MapSet.union(set1,set2) #=> #MapSet<[0, 1, 2, 3, 4, 6]>
|
||||
MapSet.intersection(set1,set2) #=> #MapSet<[2, 4]>
|
||||
MapSet.difference(set1,set2) #=> #MapSet<[1, 3]>
|
||||
MapSet.subset?(set1,set2) #=> false
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
empty_set = HashSet.new #=> #HashSet<[]>
|
||||
set1 = Enum.into(1..4,HashSet.new) #=> #HashSet<[2, 3, 4, 1]>
|
||||
Set.size(set1) #=> 4
|
||||
Set.member?(set1,3) #=> true
|
||||
Set.put(set1,9) #=> #HashSet<[2, 3, 4, 1, 9]>
|
||||
set2 = Enum.into([0,2,4,6],HashSet.new) #=> #HashSet<[0, 2, 6, 4]>
|
||||
Set.union(set1,set2) #=> #HashSet<[0, 2, 6, 4, 3, 1]>
|
||||
Set.intersection(set1,set2) #=> #HashSet<[2, 4]>
|
||||
Set.difference(set1,set2) #=> #HashSet<[3, 1]>
|
||||
Set.subset?(set1,set2) #=> false
|
||||
defmodule User do
|
||||
defstruct name: "john", age: 27
|
||||
end
|
||||
john = %User{} #=> %User{age: 27, name: "john"}
|
||||
john.name #=> "john"
|
||||
%User{age: age} = john # pattern matching
|
||||
age #=> 27
|
||||
meg = %User{name: "meg"} #=> %User{age: 27, name: "meg"}
|
||||
is_map(meg) #=> true
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue