Created
March 6, 2018 18:29
-
-
Save BasThomas/7958e92d269167ff1a058b27c4f2d206 to your computer and use it in GitHub Desktop.
A `containsOnly` and `containsNone` implementation for `Sequence`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Sequence where Element: Equatable { | |
/// Returns a Boolean value indicating whether every element of the sequence | |
/// is equal to the given element. | |
func containsOnly(_ element: Element) -> Bool { | |
var iterator = self.makeIterator() | |
guard iterator.next() != nil else { return false } | |
return first(where: { $0 != element }) == nil | |
} | |
/// Returns a Boolean value indicating whether every element of the sequence | |
/// does not equal to the given element. | |
func containsNone(_ element: Element) -> Bool { | |
return first(where: { $0 == element }) == nil | |
} | |
} | |
extension Sequence { | |
/// Returns a Boolean value indicating whether every element of the sequence | |
/// satisfies the given predicate. | |
func containsOnly(where predicate: (Element) throws -> Bool) rethrows -> Bool { | |
var iterator = self.makeIterator() | |
var isNotEmpty = false | |
while let element = iterator.next() { | |
isNotEmpty = true | |
if try !predicate(element) { | |
return false | |
} | |
} | |
return isNotEmpty | |
} | |
/// Returns a Boolean value indicating whether every element of the sequence | |
/// does not satisfies the given predicate. | |
func containsNone(where predicate: (Element) throws -> Bool) rethrows -> Bool { | |
var iterator = self.makeIterator() | |
while let element = iterator.next() { | |
if try predicate(element) { | |
return false | |
} | |
} | |
return true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
class SequenceTests: XCTestCase { | |
func test_containsAll() { | |
XCTAssertTrue(["a", "a", "a"].containsOnly("a")) | |
XCTAssertFalse(["b", "a", "a"].containsOnly("a")) | |
XCTAssertFalse(["a", "a", "a"].containsOnly("b")) | |
XCTAssertTrue(["a", "a", "a"].containsOnly { $0 == "a" }) | |
XCTAssertFalse(["b", "a", "a"].containsOnly { $0 == "a" }) | |
XCTAssertFalse(["a", "a", "a"].containsOnly { $0 == "b" }) | |
XCTAssertTrue([1, 1, 1].containsOnly(1)) | |
XCTAssertFalse([2, 1, 1].containsOnly(1)) | |
XCTAssertFalse([1, 1, 1].containsOnly(2)) | |
XCTAssertTrue([1, 1, 1].containsOnly { $0 == 1 }) | |
XCTAssertFalse([2, 1, 1].containsOnly { $0 == 1 }) | |
XCTAssertFalse([1, 1, 1].containsOnly { $0 == 2 }) | |
} | |
func test_containsNone() { | |
XCTAssertFalse(["a", "a", "a"].containsNone("a")) | |
XCTAssertFalse(["b", "a", "a"].containsNone("a")) | |
XCTAssertTrue(["a", "a", "a"].containsNone("b")) | |
XCTAssertFalse(["a", "a", "a"].containsNone { $0 == "a" }) | |
XCTAssertFalse(["b", "a", "a"].containsNone { $0 == "a" }) | |
XCTAssertTrue(["a", "a", "a"].containsNone { $0 == "b" }) | |
XCTAssertFalse([1, 1, 1].containsNone(1)) | |
XCTAssertFalse([2, 1, 1].containsNone(1)) | |
XCTAssertTrue([1, 1, 1].containsNone(2)) | |
XCTAssertFalse([1, 1, 1].containsNone { $0 == 1 }) | |
XCTAssertFalse([2, 1, 1].containsNone { $0 == 1 }) | |
XCTAssertTrue([1, 1, 1].containsNone { $0 == 2 }) | |
} | |
func test_emptySequence() { | |
let emptyStrings: [String] = [] | |
let emptyInts: [Int] = [] | |
XCTAssertTrue(emptyStrings.containsNone("a")) | |
XCTAssertTrue(emptyStrings.containsNone { $0 == "a" }) | |
XCTAssertFalse(emptyStrings.containsOnly("a")) | |
XCTAssertFalse(emptyStrings.containsOnly { $0 == "a" }) | |
XCTAssertTrue(emptyInts.containsNone(1)) | |
XCTAssertTrue(emptyInts.containsNone { $0 == 1 }) | |
XCTAssertFalse(emptyInts.containsOnly(1)) | |
XCTAssertFalse(emptyInts.containsOnly { $0 == 1 }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment