Adds "isSorted", "isSorted(by: )" and "allEqual" to Comparable Sequence#6
Adds "isSorted", "isSorted(by: )" and "allEqual" to Comparable Sequence#6mcekr wants to merge 3 commits into
Conversation
| /// Returns Bool, indicating whether a sequence is sorted using | ||
| /// the given predicate as the comparison between elements. | ||
| /// | ||
| /// - Complexity: O(*n*), where *n* is the length of the sequence. |
There was a problem hiding this comment.
These docs go right above the function signature, not below 🙂
| return true | ||
| } | ||
|
|
||
| public func allEqual() -> Bool { |
There was a problem hiding this comment.
This name is confusing to me, as it reads like it could be related to elementsEqual. It also seems like it's straightforward enough that we don't necessarily need to provide it in a library if the building blocks are there.
There was a problem hiding this comment.
I agree — let’s keep this addition focused on checking that a collection is sorted.
|
After thinking about this for a little bit, I think it might be nice if |
natecook1000
left a comment
There was a problem hiding this comment.
Thanks for this PR, @mcekr! Some notes below. Could you also look at adding a document describing the purpose for these additions to the Guides/ folder?
| isSorted(by: <) | ||
| } | ||
|
|
||
| public func isSorted(by areInIncreasingOrder: (Element, Element) -> Bool) -> Bool { |
There was a problem hiding this comment.
Two notes on this version:
- Since this method takes a closure for comparisons, it doesn’t need to be in the constrained extension
- For this kind of method, we mark the closure as throwing and the method itself as
rethrows
| import XCTest | ||
| import Algorithms | ||
|
|
||
| final class IsSortedTests: XCTestCase { |
There was a problem hiding this comment.
It would be good to test an empty collection, to check sorted collections that includes repeats (e.g. [1, 1, 1, 2, 3]), and also to verify that isSorted returns false for unsorted collections.
| return true | ||
| } | ||
|
|
||
| public func allEqual() -> Bool { |
There was a problem hiding this comment.
I agree — let’s keep this addition focused on checking that a collection is sorted.
|
|
||
| extension Sequence where Element: Comparable { | ||
| public func isSorted() -> Bool { | ||
| /// Returns Bool, indicating whether a sequence is sorted |
There was a problem hiding this comment.
To follow the Swift documentation style, these doc comments should start with “Returns a Boolean value indicating whether...”
|
@timvermeulen I disagree on this point — to match the semantics of the other operations that take comparison predicates, |
|
That's a pretty compelling argument! I believe the current implementation is incorrect then, if that's the semantics we're going for: Under these semantics, I believe |
Sure |
|
@natecook1000 @timvermeulen In order to match the semantics of the other operations if let p = prev, !(!areInIncreasingOrder(element, p) || areInIncreasingOrder(p, element)) {
return false
}but is it acceptable? |
|
@mcekr It's better to think of the comparison predicate as establishing a weak order over the if let p = prev, areInIncreasingOrder(element, p) {
return false
}@timvermeulen Passing |
|
Closing this for now, feel free to re-open when you're ready to discuss further. Thanks! |
Description
Adds a "isSorted" and "isSorted(by: )" methods which return true when a sequence is in sorted order and "allEqual" which returns true if all the elements are equal to each other.
Detailed Design
Documentation Plan
Added some comments.
Test Plan
Test class included.
Source Impact
Adds new API.
Checklist