Skip to content

Conditionally conform Chain2Sequence to MutableCollection - #283

Open
shubhransh-gupta wants to merge 1 commit into
apple:mainfrom
shubhransh-gupta:feat/chain-mutable-collection
Open

Conditionally conform Chain2Sequence to MutableCollection#283
shubhransh-gupta wants to merge 1 commit into
apple:mainfrom
shubhransh-gupta:feat/chain-mutable-collection

Conversation

@shubhransh-gupta

Copy link
Copy Markdown

Summary

Resolves #217.

Adds conditional conformance of Chain2Sequence to MutableCollection when both underlying base sequences (Base1 and Base2) conform to MutableCollection.

Motivation

When chaining two mutable collections (e.g. [1, 2, 3] and [4, 5, 6]), users expect to be able to mutate elements in-place via indices or subscripts without converting the chained sequence into an intermediate array or losing lazy execution benefits.

Changes

  1. Sources/Algorithms/Chain.swift:
    • Changed base1 and base2 in Chain2Sequence from let to var.
    • Added conditional conformance extension Chain2Sequence: MutableCollection where Base1: MutableCollection, Base2: MutableCollection.
    • Implemented subscript(i: Index) with get, set, and _modify (using yield &base1[i] / yield &base2[i] for efficient in-place mutation).
  2. Guides/Chain.md:
    • Updated documentation to list MutableCollection among the conditional conformances.
  3. Tests/SwiftAlgorithmsTests/ChainTests.swift:
    • Added testChainMutableCollection() verifying index mutations across both chained collections.

Verification

  • swift build — builds cleanly with 0 errors.

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.

Conditionally conform Chain2Sequence to MutableCollection

1 participant