-
Notifications
You must be signed in to change notification settings - Fork 446
Add new FiniteCycle
type as the return type of Collection.cycled(times:)
#106
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
Conversation
63438b9
to
70a2c56
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Sources/Algorithms/Cycle.swift
Outdated
@@ -56,6 +56,47 @@ extension Cycle: Sequence { | |||
|
|||
extension Cycle: LazySequenceProtocol where Base: LazySequenceProtocol {} | |||
|
|||
|
|||
/// A collection wrapper that repeats the elements of a base collection for a finite number of times. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Please wrap to 80 columns, here and elsewhere.
Sources/Algorithms/Cycle.swift
Outdated
/// A collection wrapper that repeats the elements of a base collection for a finite number of times. | ||
public struct FiniteCycle<Base: Collection> { | ||
/// The collection to repeat. | ||
public let base: Base |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this internal
, as well as times
.
This is because equality (we could conform this type to Equatable
, but it doesn't have to be here in this PR) should be based on elementwise comparisons, and [1, 2, 3].cycled(times: 6)
should be equivalent to [1, 2, 3, 1, 2, 3].cycled(times: 3)
. Having base
and times
publicly accessible would raise the question of whether these properties are "salient" ones for the purposes of considering equivalence.
Thanks for taking this on @LemonSpike, this is a great start! Your |
Thanks for the feedback @timvermeulen! I have tried to add the |
This is indeed what I had in mind, great work! The
|
Hi @timvermeulen, thanks for the feedback. Re: your points, I tried to implement your suggestions as follows:
Let me know your thoughts, thanks. |
Apologies for the delay, @LemonSpike!
We can get rid of the
Great point! Let's for now give
While we make all public and internal methods
That looks good, could you also have it mention the conditional
💯 |
Sources/Algorithms/Product.swift
Outdated
public mutating func next() -> (element1: Base1.Element, | ||
element2: Base2.Element)? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep this tuple unlabeled — Zip2Sequence
's element tuple doesn't have labels either, and generic labels such as element1
and element2
don't really add any clarity over .0
and .1
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am happy to do this, but I still think that the labels improve readability for users who don't know about tuple indexing. I saw we use more descriptive names for public APIs and type names, so I think it could help users understand the API. Let me know your thoughts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These labels don't improve the users' understanding, because they don't convey any useful information. It's impossible for us to give the tuple elements descriptive names because at this level of abstraction we have nothing to go on. To quote the API Design Guidelines:
Omit needless words. Every word in a name should convey salient information at the use site.
Feel free to start a discussion about this topic on the forums, as this is equally relevant to the Zip2Sequence
element type. For the time being we'd like to stick with the precedent set by the standard library.
@swift-ci Please test |
cb3aba8
to
e583de9
Compare
@swift-ci Please test |
1 similar comment
@swift-ci Please test |
Looks great, @LemonSpike — thanks for this addition! 🎉 |
This PR aims to fix #99. I have also split up some relevant tests into separate methods for empty collections and changed some tests for
FiniteCycle
. I have also added tuple labels toProduct2
when using theIterator.next()
method and also in thesubscript
method. This is a cosmetic improvement, but I felt it may read better.Checklist