-
Notifications
You must be signed in to change notification settings - Fork 446
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Adds "isSorted", "isSorted(by: )" and "allEqual" to Comparable Sequence #6
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These docs go right above the function signature, not below 🙂
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.
Yeah, didn't notice that, thanks
return true | ||
} | ||
|
||
public func allEqual() -> Bool { |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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