keyword (ex_stdlib v0.2.0)
View SourceA keyword list is a list of two-element tuples where the first element is an atom (the key) and the second element is any term (the value).
Keyword lists provide a convenient way to associate keys with values and are commonly used for options and configuration parameters.
This module provides functions for working with keyword lists, inspired by Elixir's Keyword module but adapted for Erlang.
Examples:
Options = [{port, 8080}, {host, "localhost"}, {ssl, true}],
Port = keyword:get(Options, port, 3000), % Returns 8080
NewOptions = keyword:put(Options, timeout, 5000).
Summary
Functions
Deletes all entries with the given key.
Deletes the first entry with the given key.
Drops the entries with the given keys.
Checks if two keyword lists are equal.
Fetches the first value for the given key.
Fetches the first value for the given key, raising an error if not found.
Gets the first value associated with the given key.
Gets the first value associated with the given key.
Gets all values associated with the given key.
Checks if the given key exists in the keyword list.
Returns a list of all keys in the keyword list.
Checks if the given term is a valid keyword list.
Merges two keyword lists.
Creates a new empty keyword list.
Creates a new keyword list from the given list of key-value pairs.
Pops the first value for the given key.
Pops the first value for the given key with a default.
Pops the first occurrence of the given key.
Pops the first occurrence of the given key with a default.
Puts a new key-value pair in the keyword list.
Puts a new key-value pair in the keyword list only if the key doesn't exist.
Replaces the first occurrence of the given key.
Splits the keyword list into two lists based on the given keys.
Takes only the entries with the given keys.
Returns a list of all values in the keyword list.
Types
Functions
Deletes all entries with the given key.
Returns a new keyword list with all occurrences of the key removed.
Deletes the first entry with the given key.
Returns a new keyword list with the first occurrence removed.
Drops the entries with the given keys.
Returns a new keyword list with the specified keys removed.
Checks if two keyword lists are equal.
Two keyword lists are considered equal if they contain the same key-value pairs in the same order.
Fetches the first value for the given key.
Returns {ok, Value} if found, or error if not found.
Fetches the first value for the given key, raising an error if not found.
Returns the value if found, or calls error/1 with the given reason if not found.
Gets the first value associated with the given key.
Returns undefined if the key is not found.
Gets the first value associated with the given key.
Returns the default value if the key is not found.
Gets all values associated with the given key.
Returns a list of values in the order they appear.
Checks if the given key exists in the keyword list.
Returns a list of all keys in the keyword list.
The keys are returned in the same order as they appear, including duplicates.
Checks if the given term is a valid keyword list.
A keyword list is a list of two-element tuples where the first element is an atom.
Merges two keyword lists.
Keys in the second list take precedence over keys in the first list. If a key exists in both lists, all values from the second list will appear first.
-spec new() -> keyword().
Creates a new empty keyword list.
Creates a new keyword list from the given list of key-value pairs.
If the input is already a valid keyword list, returns it unchanged. If the input is a list of atoms, creates a keyword list with nil values.
Pops the first value for the given key.
Returns {Value, NewKeywords} if the key exists, or {undefined, Keywords} if not.
Pops the first value for the given key with a default.
Returns {Value, NewKeywords} if the key exists, or {Default, Keywords} if not.
Pops the first occurrence of the given key.
Returns {Value, NewKeywords} if found, or error if not found.
Pops the first occurrence of the given key with a default.
Returns {Value, NewKeywords} if found, or {Default, Keywords} if not found.
Puts a new key-value pair in the keyword list.
If the key already exists, adds a new entry (does not replace).
Puts a new key-value pair in the keyword list only if the key doesn't exist.
If the key already exists, returns the original keyword list unchanged.
Replaces the first occurrence of the given key.
If the key doesn't exist, adds it to the front of the list.
Splits the keyword list into two lists based on the given keys.
Returns {Taken, Dropped} where Taken contains entries with the specified keys and Dropped contains the remaining entries.
Takes only the entries with the given keys.
Returns a new keyword list containing only the specified keys. The order of keys in the result follows the order in the original list.
Returns a list of all values in the keyword list.
The values are returned in the same order as they appear.