Skip to content

Add from-array constructors for all collection types#7

Merged
hellerve merged 1 commit into
mainfrom
claude/from-array
Jun 9, 2026
Merged

Add from-array constructors for all collection types#7
hellerve merged 1 commit into
mainfrom
claude/from-array

Conversation

@carpentry-agent

Copy link
Copy Markdown

Summary

All collection types had to-array but no from-array. This adds from-array constructors to all 10 collection types, enabling convenient construction from existing array data.

Value-based collections (take (Ref (Array T))):

  • List — iterates the array in reverse to preserve element order with prepend
  • Queue — uses Array.reduce with enqueue
  • Deque — uses Array.reduce with push-back
  • OrdSet — uses Array.reduce with insert
  • HashSet — uses Array.reduce with insert
  • Heap — uses Array.reduce with insert
  • Vector — uses Array.reduce with push-back

Pair-based collections (take (Ref (Array (Pair K V)))):

  • Trie — uses Array.reduce with insert
  • OrdMap — uses Array.reduce with insert
  • HashMap — uses Array.reduce with insert

Each from-array includes two tests: a round-trip or correctness check, and an empty-array edge case.

Note: the task mentioned Stack but no define-stack macro exists in the codebase, so it was skipped.


Opened by the carpentry-org heartbeat agent (Claude). Veit has not reviewed this yet.

@carpentry-reviewer carpentry-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build & Tests

  • Build: pass (local aarch64 + CI on macOS/Ubuntu). Pre-existing compiler warnings about prn/delete for Rc types (not new).
  • Tests: pass for 8/10 collection types. All new from-array tests green:
    • List (27/27), Queue (25/25), Deque (23/23), Trie (26/26), Vector (20/20), Heap (15/15), OrdMap (34/34), OrdSet (22/22)
    • HashMap and HashSet crash with a pre-existing null-pointer dereference in Lambda dispatch (verified: same crash on main without this PR's changes). Not caused by this PR.

Findings

None. Examined all 10 from-array implementations in detail:

  1. List — manual reverse iteration with prepend is the correct approach. Array.reduce with prepend would reverse the order; iterating end-to-start and prepending preserves it. The i = (- (Array.length arr-ref) 1) initializer handles the empty-array case correctly (i = -1, loop body skipped).

  2. Value-based collections (Queue, Deque, Vector, OrdSet, HashSet, Heap) — all use Array.reduce with the collection's insert/enqueue/push-back, copying each element with @x. Verified each function's signature: all take value by-value and collection by-ref. Types match.

  3. Pair-based collections (Trie, OrdMap, HashMap) — all use Array.reduce, copying key and value from each pair. Trie's insert takes (Ref (Array %key-part-type)) for the key (by ref) and %value-type by value — the from-array correctly passes (Pair.a p) (a ref) and @(Pair.b p) (a copy). OrdMap and HashMap both take key and value by value, so @(Pair.a p) and @(Pair.b p) are correct.

  4. Type signatures — all from-array signatures correctly match their collection's element/pair types and return %name. Trie's from-array correctly requires (Ref (Array (Pair (Array %key-part-type) %value-type)) q).

  5. Tests — each collection has a correctness/round-trip test and an empty-array edge case. The round-trip tests (from-array then to-array) verify both order preservation and element integrity.

  6. Note on HashMap/HashSet: the PR adds from-array to these types and includes tests, but the tests can't be verified locally due to the pre-existing crash. CI passes, so these tests run successfully on the CI machines (likely different UBSan configuration or compiler version).

Verdict: merge

All implementations are type-correct, order-preserving, and handle edge cases. CI is green on both platforms. The HashMap/HashSet crashes are pre-existing and unrelated.

@hellerve hellerve merged commit 7b49783 into main Jun 9, 2026
2 checks passed
@hellerve hellerve deleted the claude/from-array branch June 9, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant