Use logical operator as combine closure in reduce

前端 未结 6 1431
星月不相逢
星月不相逢 2020-12-15 04:01

I am trying to reduce an array of Bools by applying the logical operator OR (||) using the following code, however I get an error:

相关标签:
6条回答
  • 2020-12-15 04:30

    Ambiguous reference to member '||' means, that there are more than one possible candidates, from which compiler is not able to choose. In your case those are

    public func ||<T : BooleanType, U : BooleanType>(lhs: T, @autoclosure rhs: () throws -> U) rethrows -> Bool
    

    and

    public func ||<T : BooleanType>(lhs: T, @autoclosure rhs: () throws -> Bool) rethrows -> Bool
    

    probably your 'hack' using a { $0 || $1 } is the best solutions here.

    0 讨论(0)
  • 2020-12-15 04:34

    Here's another approach, I modified the reduceBools function to take the operator as a parameter -

    typealias LogicalOperator = ((Bool, @autoclosure () throws -> Bool) throws -> Bool)
    
    func reduceBools(values: [Bool], combine: LogicalOperator) -> Bool {
        var started: Bool = false
        return values.reduce(into: true, { (result, value) in
            result = started ? try! combine(result, value) : value // obviously up to you how you'd handle the try/catch
            started = true
        })
    }
    
    let bools = [true, false, false, true]
    
    let result1 = self.reduceBools(values: bools, combine: ||)
    print(result1) // prints true
    
    let result2 = self.reduceBools(values: bools, combine: &&)
    print(result2) // prints false
    

    Or it could be more useful as an extension of Sequence -

    extension Sequence where Element == Bool {
    
        func reduce(_ combine: LogicalOperator) -> Bool {
            var started: Bool = false
            return self.reduce(into: true, { (result, value) in
                result = started ? try! combine(result, value) : value
                started = true
            })
        }
    }
    
    print(bools.reduce(||)) // prints true
    
    0 讨论(0)
  • 2020-12-15 04:38

    Swift 4.2+ / Xcode 10.0+

    In modern versions of Swift there is allSatisfy function, which checks all elements for satisfying some rule.


    In OP's case:

    values.allSatisfy { $0 }
    
    0 讨论(0)
  • 2020-12-15 04:50

    Following approach will work

    values.reduce(false) { $0 || $1 }
    
    0 讨论(0)
  • 2020-12-15 04:53

    As an alternative, you could use the following approach

    // ||
    func reduceBoolsOr(values: [Bool]) -> Bool {
        return values.contains(true)
    }
    
    // &&
    func reduceBoolsAnd(values: [Bool]) -> Bool {
        return !values.contains(false)
    }
    

    Note that .reduce comes with an overhead. If the end result is the importance of your question (rather than enquiring above the unexpected behaviour of || and && operators in this context), then perhaps the pragmatic approach above can be of help, even if it doesn't really reduce the array, however producing the same result due to the simple nature of the boolean type.

    0 讨论(0)
  • 2020-12-15 04:56

    This happens because of Swifts closure semantics. It takes your arguments and applies function to them, omitting argument names.

    protocol Numeric {
        ...
        public static func +(lhs: Self, rhs: Self) -> Self
        ...
    }
    

    In example with Ints, you would pass (Int, Int) into a closure, and + function in Numeric protocol expects exactly two ints to sum them.

    Thats why code like below works just fine

    [1, 2, 3, 4].reduce(0, +)
    

    Because you just took 2 ints, and applied function, which takes just two ints. If you write your own function, which would take just two argument, it would work as well.

    func myOwnAwesomeFunc<T: Numeric>(a: T, b: T) -> T { in
        return 1 // production ready
    }
    
    [1, 2, 3, 4].reduce(0, myOwnAwesomeFunc) // prints 1
    

    Good so far. But why can't we write

    [true, false, true].reduce(false, ||) // yields Cannot invoke 'reduce' 
    // with an argument list of type 
    // '(Bool, (Bool, @autoclosure () throws -> Bool) throws -> Bool)'
    

    That's because this operator takes bool and a closure, which returns bool. Not bool, closure! But if it is like this, why aren't we writing true || { false }() ? Thats because of @autoclosure, which takes care of curly braces for us.

    Main question, why is it implemented this way, so we can't use Swifts awesome short-hand closure syntax with booleans? Idk

    0 讨论(0)
提交回复
热议问题