Created
September 26, 2015 00:58
-
-
Save pedrogk/94cd850c7346b4c0fcb9 to your computer and use it in GitHub Desktop.
Funciones
This file contains hidden or 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
func getBound(anArray: [Int], test: (Int, Int) -> Bool) -> Int { | |
var bound = anArray[0] | |
for item in anArray[1..<anArray.count] { | |
if test(item, bound) { | |
bound = item | |
} | |
} | |
return bound | |
} | |
func greaterThan(a: Int, b: Int) -> Bool { | |
return a > b | |
} | |
func lessThan(a: Int, b: Int) -> Bool { | |
return a < b | |
} | |
let anArray = [52, -234, 177, 23, -545, 16, -8, 23, 3, -1] | |
var aFunction = greaterThan | |
var bound = getBound(anArray, aFunction) // bound = 177 | |
aFunction = lessThan | |
bound = getBound(anArray, aFunction) // bound = -545 | |
func chooseFunction(select: Int) -> (Int, Int) -> Bool { | |
return select == 1 ? greaterThan : lessThan | |
} | |
aFunction = chooseFunction(2) | |
aFunction(4, 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment