-
Notifications
You must be signed in to change notification settings - Fork 446
add suffix:while method #65
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
Changes from 2 commits
9ef531f
1acc213
813ea79
08e424e
60bad3b
2f8f798
f9a4f79
3b51107
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||||||
//===----------------------------------------------------------------------===// | ||||||||||
// | ||||||||||
// This source file is part of the Swift Algorithms open source project | ||||||||||
// | ||||||||||
// Copyright (c) 2020 Apple Inc. and the Swift project authors | ||||||||||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||||||||||
// | ||||||||||
// See https://swift.org/LICENSE.txt for license information | ||||||||||
// | ||||||||||
//===----------------------------------------------------------------------===// | ||||||||||
|
||||||||||
//===----------------------------------------------------------------------===// | ||||||||||
// Suffix(while:) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
//===----------------------------------------------------------------------===// | ||||||||||
|
||||||||||
extension BidirectionalCollection { | ||||||||||
@inlinable | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documentation should be added. Mirroring the documentation for |
||||||||||
public __consuming func Suffix(while predicate: (Element) throws -> Bool | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
) rethrows -> [Element] { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
var result = ContiguousArray<Element>() | ||||||||||
self.reverse() | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consistent indentation
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t believe It seems that you might have meant to use the non-mutating version of that function, for element in self.reversed() { However, this comment likely won’t be applicable when changing the function to return |
||||||||||
for element in self { | ||||||||||
guard try predicate(element) else { | ||||||||||
break | ||||||||||
} | ||||||||||
result.append(element) | ||||||||||
} | ||||||||||
result.reverse() | ||||||||||
return Array(result) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This double-reverse operation and converting to This would be similar to the implementation of |
||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||
//===----------------------------------------------------------------------===// | ||||||
// | ||||||
// This source file is part of the Swift Algorithms open source project | ||||||
// | ||||||
// Copyright (c) 2020 Apple Inc. and the Swift project authors | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||||||
// | ||||||
// See https://swift.org/LICENSE.txt for license information | ||||||
// | ||||||
//===----------------------------------------------------------------------===// | ||||||
|
||||||
import XCTest | ||||||
import Algorithms | ||||||
|
||||||
final class SuffixTests: XCTestCase { | ||||||
natecook1000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
func testSuffix() { | ||||||
let a = 0...10 | ||||||
XCTAssertEqualSequences(a.suffix(while: { $0 > 5 }), (6...10)) | ||||||
} | ||||||
natecook1000 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.