mapx
Hash-table style key/value maps over ilusm objects - create an empty map, set a key, get with an optional default, check key presence, list keys, get length, delete a key (by rebuild), and merge two maps (right wins on collision).
Load with: use mapx
What this module does
mapx provides an explicit, validated API over ilusm's built-in
object type, treating it as a hash map. All keys are coerced to strings. The
operations add type-checking guards (via eru.bang) so that passing
a non-object produces a clear error rather than a cryptic runtime failure.
Key deletion is done by rebuilding a new object without the target key, since ilusm objects don't support in-place key removal.
Quick example
use mapx
m = mapxn() # {}
m = mapxs(m, "x", 42) # set
m = mapxs(m, "y", "hello")
prn(mapxg(m, "x", 0)) # 42
prn(mapxg(m, "missing", "default")) # "default"
prn(mapxh(m, "x")) # tru
prn(mapxk(m)) # ["x", "y"]
prn(mapxl(m)) # 2
m = mapxd(m, "x") # delete key
a = {a: 1}
b = {b: 2, a: 99}
merged = mapxm(a, b) # {a:99, b:2}
Functions
Map operations
mapxn()Creates and returns an empty map object.
mapxs(map, key, val)Returns a new map with key set to val. Key is coerced to string.
mapxg(map, key, default)Gets the value for key. Returns default[0] if the key is absent, or nil if no default is provided.
mapxh(map, key)Returns tru if the key is present in the map.
mapxk(map)Returns the list of all keys.
mapxl(map)Returns the number of keys.
mapxd(map, key)Returns a new map without key. Rebuilds the object by copying all other keys.
mapxm(a, b)Merges two maps. Keys from b overwrite matching keys from a. Returns a new merged object.
Notes
- Deletion is O(n) as it rebuilds the object. For frequent deletions over large maps consider an alternative structure.
- Requires
trlanderu.