Dict Library Reference
The dict
library provides a full set of functions for working with key–value maps in EasyBite. You can call these functions directly on dictionary values using method syntax (e.g. d.add(key, value)
) or import the dict
module and invoke them as dict.add(d, key, value)
. You may also import specific functions with from dict import add, get, copy, tojson, tofile
.
Available Functions
Function | Parameters | Description |
---|---|---|
add(dict, key, value) | dict (Dictionary), key (any), value (any) | Adds or updates key with value in dict , returns the modified dictionary. |
get(dict, key) | dict (Dictionary), key (any) | Returns the value for key , or error if not found. |
remove(dict, key) | dict (Dictionary), key (any) | Removes key (and its value) from dict , returns the modified dictionary. |
containskey(dict, key) | dict (Dictionary), key (any) | Returns true if key exists in dict , otherwise false . |
containsvalue(dict, value) | dict (Dictionary), value (any) | Returns true if any entry in dict has value , otherwise false . |
size(dict) | dict (Dictionary) | Returns the number of entries in dict . |
keys(dict) | dict (Dictionary) | Returns an array of all keys in dict . |
values(dict) | dict (Dictionary) | Returns an array of all values in dict . |
isempty(dict) | dict (Dictionary) | Returns true if dict has no entries, otherwise false . |
clear(dict) | dict (Dictionary) | Removes all entries from dict , returns the now-empty dictionary. |
update(dict, key, value) | dict (Dictionary), key (any), value (any) | Same as add , inserts or updates the key with value . |
merge(dict, otherDict) | dict , otherDict (Dictionaries) | Inserts all entries from otherDict into dict , overwriting duplicates, returns dict . |
copy(dict) | dict (Dictionary) | Returns a shallow copy of dict . |
tojson(dict) | dict (Dictionary) | Returns a JSON string representation of dict . |
tofile(dict, filename) | dict (Dictionary), filename (String) | Serializes dict to JSON and writes it to the given file; returns null . |
pop(dict, key) | dict (Dictionary), key (any) | Removes and returns the value for key ; error if key not found. |
popitem(dict) | dict (Dictionary) | Removes and returns the first [key, value] pair as an array; error if empty. |
items(dict) | dict (Dictionary) | Returns an array of [key, value] pairs for all entries in dict . |
setdefault(dict, key, default) | dict (Dictionary), key (any), default (any) | Returns dict[key] if present; otherwise sets dict[key]=default and returns default . |
Examples
1. add(dict, key, value)
set d to {}
set d2 to d.add("x", 10)
show(d2)
// Output:
// {"x":10}
import dict
set d3 to dict.add(d, "y", 20)
show(d3)
// Output:
// {"x":10,"y":20}
from dict import add
set d4 to add(d, "z", 30)
show(d4)
// Output:
// {"x":10,"y":20,"z":30}
2. get(dict, key)
set val to d.get("x")
show(val)
// Output:
// 10
import dict
set val2 to dict.get(d, "y")
show(val2)
// Output:
// 20
from dict import get
set val3 to get(d, "z")
show(val3)
// Output:
// 30
3. remove(dict, key)
set d5 to d.remove("x")
show(d5)
// Output:
// {"y":20,"z":30}
import dict
set d6 to dict.remove(d5, "y")
show(d6)
// Output:
// {"z":30}
from dict import remove
set d7 to remove(d6, "z")
show(d7)
// Output:
// {}
4. containskey(dict, key)
and containsvalue(dict, value)
set hasX to d.containskey("x")
show(hasX)
// Output:
// false
set has30 to d.containsvalue(30)
show(has30)
// Output:
// true
import dict
set ck to dict.containskey(d, "z")
set cv to dict.containsvalue(d, 30)
show(ck, cv)
// Output:
// false true
from dict import containskey, containsvalue
show(containskey(d, "z"), containsvalue(d, 30))
// Output:
// false true
5. size(dict)
and isempty(dict)
set s to d.size()
show(s)
// Output:
// 1
set emptyCheck to d.isempty()
show(emptyCheck)
// Output:
// false
import dict
set s2 to dict.size(d)
set e2 to dict.isempty(d)
show(s2, e2)
// Output:
// 1 false
from dict import size, isempty
show(size(d), isempty(d))
// Output:
// 1 false
6. keys(dict)
and values(dict)
set ks to d.keys()
set vs to d.values()
show(ks)
// Output:
// ["z"]
show(vs)
// Output:
// [30]
import dict
set k2 to dict.keys(d)
set v2 to dict.values(d)
show(k2, v2)
// Output:
// ["z"] [30]
from dict import keys, values
show(keys(d), values(d))
// Output:
// ["z"] [30]
7. clear(dict)
set cleared to d.clear()
show(cleared)
// Output:
// {}
import dict
set c2 to dict.clear(d)
show(c2)
// Output:
// {}
from dict import clear
show(clear(d))
// Output:
// {}
8. update(dict, key, value)
(alias of add
)
set d8 to d.update("a", 100)
show(d8)
// Output:
// {"a":100}
import dict
set u2 to dict.update(d, "b", 200)
show(u2)
// Output:
// {"a":100,"b":200}
from dict import update
show(update(d, "c", 300))
// Output:
// {"a":100,"b":200,"c":300}
9. merge(dict, otherDict)
set a to {"a":1}
set b to {"b":2}
set merged to a.merge(b)
show(merged)
// Output:
// {"a":1,"b":2}
import dict
set m2 to dict.merge(a, b)
show(m2)
// Output:
// {"a":1,"b":2}
from dict import merge
show(merge(a, b))
// Output:
// {"a":1,"b":2}
10. copy(dict)
set original to {"k":100}
set copyDict to original.copy()
show(copyDict)
// Output:
// {"k":100}
import dict
set c3 to dict.copy(original)
show(c3)
// Output:
// {"k":100}
from dict import copy
show(copy(original))
// Output:
// {"k":100}
11. tojson(dict)
and tofile(dict, filename)
set jsonStr to d.tojson()
show(jsonStr)
// Output:
// {"a":100,"b":200,"c":300}
import dict
set json2 to dict.tojson(d)
show(json2)
// Output:
// {"a":100,"b":200,"c":300}
from dict import tojson, tofile
set j3 to tojson(d)
show(j3)
// Output:
// {"a":100,"b":200,"c":300}
tofile(d, "out.json")
// Writes JSON to "out.json", returns null
12. pop(dict, key)
set popped to d.pop("c")
show(popped)
// Output:
// 300
import dict
set p2 to dict.pop(d, "b")
show(p2)
// Output:
// 200
from dict import pop
show(pop(d, "a"))
// Output:
// 100
13. popitem(dict)
and items(dict)
set d9 to {"x":1,"y":2}
set pair to d9.popitem()
show(pair)
// Output (example ordering):
// ["x",1]
set allItems to d9.items()
show(allItems)
// Output:
// [["y",2]]
import dict
show(dict.popitem(d9), dict.items(d9))
from dict import popitem, items
show(popitem(d9), items(d9))
14. setdefault(dict, key, default)
set v1 to d.setdefault("y", 999)
show(v1)
// Output:
// 999
show(d)
// Output:
// {"y":999}
import dict
show(dict.setdefault(d, "y", 888))
// Output:
// 999
from dict import setdefault
show(setdefault(d, "new", 111))
// Output:
// 111
show(d)
// Output:
// {"y":999,"new":111}
Conclusion
The dict
library in EasyBite gives you powerful tools for map manipulation:
- Method syntax:
d.add(key, value)
- Module syntax:
import dict
→dict.add(d, key, value)
- Selective import:
from dict import add, get, copy, tojson, tofile
→add(d, k, v)
Use the style that keeps your code clear and concise for your project’s needs.