dict(3)
π dict(3) π
okay bestie, so this is the manual page for dict(3) from the Erlang stdlib β yes, the module definition one, we're in the big leagues now β¨
πΈ NAME πΈ
dict β a key-value dictionary. that's it, that's the girl π
π¦ DESCRIPTION π¦
okay so first things first, the way a dictionary is actually represented internally? not defined. we don't know her, we don't ask, we just use her. mysterious queen energy fr fr π
This module gives you the exact same interface as the orddict module does. but there's one lil difference that lowkey matters a lot:
dictconsiders two keys different if they do not match (=:=)orddictconsiders two keys different if and only if they do not compare equal (==)
NOTE: for new code, prefer maps over this module. like genuinely. this is your bestie telling you, not the manpage (okay it's kinda the manpage too but still) π
β¨ See Also β¨
gb_trees, orddict, maps β go read those too, they're giving range π
π DATA TYPES π
dict() :: dict(_, _).-opaque dict(Key, Value)β a dictionary as returned bynew/0. it's opaque bestie, you can look but you cannot see inside okay π
π FUNCTIONS π
append(Key, Value, Dict1) -> Dict2βwhen Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value).
appends a new Value to the current list of values associated with Key. it's an adding-to-the-list moment, not a replacing moment, iykyk π
Examples
1> Dict0 = dict:from_list([{a,[]}]).
2> Dict1 = dict:append(a, 1, Dict0).
3> dict:to_list(Dict1).
[{a,[1]}]
4> Dict2 = dict:append(a, 10, Dict1).
5> dict:to_list(Dict2).
[{a,[1,10]}]
append_list(Key, ValList, Dict1) -> Dict2βwhen Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value), ValList :: [Value].
appends a whole list of values ValList to the current list of values associated with Key. bulk appending, we love efficiency β¨
π¨ an exception is generated if the initial value associated with Key is not a list of values. so check your vibes first bestie.
Examples
1> Dict0 = dict:from_list([{a,[]}]).
2> Dict1 = dict:append_list(a, [1,2,3], Dict0).
3> dict:to_list(Dict1).
[{a,[1,2,3]}]
erase(Key, Dict1) -> Dict2βwhen Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value).
erases an item with a given key from a dictionary. gone, deleted, no longer with us, she's not coming back π
Examples
1> Dict0 = dict:from_list([{2,b},{1,a}]).
2> Dict1 = dict:erase(2, Dict0).
3> dict:to_list(Dict1).
[{1,a}]
4> Dict2 = dict:erase(99, Dict0).
fetch(Key, Dict) -> Valueβwhen Dict :: dict(Key, Value).
returns the value associated with Key in dictionary Dict. the classic "gimme the thing" move π«Ά
this function assumes thatKeyis present in dictionaryDict, and an exception is generated ifKeyis not in the dictionary. so if you're not sure, maybe don't. or do, and live with the consequences π
Examples
1> Dict = dict:from_list([{2,b},{1,a}]).
2> dict:fetch(1, Dict).
a
fetch_keys(Dict) -> Keysβwhen Dict :: dict(Key, Value :: term()), Keys :: [Key].
returns a list of all the keys in dictionary Dict. every single one. the full roster π
Examples
1> Dict = dict:from_list([{2,b},{1,a}]).
2> lists:sort(dict:fetch_keys(Dict)).
[1,2]
filter(Pred, Dict1) -> Dict2βwhen Pred :: fun((Key, Value) -> boolean()), Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value).
returns a dictionary of all keys and values in Dict1 for which Pred(Key, Value) is true. filtering out the ones that aren't serving, we respect it π
Examples
1> Dict0 = dict:from_list([{a,1},{b,2},{c,3}]).
2> Dict1 = dict:filter(fun(_, V) -> V rem 2 =:= 1 end, Dict0).
3> lists:sort(dict:to_list(Dict1)).
[{a,1},{c,3}]
find(Key, Dict) -> {ok, Value} | errorβwhen Dict :: dict(Key, Value).
searches for a key in dictionary Dict. the polite version of fetch, she tells you nicely instead of screaming πΈ
returns {ok, Value}, where Value is the value associated with Key, or error if the key is not present in the dictionary. no drama, just facts β¨
Examples
1> Dict = dict:from_list([{2,b},{1,a}]).
2> dict:find(1, Dict).
{ok,a}
3> dict:find(99, Dict).
error
fold(Fun, Acc0, Dict) -> Acc1βwhen Fun :: fun((Key, Value, AccIn) -> AccOut), Dict :: dict(Key, Value), Acc0 :: Acc, Acc1 :: Acc, AccIn :: Acc, AccOut :: Acc.
calls Fun on successive keys and values of dictionary Dict together with an extra argument Acc (short for accumulator β she's carrying the whole team on her back fr) π«Ά
Fun must return a new accumulator that is passed to the next call. Acc0 is returned if the dictionary is empty.
β οΈ the evaluation order is undefined. don't build your whole personality on it, bestie.
Examples
1> Dict0 = dict:from_list([{a,1},{b,2},{c,3}]).
2> dict:fold(fun(_, N, Acc) -> Acc + N end, 0, Dict0).
6
from_list(List) -> Dictβwhen Dict :: dict(Key, Value), List :: [{Key, Value}].
converts the key-value list List to a dictionary. glow-up, list edition π
Examples
1> Dict = dict:from_list([{2,b},{1,a}]).
2> lists:sort(dict:to_list(Dict)).
[{1,a},{2,b}]
is_empty(Dict) -> boolean()βwhen Dict :: dict().
returns true if dictionary Dict has no elements; otherwise, returns false. a vibe check, essentially π¦
Examples
1> Dict = dict:new().
2> dict:is_empty(Dict).
true
is_key(Key, Dict) -> boolean()βwhen Dict :: dict(Key, Value :: term()).
tests whether Key is contained in dictionary Dict. are you in the group chat or not, that's the question π¬
Examples
1> Dict = dict:from_list([{count,0}]).
2> dict:is_key(count, Dict).
true
3> dict:is_key(table, Dict).
false
map(Fun, Dict1) -> Dict2βwhen Fun :: fun((Key, Value1) -> Value2), Dict1 :: dict(Key, Value1), Dict2 :: dict(Key, Value2).
calls Fun on successive keys and values of dictionary Dict1 to return a new value for each key. makeover time, every value gets a new look β¨
β οΈ the evaluation order is undefined. same warning, same energy.
Examples
1> Dict0 = dict:from_list([{a,1},{b,2},{c,3}]).
2> Dict1 = dict:map(fun(_, V) -> 2 * V end, Dict0).
3> lists:sort(dict:to_list(Dict1)).
[{a,2},{b,4},{c,6}]
merge(Fun, Dict1, Dict2) -> Dict3βwhen Fun :: fun((Key, Value1, Value2) -> Value), Dict1 :: dict(Key, Value1), Dict2 :: dict(Key, Value2), Dict3 :: dict(Key, Value).
merges two dictionaries, Dict1 and Dict2, to create a new dictionary. it's giving collab π€
all the key-value pairs from both dictionaries are included in the new dictionary. if a key occurs in both dictionaries, Fun is called with the key and both values to return a new value.
Examples
1> Dict0 = dict:from_list([{a,1},{b,2}]).
2> Dict1 = dict:from_list([{b,7},{c,99}]).
3> Dict2 = dict:merge(fun(_, V1, V2) -> V1 + V2 end, Dict0, Dict1).
4> lists:sort(dict:to_list(Dict2)).
[{a,1},{b,9},{c,99}]
new() -> dict().
creates a new dictionary. blank slate, main character origin story π
Examples
1> dict:new().
size(Dict) -> non_neg_integer()βwhen Dict :: dict().
returns the number of elements in dictionary Dict. how many friends does she have, we count them all π’
Examples
1> Dict = dict:from_list([{2,b},{1,a}]).
2> dict:size(Dict).
2
store(Key, Value, Dict1) -> Dict2βwhen Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value).
stores a key-value pair in a dictionary. the it-girl move π
if Key already exists in Dict1, the associated value is replaced by Value. no duplicates here, we replace, we don't collect π
Examples
1> Dict0 = dict:new().
2> Dict1 = dict:store(name, arne, Dict0).
3> dict:to_list(Dict1).
[{name,arne}]
4> Dict2 = dict:store(name, kalle, Dict1).
5> dict:to_list(Dict2).
[{name,kalle}]
take(Key, Dict) -> {Value, Dict1} | errorβwhen Dict :: dict(Key, Value), Dict1 :: dict(Key, Value), Key :: term(), Value :: term().
this function returns a value from a dictionary and a new dictionary without this value. she took it with her and didn't look back π
returns error if the key is not present in the dictionary.
Examples
1> Dict0 = dict:from_list([{2,b},{1,a}]).
2> {V, Dict2} = dict:take(1, Dict0).
3> V.
a
4> dict:to_list(Dict2).
[{2,b}]
to_list(Dict) -> Listβwhen Dict :: dict(Key, Value), List :: [{Key, Value}].
converts a dictionary to a list representation. she's going from dictionary to list, a whole transformation arc π¦
Examples
1> Dict = dict:from_list([{2,b},{1,a}]).
2> lists:sort(dict:to_list(Dict)).
[{1,a},{2,b}]
update(Key, Fun, Dict1) -> Dict2βwhen Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value), Fun :: fun((Value1 :: Value) -> Value2 :: Value).
updates a value in a dictionary by calling Fun on the value to get a new value. glow-up, single value edition β¨
Examples
1> Dict0 = dict:from_list([{a,10}]).
2> Dict1 = dict:update(a, fun(N) -> N + 1 end, Dict0).
3> dict:to_list(Dict1).
[{a,11}]
π¨ an exception is generated ifKeyis not present in the dictionary. this version ofupdateis not the forgiving one, bestie.
update(Key, Fun, Initial, Dict1) -> Dict2βwhen Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value), Fun :: fun((Value1 :: Value) -> Value2 :: Value), Initial :: Value.
updates a value in a dictionary by calling Fun on the value to get a new value.
if Key is not present in the dictionary, Initial is stored as the first value. she's prepared, she has a fallback, she's iconic π
Examples
1> Dict0 = dict:new().
2> Inc = fun(N) -> N + 1 end.
3> Dict1 = dict:update(a, Inc, 0, Dict0).
4> dict:to_list(Dict1).
[{a,0}]
5> Dict2 = dict:update(a, Inc, 0, Dict1).
6> dict:to_list(Dict2).
[{a,1}]
update_counter(Key, Increment, Dict1) -> Dict2βwhen Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value), Increment :: number().
adds Increment to the value associated with Key and stores this value. number go up, we love to see it π
if Key is not present in the dictionary, Increment is stored as the first value.
Examples
1> Dict0 = dict:new().
2> Dict1 = dict:update_counter(a, 10, Dict0).
3> dict:to_list(Dict1).
[{a,10}]
4> Dict2 = dict:update_counter(a, 10, Dict1).
5> dict:to_list(Dict2).
[{a,20}]
okay and that's the whole page bestie, you ate that π«Ά this iconic lil number was brought to you by Ericsson AB, running stdlib 8.0.4, and yes it's all about dict(3) β¨ go forth and dictionary responsibly πππΈ
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms