Associative Lists / Dictionaries

From MemCP
Revision as of 11:59, 28 August 2026 by Wikiservice (talk | contribs) (Refresh MemCP documentation: accuracy, operational guidance, performance profile and maintained API reference)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Associative Lists / Dictionaries

Generated from MemCP commit c42e19eba on 27 August 2026. See Full SCM API documentation.

The Associative Lists / Dictionaries module works with functional key/value collections. It includes:

  • lookup and presence checks;
  • extraction, filtering, and mapping by key and value;
  • immutable updates and merges;
  • reductions and sorting;
  • structural indexes and catalogs for repeated lookups.

Ordinary operations return a new value instead of mutating an outer binding. This makes associative data safe to share through functional query code; use explicit sessions or synchronization primitives when mutable shared state is required.

filter_assoc

returns a filtered dictionary according to a filter function

Allowed number of parameters: 2–2

Parameters

  • dict (list): dictionary that has to be filtered
  • condition (func): returns whether a dictionary entry should be included
    • Parameters
      • key (string): entry key
      • value (any): entry value
    • Returns
      • included (bool): whether to include the entry

Returns

  • value (list)

find_assoc

returns the first key/value pair that passes the condition function, or nil/default if none matches

Allowed number of parameters: 2–3

Parameters

  • dict (list): dictionary to search
  • condition (func): predicate applied until the first matching dictionary entry
    • Parameters
      • key (string): entry key
      • value (any): entry value
    • Returns
      • matches (bool): whether the entry matches
  • default (any): optional default value if nothing matches (optional)

Returns

  • value (any)

map_assoc

returns a mapped dictionary according to a map function Keys will stay the same but values are mapped.

Allowed number of parameters: 2–2

Parameters

  • dict (list): dictionary that has to be mapped
  • map (func): transforms each dictionary value
    • Parameters
      • key (string): entry key
      • value (any): entry value
    • Returns
      • mapped_value (any): replacement value

Returns

  • value (list)

reduce_assoc

reduces a dictionary according to a reduce function

Allowed number of parameters: 3–3

Parameters

  • dict (list): dictionary that has to be reduced
  • reduce (func): combines the accumulator with each dictionary entry
    • Parameters
      • acc (any): current accumulator
      • key (string): entry key
      • value (any): entry value
    • Returns
      • acc (any): next accumulator
  • neutral (any): initial value for the accumulator

Returns

  • value (any)

make_structural_index

Builds an immutable structural-expression index. It eagerly hashes every key and every node under roots, then returns a parallel-safe lookup function that maps an equal expression to its zero-based key position or nil.

Allowed number of parameters: 2–2

Parameters

  • keys (list): immutable structural expressions to index
  • roots (list): immutable expression roots whose descendant hashes are precomputed

Returns

  • lookup (func): looks up the indexed position of a structurally equal expression
    • Parameters
      • expression (any): a key, root, descendant of a declared root, or scalar expression
    • Returns
      • position (int|nil): zero-based key position, or nil when the expression is not indexed

make_structural_catalog

Creates an atomic compile-local structural catalog. Look up with (catalog key), insert with (catalog key value), or freeze with (catalog) for parallel-safe read-only lookup.

Allowed number of parameters: 0–1

Parameters

  • mode (bool|symbol): true forces collisions for tests; ast selects type-stable compiler equality (optional)

Returns

  • catalog (func): atomic structural-expression lookup and update function
    • Parameters
      • key (any): expression to look up; omit to freeze the catalog (optional)
      • value (any): value to store for key (optional)
    • Returns
      • result (any|func): stored value, lookup result, or frozen lookup function
        • Parameters
          • key (any): expression to look up in the frozen catalog
        • Returns
          • value (any): value stored for a structurally equal expression, or nil

has_assoc?

checks if a dictionary has a key present

Allowed number of parameters: 2–2

Parameters

  • dict (list): dictionary that has to be checked
  • key (string): key to test

Returns

  • value (bool)

get_assoc

gets a value from a dictionary by key, returns nil if not found

Allowed number of parameters: 2–3

Parameters

  • dict (list): dictionary to look up
  • key (any): key to look up
  • default (any): optional default value if key not found (optional)

Returns

  • value (any)

get_assoc_pairlist

gets a value from a list of key/value rows without flattening the rows

Allowed number of parameters: 3–3

Parameters

  • rows (list): list whose rows contain a key followed by one or more values
  • key (any): key compared with the first item of each row
  • default (any): value returned when no row contains the key

Returns

  • value (any)

extract_assoc

applies a function (key value) on the dictionary and returns the results as a flat list

Allowed number of parameters: 2–2

Parameters

  • dict (list): dictionary that has to be checked
  • map (func): extracts one element per dictionary entry
    • Parameters
      • key (string): entry key
      • value (any): entry value
    • Returns
      • element (any): element extracted from the entry

Returns

  • value (list)

set_assoc

returns a new dictionary where a single value has been changed. The original dictionary is not modified.

Allowed number of parameters: 3–4

Parameters

  • dict (list): input dictionary
  • key (string): key that has to be set
  • value (any): new value to set
  • merge (func): combines values when an existing entry is overwritten (optional)
    • Parameters
      • old (any): existing value
      • new (any): replacement value
    • Returns
      • merged (any): value stored in the new dictionary

Returns

  • value (list)

merge_assoc

returns a dictionary where all keys from dict1 and all keys from dict2 are present. If a key is present in both inputs, the second one will be dominant so the first value will be overwritten unless you provide a merge function

Allowed number of parameters: 2–3

Parameters

  • dict1 (list): first input dictionary that has to be changed. You must not use this value again.
  • dict2 (list): input dictionary that contains the new values that have to be added
  • merge (func): combines values when both dictionaries contain an entry (optional)
    • Parameters
      • old (any): value from the first dictionary
      • new (any): value from the second dictionary
    • Returns
      • merged (any): value stored in the merged dictionary

Returns

  • value (list)

sort

returns a sorted copy of a list using a comparator (lambda (a b) truthy/falsy)

Allowed number of parameters: 2–2

Parameters

  • list (list)
  • comparator (func)
    • Parameters
      • parameter (any)
      • parameter (any)
    • Returns
      • value (bool)

Returns

  • value (list)

sort_mut

sorts a list in-place using a comparator (lambda (a b) truthy/falsy)

Allowed number of parameters: 2–2

Parameters

  • list (list)
  • comparator (func)
    • Parameters
      • parameter (any)
      • parameter (any)
    • Returns
      • value (bool)

Returns

  • value (list)

mapkey_assoc

returns a mapped dictionary according to a map function Values stay the same but keys are mapped.

Allowed number of parameters: 2–2

Parameters

  • dict (list): dictionary whose keys have to be mapped
  • map (func): computes a replacement key for each dictionary entry
    • Parameters
      • key (string): existing key
      • value (any): entry value
    • Returns
      • new_key (any): replacement key

Returns

  • value (list)