View Source Keyword Lists
A Keyword
list is a list of key-value pairs that has three special characteristics:
- Keys must be
Atom
s. - The key-value pairs are ordered.
- Keys can be duplicated.
Here's how to use keyword lists:
keyword = [name: "Peter", age: 32, name: "Pietah"]
# Access an element using the bracket notation
keyword[:age] # => 32
# But it will only return the first key
keyword[:name] # => "Peter"
# You can also use a helper function
Keyword.get(keyword, :age) # => 32
# Replace an existing value with a new one
Keyword.replace(keyword, :age, 33)
# => [name: "Peter", age: 33, name: "Pietah"]
# But when you update a duplicate key, it will only keep the new value and remove the rest
Keyword.replace(keyword, :name, "Pieter")
# => [name: "Pieter", age: 32]
# Delete all pairs that have a given key
Keyword.delete(keyword, :name)
# => [age: 32]
# Only delete the first occurrence
Keyword.delete_first(keyword, :name)
# => [age: 32, name: "Pietah"]