Add commonPrefix(with:), commonSuffix(with:)#153
Add commonPrefix(with:), commonSuffix(with:)#153timvermeulen wants to merge 7 commits intoapple:mainfrom
commonPrefix(with:), commonSuffix(with:)#153Conversation
|
I think it's more natural to not pluralize of |
Fair point. I think I went with this because we already have |
| } else { | ||
| return nil | ||
| } | ||
| } |
There was a problem hiding this comment.
I think the iterator needs to add a done flag to its state. For instance, if we do "1234A56" vs. "1234B56", and we iterate past the first nil, we'll get a "5", instead of nil forevermore.
There was a problem hiding this comment.
Thanks, you're absolutely right!
| by areEquivalent: @escaping (Element, Other.Element) -> Bool | ||
| ) -> CommonPrefix<Self, Other> { | ||
| CommonPrefix(base: self, other: other, areEquivalent: areEquivalent) | ||
| } |
There was a problem hiding this comment.
Shouldn't these be using Elements and elements instead of Self and self?
There was a problem hiding this comment.
That's a way to do it, but this is intentional. If you wrap elements instead of self then the type no longer contains any sign of laziness, so you'll need to wrap it in LazySequence<...> again to add the laziness back. But by wrapping self, all we need is for CommonPrefix to conditionally conform to LazySequenceProtocol when the (first) base collection does, in order to propagate the laziness.
CommonPrefix also can't unconditionally conform to LazySequenceProtocol because it's also returned by the Sequence overloads, so in a non-lazy context.
| by areEquivalent: @escaping (Element, Other.Element) -> Bool | ||
| ) -> CommonPrefix<Self, Other> { | ||
| CommonPrefix(base: self, other: other, areEquivalent: areEquivalent) | ||
| } |
There was a problem hiding this comment.
Same as in the LazySequenceProtocol section: shouldn't these be using Elements and elements instead of Self and self?
|
Don't |
…ffix` -> `startOfCommonSuffix`
d6d9443 to
3c60204
Compare
Find the common prefix/suffix of two sequences or collections.
commonPrefix(with:)commonSuffix(with:)endOfCommonPrefix(with:): get a pair of indices that mark the end of the common prefix of two collectionsstartOfCommonSuffix(with:): get a pair of indices that mark the start of the common suffix of two collectionsChecklist