As I understand things, the compiler will short-circuit evaluations once it nows the outcome.
For example
if (a && b && c && d)
if a is false, there is no need to evaluate b, c or d as regardless of their value, the if will always be false.
Now, assume there is an array of Booleans and you want to find out if they are all true. You could do that with a reduce() statement.
array.reduce(true){$0 && $1}
Does the evaluation of the reduce stop once a false value is encountered?
FWIW, I came up with this while writing a function to convert Apples OSErr type to a printable string if possible.
// convert Apple 32-bit int to 4 characters if valid
// 1819304813 = "lpcm"
// 1718449215 = "fmt?"
// -50 = (-50)
func int32ToString(_ error: OSStatus) -> String {
let chars = Swift.withUnsafeBytes(of: error.bigEndian){ Data($0) }.map{ Character(Unicode.Scalar($0)) }
return chars.reduce(true){ $0 && $1.isASCII } ? "\"\(String(chars))\"" : "(\(error))"
}
I like eliminating For loops with map() or reduce() statements.