-
Notifications
You must be signed in to change notification settings - Fork 442
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
Adjacent pairs wrapping #141
Conversation
98a5278
to
f578c98
Compare
f578c98
to
1370d96
Compare
|
This is something that I struggled with. I was hoping to get some feedback on the naming. I agree that Here are some other options to get the discussion going:
|
If we added this, I presume we would want to add the same functionality to let x = [1, 2, 3, 4, 5]
for window in x.windows(ofCount: 3, wrapping: true) {
print(window)
}
// Prints [1, 2, 3]
// Prints [2, 3, 4]
// Prints [3, 4, 5]
// Prints [4, 5, 1]
// Prints [5, 1, 2] |
I think you meant Due to the similarities between Perhaps instead of adding a |
I appreciate that this was motivated by a concrete real world use case, however after thinking about this more, I'm not sure adding a For example, I think this behavior can be relatively trivially composed out of let x = (1...).prefix(5)
for pair in chain(x, x.prefix(1)).adjacentPairs() {
print(pair)
}
// Prints "(1, 2)"
// Prints "(2, 3)"
// Prints "(3, 4)"
// Prints "(4, 5)"
// Prints "(5, 1)" I vote that we close this for now and revisit if additional concrete use cases arise. |
Agreed, we can close this one for now. Thanks for testing this out, @mdznr — happy to discuss this more on the forums! |
Description
Adds an optional
wrapping
parameter toadjacentPairs
algorithm that allows you to include a pair of the last and first element.Detailed Design
Alternatives Considered
Sequence
andCollection
algorithm. This would have resulted in a lot of duplicate code fromAdjacentPairs
that would be better shared between the implementations.Documentation Plan
I’ll waiting to hear feedback on whether or not this is desirable and on the direction before diving into the documentation too much.
Guides/AdjacentPairs.md
README.md
Test Plan
For each of the existing
adjacentPairs
unit tests, I’ve added one that tests the wrapping behavior as well.Source Impact
Existing calls to
adjacentPairs()
continues to work as-is since thewrapping
parameter has a default value offalse
.Checklist