Map

Creation

Atoms and strings are DIFFERENT keys with different matching and creation syntax

# String Syntax
capitals = %{"Alabama" => "Montgomery",
	"Alaska" => "Juneau", "Arizona" => "Phoenix"}
# Atom Syntax
capitals2 = %{alabama: "Montgomery",
	alaska: "Juneau", arizona: "Phoenix"}

Get

map = %{a: 1, b: 2}
Map.get(“key”) || Map.get(:key) #Atom and String

#Use the atom or string
Map.get(map, :c) #nil
Map.get(map, :c, 3) #3 *note if %{c: nil} then this can return nil
Map.fetch(map, :a) #:error
Map.fetch(map, :c) #{:ok, 1}

map[:b] #atom key
map.b #atom key
map["b"] # String keys

#For dynamic maps
map["non_existing_key"] #nil
map[:non_existing_key] #nil
map.non_existing_key #Key Error

Map.has_key?(event, :description)

Setting

Pattern matching

Other

Pipe Merging

Advanced

Last updated