Hi everyone!

I’m working on the following method:

extension SequenceType {

    /// Check if `predicate` is true for all elements of `self`
    ///
    /// - Parameter predicate: The predicate called on each element of `self`
    ///
    /// - Returns: True iff every element in `self` satisfies `predicate`, 
false otherwise

    @warn_unused_result
    func all(@noescape where predicate: Generator.Element throws -> Bool) 
rethrows -> Bool {
        for element in self where try !predicate(element) {
            return false
        }

        return true
    }
}

However, when the sequence is empty the method returns true, which is not the 
desired behaviour.

let a = [Int]()
let b = a.all(where: { $0 > 7 })
XCTAssertFalse(b)   // This fails, cause there’s no guard against an empty 
sequence

Does anyone know how to guard against an empty sequence?

I’m using Xcode 7.3.1 and Swift 2.2.

Best,

— A
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to