Skip to content

Add AdjacentPairs #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed

Add AdjacentPairs #12

wants to merge 5 commits into from

Conversation

christopherweems
Copy link

@christopherweems christopherweems commented Oct 10, 2020

Description

Adds a LazyAdjacentPairs type, a lazy collection iterating over the adjacent elements of a base collection, and an accessor property on Collection and LazyCollectionProtocol. Each AdjacentPair is a tuple defined as (leading: Element, trailing: Element). The first pair is the first two elements of the base collection, the second pair is the second and third elements, and so forth.

Detailed Design

Include any additional information about the design here. At minimum, show any new API:

extension LazyCollectionProtocol {
    var adjacentPairs: LazyAdjacentPairs<Self>
}

extension Collection {
    typealias AdjacentPair = (leading: Element, trailing: Element)
    var adjacentPairs: [AdjacentPair]
}

Documentation Plan

How has the new feature been documented? Have the relevant portions of the guides been updated in addition to symbol-level documentation?

A usage guide exists in Guides/AdjacentPairs.md.

Test Plan

Tests have been added in Tests/SwiftAlgorithmsTests/AdjacentPairsTests.swift.

Source Impact

This is a purely additive change.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary README.md remains unchanged

@timvermeulen
Copy link
Contributor

For reference, this has previously been proposed for inclusion in the standard library (pitch, Swift Evolution, Swift Evolution Staging).

@christopherweems
Copy link
Author

christopherweems commented Oct 12, 2020

Thank you Tim, I had missed some of the previous attempts of getting this thing going. I noticed some of the previous proposals used .adjacentPairs() as a function call, and not a property as in this PR.

I'm concerned with the non-lazy implementation unnecessarily using twice the memory of the base collection. It's not something I have tested for, but seems that an array of tuples of overlapping elements would wastefully duplicate members of the base collection. I'm considering a rewrite to return a different collection that does not duplicate elements in storage.

@christopherweems
Copy link
Author

christopherweems commented Oct 31, 2020

This functionality has been covered by more general .slidingWindows() algorithm b28d248, making this redundant.

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.

2 participants