Key-value dictionary.
This module provides a Key
-Value
dictionary.
The representation of a dictionary is not defined.
This module provides the same interface as the
orddict(3)
module.
One difference is that while this module
considers two keys as different if they do not match (=:=
),
orddict
considers two keys as different if and only if
they do not compare equal (==
).
Functions
append(Key, Value, Dict1) -> Dict2
Dict1 = Dict2 = dict(Key, Value)
Appends a new
to the current list
of values associated with
.
See also section Notes.
append_list(Key, ValList, Dict1) -> Dict2
Dict1 = Dict2 = dict(Key, Value)
ValList = [Value]
Appends a list of values
to
the current list of values associated with
. An
exception is generated if the initial value associated with
is not a list of values.
See also section Notes.
erase(Key, Dict1) -> Dict2
Dict1 = Dict2 = dict(Key, Value)
Erases all items with a given key from a dictionary.
fetch(Key, Dict) -> Value
Dict = dict(Key, Value)
Returns the value associated with
in dictionary
. This function assumes that
is present in dictionary Dict
,
and an exception
is generated if
is not in the dictionary.
See also section Notes.
fetch_keys(Dict) -> Keys
Dict = dict(Key, Value :: term())
Keys = [Key]
Returns a list of all keys in dictionary Dict
.
take(Key, Dict) -> {Value, Dict1} | error
Dict = Dict1 = dict(Key, Value)
Key = Value = term()
This function returns value from dictionary and a
new dictionary without this value.
Returns error
if the key is not present in the dictionary.
filter(Pred, Dict1) -> Dict2
Pred = fun((Key, Value) -> boolean())
Dict1 = Dict2 = dict(Key, Value)
is a dictionary of all keys and values in
for which
is
true
.
find(Key, Dict) -> {ok, Value} | error
Dict = dict(Key, Value)
Searches for a key in dictionary Dict
. Returns
{ok,
, where
is
the value associated with
, or error
if the key is not present in the dictionary.
See also section Notes.
fold(Fun, Acc0, Dict) -> Acc1
Fun = fun((Key, Value, AccIn) -> AccOut)
Dict = dict(Key, Value)
Acc0 = Acc1 = AccIn = AccOut = Acc
Calls
on successive keys and values of
dictionary
together with an extra argument
Acc
(short for accumulator).
must return a new
accumulator that is passed to the next call.
is returned if the dictionary is empty. The evaluation order is
undefined.
from_list(List) -> Dict
Dict = dict(Key, Value)
List = [{Key, Value}]
Converts the
-
list
to dictionary Dict
.
is_empty(Dict) -> boolean()
Dict = dict()
Returns true
if dictionary
has no
elements, otherwise false
.
is_key(Key, Dict) -> boolean()
Dict = dict(Key, Value :: term())
Tests if
is contained in
dictionary
.
map(Fun, Dict1) -> Dict2
Calls
on successive keys and values
of dictionary
to return a new value for
each key. The evaluation order is undefined.
merge(Fun, Dict1, Dict2) -> Dict3
Fun = fun((Key, Value1, Value2) -> Value)
Dict1 = dict(Key, Value1)
Dict2 = dict(Key, Value2)
Dict3 = dict(Key, Value)
Merges two dictionaries,
and
, to create a new dictionary. All the
-
pairs from both
dictionaries are included in the new dictionary. If a key occurs
in both dictionaries,
is called with the
key and both values to return a new value.
merge
can be defined as follows, but is faster:
merge(Fun, D1, D2) -> fold(fun (K, V1, D) -> update(K, fun (V2) -> Fun(K, V1, V2) end, V1, D) end, D2, D1).
new() -> dict()
Creates a new dictionary.
store(Key, Value, Dict1) -> Dict2
Dict1 = Dict2 = dict(Key, Value)
Stores a
-
pair in
dictionary Dict2
. If
already exists in
,
the associated value is replaced by
.
to_list(Dict) -> List
Dict = dict(Key, Value)
List = [{Key, Value}]
Converts dictionary Dict
to a list representation.
update(Key, Fun, Dict1) -> Dict2
Dict1 = Dict2 = dict(Key, Value)
Fun = fun((Value1 :: Value) -> Value2 :: Value)
Updates a value in a dictionary by calling
on
the value to get a new value. An exception is generated if
is not present in the dictionary.
update(Key, Fun, Initial, Dict1) -> Dict2
Dict1 = Dict2 = dict(Key, Value)
Fun = fun((Value1 :: Value) -> Value2 :: Value)
Initial = Value
Updates a value in a dictionary by calling
on
the value to get a new value. If
is not
present in the dictionary,
is stored as
the first value. For example, append/3
can be defined as:
append(Key, Val, D) -> update(Key, fun (Old) -> Old ++ [Val] end, [Val], D).
update_counter(Key, Increment, Dict1) -> Dict2
Dict1 = Dict2 = dict(Key, Value)
Increment = number()
Adds
to the value associated with
and stores this value.
If
is not present in the dictionary,
is stored as the first value.
This can be defined as follows, but is faster:
update_counter(Key, Incr, D) -> update(Key, fun (Old) -> Old + Incr end, Incr, D).
Notes
Functions append
and append_list
are included
so that keyed values can be stored in a list accumulator, for
example:
> D0 = dict:new(), D1 = dict:store(files, [], D0), D2 = dict:append(files, f1, D1), D3 = dict:append(files, f2, D2), D4 = dict:append(files, f3, D3), dict:fetch(files, D4). [f1,f2,f3]
This saves the trouble of first fetching a keyed value, appending a new value to the list of stored values, and storing the result.
Function fetch
is to be used if the key is known to
be in the dictionary, otherwise function find
.