x-lang

← Index

x/type/assoc

Association list operations, homed on the Assoc class.

An assoc is one dotted (key . val) pair; an alist is a list of assocs. Keys compared with eq?.

The get/has?/del/put/keys bootstrap globals remain in x/core/alist (the object

system runs on them); these methods delegate to that layer.

Class Assoc

Persistent association lists: every operation returns a NEW alist, the input untouched.

The verb rule (#358): persistent operations carry bare verbs (put/del) because nothing mutates; the mutating twins live on Dict with the bang (set!/del!). Same data shape, opposite update models – pick by whether callers share the structure.

(Assoc get key alist)

Look up a key in an alist, returning its value or nil.

Parameters:

Returns: ANY — Value associated with key, or nil if not found

(Assoc get-or d key alist)

Look up a key in an alist, returning a default only when the key is absent.

Presence-based: a stored nil is returned as-is, not replaced by the default.

Delegates to the option-store walker, so a flat plist is also accepted.

Parameters:

Returns: ANY — Value associated with key, or the default

(Assoc has? key alist)

Test whether a key exists in an alist.

Parameters:

Returns: BOOL — True if key is present

(Assoc del key alist)

Remove all entries for a key from an alist.

Parameters:

Returns: LIST — Alist without the given key

(Assoc put key val alist)

Set a key-value pair, replacing any existing entry for that key.

Parameters:

Returns: LIST — Alist with the key set to val

(Assoc keys alist)

Return all keys from an alist.

Parameters:

Returns: LIST — List of keys

(Assoc vals alist)

Return all values from an alist.

Parameters:

Returns: LIST — List of values

(Assoc map f alist)

Apply a function to every value in an alist, preserving keys.

Block form: (Assoc map (x) body … alist) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: LIST — New alist with transformed values

(Assoc filter pred alist)

Keep only assocs satisfying a predicate.

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

Parameters:

Returns: LIST — Filtered alist

(Assoc merge a b)

Merge two alists; keys in the first take priority.

Parameters:

Returns: LIST — Merged alist; entries in a shadow those in b

(Assoc pick keys alist)

Select entries whose keys appear in a given list.

Parameters:

Returns: LIST — Alist containing only the selected keys

(Assoc omit keys alist)

Remove entries whose keys appear in a given list.

Parameters:

Returns: LIST — Alist without the excluded keys

(Assoc from-bindings bindings)

Convert a bindings list into an alist of (key . val) assocs.

Parameters:

Returns: LIST — Association list

(Assoc ->bindings alist)

Convert an alist into a bindings list of (key value) two-element lists.

Parameters:

Returns: LIST — Bindings list

(Assoc from-plist plist)

Convert a flat plist into an alist of assocs.

Parameters:

Returns: LIST — Association list

Examples:

(Assoc from-plist (list 'a 1)) => (('a . 1))

(Assoc ->plist alist)

Convert an alist into a flat (k v k v …) plist.

Parameters:

Returns: LIST — Plist

(Assoc evolve fns alist)

Apply per-key transform functions to values in an alist.

Parameters:

Returns: LIST — Alist with selected values transformed

(Assoc entry key alist)

The (key . value) entry itself, by identity (eq?), or nil – the presence-unambiguous entry door (a hit is always a pair); get is the value door. Was (List assq), #357.

Parameters:

Returns: ANY — The matching entry pair, or nil

Examples:

(Assoc entry 'b (list (pair 'a 1) (pair 'b 2))) => ('b . 2)

(Assoc find key alist)

The (key . value) entry itself, by equality (equal?), or nil. String and number keys hit here and miss the eq?-keyed doors (get/entry). Was (List assoc), #357.

Parameters:

Returns: ANY — The matching entry pair, or nil

Examples:

(Assoc find "b" (list (pair "a" 1) (pair "b" 2))) => ("b" . 2)

(Assoc opt-get-or d key store)

Look up a key in an option store (alist or plist); return a default if absent.

Parameters:

Returns: ANY — Stored value, or the default

Examples:

(Assoc opt-get-or 0 'b '(a 1)) => 0

(Assoc opt-get-or-else thunk key store)

Like opt-get-or but the default is lazy: thunk runs only when the key is absent.

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

Parameters:

Returns: ANY — Stored value, or (thunk) when the key is absent