Cheat Sheets

A collection of cheat sheets.

Hosted on GitHub Pages — Theme by mattgraham

Maps

Official documentation

Literals

%{ key1 => value1,
   key2 => value2,
   atom_key: value3 } # Shorthand notation for atom keys

Lookup

# Returns nil on missing key
value = map[key]

# If key is atom, raises on missing key
value = map.key

Pattern Matching

%{key: x} = %{key: 5}
# x is now bound to 5

Updating

# Only works for existing keys
map = %{x: 1, y: 2}
updated = %{ map | y: 3 }
# => updated is now %{ x: 1, y: 3 }