-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Adding RangeReplaceable.filter returning Self #9741
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 all commits
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 |
---|---|---|
|
@@ -877,6 +877,13 @@ extension Sequence { | |
public func filter( | ||
_ isIncluded: (Element) throws -> Bool | ||
) rethrows -> [Element] { | ||
return try _filter(isIncluded) | ||
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 will mean ContiguousArray and ArraySlice will also return an Array from filter, not Self? Which is... probably the right thing? Worth checking my thinking though. 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. That's what they have been doing all along. 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. Oh, I see what you mean here... The line you commented on is misleading, but yeah, even with the addition of a new overload, they will still return 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. Right. I was just questioning whether this is the right thing. I think it is for those two types specifically. |
||
} | ||
|
||
@_transparent | ||
public func _filter( | ||
_ isIncluded: (Element) throws -> Bool | ||
) rethrows -> [Element] { | ||
|
||
var result = ContiguousArray<Element>() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// RUN: rm -rf %t ; mkdir -p %t | ||
// RUN: %target-build-swift %s -o %t/a.out3 -swift-version 3 && %target-run %t/a.out3 | ||
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-run %t/a.out4 | ||
|
||
import StdlibUnittest | ||
|
||
var tests = TestSuite("RangeReplaceableFilterCompatibility") | ||
|
||
tests.test("String.filter return type") { | ||
var filtered = "Hello, World".filter { $0 < "A" } | ||
#if swift(>=4) | ||
expectType(String.self, &filtered) | ||
#else | ||
expectType([Character].self, &filtered) | ||
#endif | ||
} | ||
|
||
tests.test("Array.filter return type") { | ||
var filtered = Array(0..<10).filter { $0 % 2 == 0 } | ||
expectType([Int].self, &filtered) | ||
} | ||
|
||
tests.test("String.filter can return [Character]") { | ||
let filtered = "Hello, World".filter { "A" <= $0 && $0 <= "Z"} as [Character] | ||
expectEqualSequence("HW", filtered) | ||
} | ||
|
||
|
||
runAllTests() |
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.
Probably worth checking that this is just as fast as a hand-rolled loop with
append
. It ought to be, and this is definitely the right way to code it, but worth checking. I'll add a benchmark – doesn't need to hold this up though.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.
One cannot regress something that does not exist. 🤷♂️
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.
Here is the snippet I played with:
Commenting one and uncommenting the other gives these results on my machine:
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.
Also both IR and SIL look better in case of
lazyFilter
.