KeySelect¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
KeySelect[assoc, pred]
Keeps the entries whose key satisfies pred.
Examples (3)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (1)¶
Applications (2)¶
Keep even-numbered keys
Implementation notes¶
Algorithm. builtin_keyselect keeps the entries whose key passes a predicate.
For each entry it forms pred[key] (apply1), evaluates it, and keeps the whole
entry only when the verdict is literally the symbol True; any other result drops
the entry. The surviving entries, in their original order, form a fresh
Association.
Data structures. A single Expr** output array sized to the entry count; the
predicate application is a throwaway evaluated per key.
Complexity / limits. O(n), one predicate evaluation per entry. The companion
KeySelect tests keys; Select (and Discard) over an association test the values
instead. A non-True verdict ā including an unevaluated predicate call ā counts as
rejection.
Attributes: Protected.
References¶
- Source:
src/assoc.c - Specification:
docs/spec/builtins/data-structures.md - Tests:
tests/test_assoc_forms.c - Tests:
tests/test_association.c
Notes & additional examples¶
Notes¶
The predicate is applied to each key; an entry is kept only when the result is
exactly True. Order is preserved. To filter by the values rather than the keys, use
Select over the association.