.TH dict 3erl "stdlib 1.18.1" "Ericsson AB" "Erlang Module Definition" .SH NAME dict \- Key-Value Dictionary .SH DESCRIPTION .LP \fIDict\fR\& implements a \fIKey\fR\& - \fIValue\fR\& dictionary\&. The representation of a dictionary is not defined\&. .LP This module provides exactly the same interface as the module \fIorddict\fR\&\&. One difference is that while this module considers two keys as different if they do not match (\fI=:=\fR\&), \fIorddict\fR\& considers two keys as different if and only if they do not compare equal (\fI==\fR\&)\&. .SH DATA TYPES .nf .B \fBdict()\fR\& .br .fi .RS .LP Dictionary as returned by \fInew/0\fR\&\&. .RE .SH EXPORTS .LP .nf .B append(Key, Value, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Key = Value = term() .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP This function appends a new \fIValue\fR\& to the current list of values associated with \fIKey\fR\&\&. .RE .LP .nf .B append_list(Key, ValList, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Key = term() .br ValList = [Value :: term()] .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP This function appends a list of values \fIValList\fR\& to the current list of values associated with \fIKey\fR\&\&. An exception is generated if the initial value associated with \fIKey\fR\& is not a list of values\&. .RE .LP .nf .B erase(Key, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Key = term() .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP This function erases all items with a given key from a dictionary\&. .RE .LP .nf .B fetch(Key, Dict) -> Value .br .fi .br .RS .LP Types: .RS 3 Key = term() .br Dict = dict() .br Value = term() .br .RE .RE .RS .LP This function returns the value associated with \fIKey\fR\& in the dictionary \fIDict\fR\&\&. \fIfetch\fR\& assumes that the \fIKey\fR\& is present in the dictionary and an exception is generated if \fIKey\fR\& is not in the dictionary\&. .RE .LP .nf .B fetch_keys(Dict) -> Keys .br .fi .br .RS .LP Types: .RS 3 Dict = dict() .br Keys = [term()] .br .RE .RE .RS .LP This function returns a list of all keys in the dictionary\&. .RE .LP .nf .B filter(Pred, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Pred = fun((Key :: term(), Value :: term()) -> boolean()) .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP \fIDict2\fR\& is a dictionary of all keys and values in \fIDict1\fR\& for which \fIPred(Key, Value)\fR\& is \fItrue\fR\&\&. .RE .LP .nf .B find(Key, Dict) -> {ok, Value} | error .br .fi .br .RS .LP Types: .RS 3 Key = term() .br Dict = dict() .br Value = term() .br .RE .RE .RS .LP This function searches for a key in a dictionary\&. Returns \fI{ok, Value}\fR\& where \fIValue\fR\& is the value associated with \fIKey\fR\&, or \fIerror\fR\& if the key is not present in the dictionary\&. .RE .LP .nf .B fold(Fun, Acc0, Dict) -> Acc1 .br .fi .br .RS .LP Types: .RS 3 Fun = fun((Key, Value, AccIn) -> AccOut) .br Key = Value = Acc0 = Acc1 = AccIn = AccOut = term() .br Dict = dict() .br .RE .RE .RS .LP Calls \fIFun\fR\& on successive keys and values of \fIDict\fR\& together with an extra argument \fIAcc\fR\& (short for accumulator)\&. \fIFun\fR\& must return a new accumulator which is passed to the next call\&. \fIAcc0\fR\& is returned if the list is empty\&. The evaluation order is undefined\&. .RE .LP .nf .B from_list(List) -> Dict .br .fi .br .RS .LP Types: .RS 3 List = [{Key :: term(), Value :: term()}] .br Dict = dict() .br .RE .RE .RS .LP This function converts the \fIKey\fR\& - \fIValue\fR\& list \fIList\fR\& to a dictionary\&. .RE .LP .nf .B is_key(Key, Dict) -> boolean() .br .fi .br .RS .LP Types: .RS 3 Key = term() .br Dict = dict() .br .RE .RE .RS .LP This function tests if \fIKey\fR\& is contained in the dictionary \fIDict\fR\&\&. .RE .LP .nf .B map(Fun, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Fun = .br fun((Key :: term(), Value1 :: term()) -> Value2 :: term()) .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP \fImap\fR\& calls \fIFun\fR\& on successive keys and values of \fIDict1\fR\& to return a new value for each key\&. The evaluation order is undefined\&. .RE .LP .nf .B merge(Fun, Dict1, Dict2) -> Dict3 .br .fi .br .RS .LP Types: .RS 3 Fun = .br fun((Key :: term(), Value1 :: term(), Value2 :: term()) -> .br Value :: term()) .br Dict1 = Dict2 = Dict3 = dict() .br .RE .RE .RS .LP \fImerge\fR\& merges two dictionaries, \fIDict1\fR\& and \fIDict2\fR\&, to create a new dictionary\&. All the \fIKey\fR\& - \fIValue\fR\& pairs from both dictionaries are included in the new dictionary\&. If a key occurs in both dictionaries then \fIFun\fR\& is called with the key and both values to return a new value\&. \fImerge\fR\& could be defined as: .LP .nf merge(Fun, D1, D2) -> fold(fun (K, V1, D) -> update(K, fun (V2) -> Fun(K, V1, V2) end, V1, D) end, D2, D1). .fi .LP but is faster\&. .RE .LP .nf .B new() -> dict() .br .fi .br .RS .LP This function creates a new dictionary\&. .RE .LP .nf .B size(Dict) -> integer() >= 0 .br .fi .br .RS .LP Types: .RS 3 Dict = dict() .br .RE .RE .RS .LP Returns the number of elements in a \fIDict\fR\&\&. .RE .LP .nf .B store(Key, Value, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Key = Value = term() .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP This function stores a \fIKey\fR\& - \fIValue\fR\& pair in a dictionary\&. If the \fIKey\fR\& already exists in \fIDict1\fR\&, the associated value is replaced by \fIValue\fR\&\&. .RE .LP .nf .B to_list(Dict) -> List .br .fi .br .RS .LP Types: .RS 3 Dict = dict() .br List = [{Key :: term(), Value :: term()}] .br .RE .RE .RS .LP This function converts the dictionary to a list representation\&. .RE .LP .nf .B update(Key, Fun, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Key = term() .br Fun = fun((Value1 :: term()) -> Value2 :: term()) .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP Update a value in a dictionary by calling \fIFun\fR\& on the value to get a new value\&. An exception is generated if \fIKey\fR\& is not present in the dictionary\&. .RE .LP .nf .B update(Key, Fun, Initial, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Key = Initial = term() .br Fun = fun((Value1 :: term()) -> Value2 :: term()) .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP Update a value in a dictionary by calling \fIFun\fR\& on the value to get a new value\&. If \fIKey\fR\& is not present in the dictionary then \fIInitial\fR\& will be stored as the first value\&. For example \fIappend/3\fR\& could be defined as: .LP .nf append(Key, Val, D) -> update(Key, fun (Old) -> Old ++ [Val] end, [Val], D). .fi .RE .LP .nf .B update_counter(Key, Increment, Dict1) -> Dict2 .br .fi .br .RS .LP Types: .RS 3 Key = term() .br Increment = number() .br Dict1 = Dict2 = dict() .br .RE .RE .RS .LP Add \fIIncrement\fR\& to the value associated with \fIKey\fR\& and store this value\&. If \fIKey\fR\& is not present in the dictionary then \fIIncrement\fR\& will be stored as the first value\&. .LP This could be defined as: .LP .nf update_counter(Key, Incr, D) -> update(Key, fun (Old) -> Old + Incr end, Incr, D). .fi .LP but is faster\&. .RE .SH "NOTES" .LP The functions \fIappend\fR\& and \fIappend_list\fR\& are included so we can store keyed values in a list \fIaccumulator\fR\&\&. For example: .LP .nf > 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] .fi .LP This saves the trouble of first fetching a keyed value, appending a new value to the list of stored values, and storing the result\&. .LP The function \fIfetch\fR\& should be used if the key is known to be in the dictionary, otherwise \fIfind\fR\&\&. .SH "SEE ALSO" .LP \fBgb_trees(3erl)\fR\&, \fBorddict(3erl)\fR\&