x-lang

← Index

x/type/dict

Dict: a mutable content-hashed table – the O(1) associative container.

Buckets over a raw slot vector; content hashing (FNV-1a) + equal? keys, instances by identity (address hash + eq?); doubles past a 3/4 load factor.

Class Dict

A mutable hash table: O(1) expected get/set!/del! over content-hashed keys.

Keys may be symbols, strings, integers, or chars (hashed by content, compared with equal?), or class instances (identity keys: hashed by address, compared with same?); anything else errors.

Mutators (set!/del!) return the dict for chaining. get-or is presence-based: a stored nil is returned, not the default.

The verb rule (#358): mutating operations carry the bang (set!/del!); the persistent twins live on Assoc with bare verbs (put/del). Same data shape, opposite update models.

Every association shape has a named door: from-alist ((k . v) …), from-plist (k v k v …), from-bindings ((k v) …) – and ->alist/->plist/->bindings back out.

store

Member: data carried by a Dict instance.

cap

Member: data carried by a Dict instance.

n

Member: data carried by a Dict instance.

(Dict dict? x)

Test whether a value is a Dict.

Parameters:

Returns: BOOL — #t when x is a Dict instance

(Dict make . opt)

An empty dict. Pass a capacity to pre-size the bucket table.

Parameters:

Returns: Dict — A new empty dict

Examples:

((Dict make) empty?) => #t

(Dict from-plist plist)

Build a dict from a flat plist – the simplest literal shape.

Parameters:

Returns: Dict — A dict of the plist’s pairs

Examples:

((Dict from-plist (list 'a 1 'b 2)) get 'b) => 2

(Dict from-bindings bindings)

Build a dict from a bindings list.

Parameters:

Returns: Dict — A dict of the bindings

Examples:

((Dict from-bindings (list (list 'a 1))) get 'a) => 1

(Dict from-alist alist)

Build a dict from an alist (later duplicates overwrite earlier).

Parameters:

Returns: Dict — A dict holding the alist’s assocs

Examples:

((Dict from-alist (list (pair "a" 1))) get "a") => 1

(%grow!)

Instance method: called on a Dict instance.

(get k)

The value stored under a key, or nil when absent.

Instance method: called on a Dict instance.

Parameters:

Returns: ANY — Stored value, or nil

Examples:

(let ((d (Dict make))) (d set! 'a 1) (d get 'a)) => 1

(get-or d k)

The value stored under a key, or a default only when the key is ABSENT.

Instance method: called on a Dict instance.

Parameters:

Returns: ANY — Stored value (a stored nil included), or the default

(get-or-else thunk k)

The value stored under a key, or (thunk) only when the key is ABSENT – get-or’s lazy twin (mirrors Assoc opt-get-or-else).

Instance method: called on a Dict instance.

Block form: (get-or-else () body … k) – the body is the default, run only on a miss.

Parameters:

Returns: ANY — Stored value (a stored nil included), or (thunk)

(has? k)

Test whether a key is present.

Instance method: called on a Dict instance.

Parameters:

Returns: BOOL — #t when the key is stored

(set! k v)

Store a value under a key (overwriting any previous value); returns the dict for chaining.

Instance method: called on a Dict instance.

Parameters:

Returns: Dict — self

Examples:

(((Dict make) set! 'a 1) get 'a) => 1

(del! k)

Remove a key (a no-op when absent); returns the dict for chaining.

Instance method: called on a Dict instance.

Parameters:

Returns: Dict — self

(length)

The number of stored entries (a stored property, O(1)).

Instance method: called on a Dict instance.

Returns: INT — Entry count

(empty?)

Test whether the dict holds no entries.

Instance method: called on a Dict instance.

Returns: BOOL — #t when empty

(->alist)

A fresh alist snapshot of the entries (unordered).

Instance method: called on a Dict instance.

Returns: LIST — ((key . val) …) – new assocs, detached from the table

(->plist)

The entries as a flat (k v k v …) plist snapshot (unordered).

Instance method: called on a Dict instance.

Returns: LIST — Plist of the entries

(->bindings)

The entries as a bindings list ((key value) …) snapshot (unordered).

Instance method: called on a Dict instance.

Returns: LIST — Bindings list of the entries

(keys)

All stored keys (unordered).

Instance method: called on a Dict instance.

Returns: LIST — List of keys

(vals)

All stored values (unordered).

Instance method: called on a Dict instance.

Returns: LIST — List of values

(for-each f)

Apply f to each (key . val) entry pair, for side effects.

Instance method: called on a Dict instance.

Block form: (for-each (p) body …) – or (k v) for the key and the value.

Parameters:

Returns: ANY — nil

(map f)

A new dict with the same keys, each value replaced by (f (key . val)).

The callback receives the ENTRY, as for-each does, so the key is available; the RESULT is the value. To rewrite keys, map the ->alist and rebuild with from-alist.

Instance method: called on a Dict instance.

Block form: (map (p) body …) – or (k v) for the key and the value.

Parameters:

Returns: Dict — A fresh dict: this dict’s keys, mapped values

Examples:

(((Dict from-plist (list 'a 1 'b 2)) map (fn (_ e) (* (rest e) 10))) get 'b) => 20