-
Notifications
You must be signed in to change notification settings - Fork 440
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
Add commonPrefix(with:)
, commonSuffix(with:)
#153
base: main
Are you sure you want to change the base?
Conversation
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't these be using Elements
and elements
instead of Self
and self
?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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