keyword (ex_stdlib v0.2.0)

View Source

A 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

key/0

-type key() :: atom().

keyword/0

-type keyword() :: [{atom(), term()}].

value/0

-type value() :: term().

Functions

delete(Keywords, Key)

-spec delete(keyword(), key()) -> keyword().

Deletes all entries with the given key.

Returns a new keyword list with all occurrences of the key removed.

delete_first(Keywords, Key)

-spec delete_first(keyword(), key()) -> keyword().

Deletes the first entry with the given key.

Returns a new keyword list with the first occurrence removed.

drop(Keywords, Keys)

-spec drop(keyword(), [key()]) -> keyword().

Drops the entries with the given keys.

Returns a new keyword list with the specified keys removed.

equal(Keywords1, Keywords2)

-spec equal(keyword(), keyword()) -> boolean().

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.

fetch(Keywords, Key)

-spec fetch(keyword(), key()) -> {ok, value()} | error.

Fetches the first value for the given key.

Returns {ok, Value} if found, or error if not found.

fetch(Keywords, Key, ErrorReason)

-spec fetch(keyword(), key(), term()) -> value().

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.

get(Keywords, Key)

-spec get(keyword(), key()) -> value() | undefined.

Gets the first value associated with the given key.

Returns undefined if the key is not found.

get(Keywords, Key, Default)

-spec get(keyword(), key(), value()) -> value().

Gets the first value associated with the given key.

Returns the default value if the key is not found.

get_values(Keywords, Key)

-spec get_values(keyword(), key()) -> [value()].

Gets all values associated with the given key.

Returns a list of values in the order they appear.

has_key(Keywords, Key)

-spec has_key(keyword(), key()) -> boolean().

Checks if the given key exists in the keyword list.

keys(Keywords)

-spec keys(keyword()) -> [key()].

Returns a list of all keys in the keyword list.

The keys are returned in the same order as they appear, including duplicates.

keyword(Term)

-spec keyword(term()) -> boolean().

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.

merge(Keywords1, Keywords2)

-spec merge(keyword(), keyword()) -> keyword().

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.

new()

-spec new() -> keyword().

Creates a new empty keyword list.

new(List)

-spec new([{key(), value()}] | [key()]) -> keyword().

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.

pop(Keywords, Key)

-spec pop(keyword(), key()) -> {value() | undefined, keyword()}.

Pops the first value for the given key.

Returns {Value, NewKeywords} if the key exists, or {undefined, Keywords} if not.

pop(Keywords, Key, Default)

-spec pop(keyword(), key(), value()) -> {value(), keyword()}.

Pops the first value for the given key with a default.

Returns {Value, NewKeywords} if the key exists, or {Default, Keywords} if not.

pop_first(Keywords, Key)

-spec pop_first(keyword(), key()) -> {value(), keyword()} | error.

Pops the first occurrence of the given key.

Returns {Value, NewKeywords} if found, or error if not found.

pop_first(Keywords, Key, Default)

-spec pop_first(keyword(), key(), value()) -> {value(), keyword()}.

Pops the first occurrence of the given key with a default.

Returns {Value, NewKeywords} if found, or {Default, Keywords} if not found.

put(Keywords, Key, Value)

-spec put(keyword(), key(), value()) -> keyword().

Puts a new key-value pair in the keyword list.

If the key already exists, adds a new entry (does not replace).

put_new(Keywords, Key, Value)

-spec put_new(keyword(), key(), value()) -> keyword().

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.

replace(Keywords, Key, Value)

-spec replace(keyword(), key(), value()) -> keyword().

Replaces the first occurrence of the given key.

If the key doesn't exist, adds it to the front of the list.

split(Keywords, Keys)

-spec split(keyword(), [key()]) -> {keyword(), keyword()}.

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.

take(Keywords, Keys)

-spec take(keyword(), [key()]) -> keyword().

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.

values(Keywords)

-spec values(keyword()) -> [value()].

Returns a list of all values in the keyword list.

The values are returned in the same order as they appear.