Module kvlists

Convenience functions for lists of key/value pairs.

Copyright © (C) 2013 Juan Jose Comellas

Authors: Juan Jose Comellas (juanjo@comellas.org).

Description

Convenience functions for lists of key/value pairs.

Data Types

element_id()

element_id() = atom() | binary()

key()

key() = atom() | binary()

kv()

kv() = {key(), value()}

kvlist()

kvlist() = [kv()]

path()

path() = [path_key()] | path_key()

path_key()

path_key() = key() | non_neg_integer() | {key(), element_id()}

value()

value() = term()

Function Index

delete_nth/2
delete_path/2
delete_value/2Deletes all entries associated with Key from List.
equal/2Compares two lists and returns a boolean indicating whether both lists are equal.
get_path/2Performs the lookup of a Path (list of keys) over a nested List of key/value pairs.
get_value/2Equivalent to get_value(Key, List, undefined).
get_value/3Returns the value of a simple key/value property in List.
get_values/2Returns the list of values corresponding to the different Keys in List.
match/2Matches a list of key-value pairs against a Pattern passed as a key-value list where optional keys, values or key-value pairs can be matched using the '_' atom as a wildcard.
member/2Returns true if there is an entry in List whose key is equal to Key, otherwise false.
set_nth/3
set_path/3Assigns a Value to the element in a List of key/value pairs corresponding to the Key that was passed.
set_value/3Adds a property to the List with the corresponding Key and Value.
set_values/2Sets each Key in List to its corresponding Value.
take_value/2Equivalent to take_value(Key, List, undefined).
take_value/3Returns the value of a simple key/value property in List.
take_values/2Returns the list of values corresponding to the different Keys in List.
with/2Return a NewList where the Key of each element is present in the list of Keys.
without/2Return a NewList where the Key of each element is not present in the list of Keys.

Function Details

delete_nth/2

delete_nth(N::non_neg_integer(), List::kvlist()) -> kvlist()

delete_path/2

delete_path(Path::path(), List::kvlist()) -> value()

delete_value/2

delete_value(Key::key(), List::kvlist()) -> kvlist()

Deletes all entries associated with Key from List.

equal/2

equal(List1::kvlist(), List2::kvlist()) -> boolean()

Compares two lists and returns a boolean indicating whether both lists are equal. Two kvlists are equal when they have the same keys with the same values, independently of the position of each key in the list.

get_path/2

get_path(Path::path(), List::kvlist()) -> value()

Performs the lookup of a Path (list of keys) over a nested List of key/value pairs. Each path_key() can either be a name (atom() or binary()); a positive integer (using 1-based indexing) or an element identifier ({Key, ElementId}). If no value is found corresponding to the Path then [] is returned.

See also: set_path/3.

get_value/2

get_value(Key::key(), List::kvlist()) -> value() | undefined

Equivalent to get_value(Key, List, undefined).

get_value/3

get_value(Key::key(), List::kvlist(), Default::value()) -> value()

Returns the value of a simple key/value property in List. If the Key is found in the list, this function returns the corresponding Value, otherwise Default is returned.

See also: get_value/2, set_value/3.

get_values/2

get_values(Keys::[Key::key() | {Key::key(), Default::value()}], List::kvlist()) -> Values::[value()]

Returns the list of values corresponding to the different Keys in List. If the entry in Keys is found in the List, this function returns the corresponding value. If the entry is not found and it's a {Key, Default} tuple, Default is added to the returned list in its place and if the entry is just a key, then undefined is added to the returned list.

See also: get_value/2, set_values/2.

match/2

match(Pattern::kvlist(), List::kvlist()) -> boolean()

Matches a list of key-value pairs against a Pattern passed as a key-value list where optional keys, values or key-value pairs can be matched using the '_' atom as a wildcard. It returns a boolean value indicating that the match was successful.

The Pattern is evaluated sequentially from head to tail, but the key-value pairs in the List need not follow the same order that was used in the Pattern.

The wildcard atom '_' will match any expression at the nesting level where it was added in the pattern. Some of the ways in which it can be used are:

- To match any value for key foo, use the key-value pair {foo, '_'}. - To match all keys with the same value, use {'_' Value}. - To match any key-value pair use {'_' '_'}. - To match anything (including key-value pairs) use '_'.

The matching rules are applied recursively when the kvlist is nested.

member/2

member(Key::key(), List::kvlist()) -> boolean()

Returns true if there is an entry in List whose key is equal to Key, otherwise false.

set_nth/3

set_nth(N::non_neg_integer(), Value::value(), List::kvlist()) -> kvlist()

set_path/3

set_path(Path::path(), Value::value(), List::kvlist()) -> kvlist()

Assigns a Value to the element in a List of key/value pairs corresponding to the Key that was passed. The Key can be a sequence of names (atom() or binary()); indexes (1-based) or element identifiers ({Key, ElementId}).

See also: get_path/2.

set_value/3

set_value(Key::key(), Value::value(), List::kvlist()) -> kvlist()

Adds a property to the List with the corresponding Key and Value.

See also: get_value/2, get_value/3.

set_values/2

set_values(Tail::[{Key::key(), Value::value()}], List::kvlist()) -> NewList::kvlist()

Sets each Key in List to its corresponding Value.

See also: get_values/2, set_value/3.

take_value/2

take_value(Key::key(), List::kvlist()) -> value() | undefined

Equivalent to take_value(Key, List, undefined).

take_value/3

take_value(Key::key(), List::kvlist(), Default::value()) -> value()

Returns the value of a simple key/value property in List. If the Key is found in the list, this function returns a tuple with the corresponding Value and the List with this entry removed, otherwise it returns a tuple with Default and the original List is returned.

See also: get_value/2, set_value/3, take_values/2.

take_values/2

take_values(Keys::[Key::key() | {Key::key(), Default::value()}], List::kvlist()) -> {Values::[value()], NewList::kvlist()}

Returns the list of values corresponding to the different Keys in List. If the entry in Keys is found in the List, this function returns the corresponding value and removes it from the List. If the entry is not found and it's a {Key, Default} tuple, Default is added to the returned list in its place and if the entry is just a Key, then undefined is added to the returned list.

See also: get_values/2, set_values/2.

with/2

with(Keys::[key()], List::kvlist()) -> NewList::kvlist()

Return a NewList where the Key of each element is present in the list of Keys.

See also: without/2.

without/2

without(Keys::[key()], List::kvlist()) -> NewList::kvlist()

Return a NewList where the Key of each element is not present in the list of Keys.

See also: with/2.


Generated by EDoc