dict(3)

πŸ“¦
version 29.0.6 Β· 3 versions
, in ubuntu stonking

πŸ’– 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:

  • dict considers two keys different if they do not match (=:=)
  • orddict considers 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 by new/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 that Key is present in dictionary Dict, and an exception is generated if Key is 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 if Key is not present in the dictionary. this version of update is 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