5.4.1.2 Predefined functions on dicts
Dicts currently define the following reserved functions:
- get(?KeyPath)
- Return the value associates with KeyPath. KeyPath
is either a single key or a term
Key1/Key2/...
. Each key is either an atom, small integer or a variable. WhileDict.Key
throws an existence error, this function fails silently if a key does not exist in the target dict. See also :</2, which can be used to test for existence and unify multiple key values from a dict. For example:?- write(t{a:x}.get(a)). x ?- write(t{a:x}.get(b)). false. ?- write(t{a:t{b:x}}.get(a/b)). x
- put(+New)
- Evaluates to a new dict where the key-values in New replace or extend the key-values in the original dict. See put_dict/3.
- get(?KeyPath, +Default)
- Same as get/1 , but if no match is found the function evaluates to Default. If KeyPath contains variables possible choice points are respected and the function only evaluates to Default if the pattern has no matches.
- put(+KeyPath, +Value)
- Evaluates to a new dict where the KeyPath-Value
replaces or extends the key-values in the original dict. KeyPath
is either a key or a term KeyPath/Key,163Note
that we do not use the’.' functor here, because the
would evaluate. replacing the value associated with Key in a sub-dict of the dict on which the function operates. See put_dict/4. Below are some examples:.
/2?- A = _{}.put(a, 1). A = _G7359{a:1}. ?- A = _{a:1}.put(a, 2). A = _G7377{a:2}. ?- A = _{a:1}.put(b/c, 2). A = _G1395{a:1, b:_G1584{c:2}}. ?- A = _{a:_{b:1}}.put(a/b, 2). A = _G1429{a:_G1425{b:2}}. ?- A = _{a:1}.put(a/b, 2). A = _G1395{a:_G1578{b:2}}.