Skip to content

[swift-4.0-branch][stdlib] Add a Swift 4 only RangeRepleaceable.filter returning Self #9750

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

Merged
merged 2 commits into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,16 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
}
}

// Since RangeReplaceableCollection now has a version of filter that is less
// efficient, we should make the default implementation coming from Sequence
// preferred.
@_inlineable
public func filter(
_ isIncluded: (Element) throws -> Bool
) rethrows -> [Element] {
return try _filter(isIncluded)
}

//===--- algorithms -----------------------------------------------------===//

@_inlineable
Expand Down
25 changes: 25 additions & 0 deletions stdlib/public/core/RangeReplaceableCollection.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,31 @@ public func += <
lhs.append(contentsOf: rhs)
}

extension RangeReplaceableCollection {
/// Returns a new collection of the same type containing, in order, the
/// elements of the original collection that satisfy the given predicate.
///
/// In this example, `filter(_:)` is used to include only names shorter than
/// five characters.
///
/// let cast = ["Vivien", "Marlon", "Kim", "Karl"]
/// let shortNames = cast.filter { $0.count < 5 }
/// print(shortNames)
/// // Prints "["Kim", "Karl"]"
///
/// - Parameter isIncluded: A closure that takes an element of the
/// sequence as its argument and returns a Boolean value indicating
/// whether the element should be included in the returned array.
/// - Returns: An array of the elements that `isIncluded` allowed.
@_inlineable
@available(swift, introduced: 4.0)
public func filter(
_ isIncluded: (Element) throws -> Bool
) rethrows -> Self {
return try Self(self.lazy.filter(isIncluded))
}
}

@available(*, unavailable, renamed: "RangeReplaceableCollection")
public typealias RangeReplaceableCollectionType = RangeReplaceableCollection

Expand Down
7 changes: 7 additions & 0 deletions stdlib/public/core/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,13 @@ extension Sequence {
public func filter(
_ isIncluded: (Element) throws -> Bool
) rethrows -> [Element] {
return try _filter(isIncluded)
}

@_transparent
public func _filter(
_ isIncluded: (Element) throws -> Bool
) rethrows -> [Element] {

var result = ContiguousArray<Element>()

Expand Down
12 changes: 12 additions & 0 deletions test/stdlib/ArrayFilter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-frontend -swift-version 4 -O %s -emit-sil | %FileCheck %s

// This test is testing that even in presence of
// RangeReplaceableCollection.filter(_:), Arrays are still calling the default
// implementation from Sequence.

// CHECK-NOT: RangeReplaceableCollection.filter(_:)
@inline(never)
public func foobar(_ xs: [Int]) -> [Int] {
return xs.filter { _ in false }
}

39 changes: 39 additions & 0 deletions test/stdlib/RangeReplaceableFilterCompatibility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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("ContiguousArray.filter return type") {
var filtered = ContiguousArray(0..<10).filter { $0 % 2 == 0 }
expectType([Int].self, &filtered)
}

tests.test("ArraySlice.filter return type") {
var filtered = Array(0..<10)[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()