I am moving through the Swift Programming Language Book and I don't fully understand this:
//I don't understand 'condition: Int -> Bool' as a parameter, what is that saying?
func hasAnyMatches(list: Int[], condition: Int -> Bool) -> Bool {
//So here i see we are iterating through an array of integers
for item in list {
//I see that condition should return a Bool, but what exactly is being compared here? Is it, 'if item is an Int return true'??
if condition(item) {
return true
}
}
return false
}
//This func I understand
func lessThanTen(number: Int) -> Bool {
return number < 10
}
var numbers = [20, 19, 7, 20]
hasAnyMatches(numbers, lessThanTen)
If you could explain a little more of what exactly is going on here it would be much appreciated. I ask most of the question in the comments so it's easier to read but the main thing confusing me is condition: Int -> Bool as a parameter.
lessThanTen()function's type is "take Int, return Bool", which is expressed syntactically asInt -> Bool. The code will not compile if you provide a function with a different type.