Created
May 15, 2021 15:00
-
-
Save Ochornma/e0b5f22f48ccdc86d2e01e3681d4d400 to your computer and use it in GitHub Desktop.
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
let plus: (Int, Int) -> Int = { | |
return $0 + $1 | |
} | |
let minus: (Int, Int) -> Int = { | |
return $0 - $1 | |
} | |
let add: (Int, Int) -> Int = { | |
return $0 + $1 | |
} | |
let subtract: (Int, Int) -> Int = { | |
return $0 - $1 | |
} | |
let closureFunctions = ["plus": plus, "minus" : minus ] | |
let plusSign = closureFunctions["plus"] | |
print(plusSign(1, 5)) | |
//result is: | |
//6 | |
//Add a new value to the dictionary | |
closureFunctions["add"] = add | |
closureFunctions["subtract"] = subtract | |
print(closureFunctions.count) | |
//result is: | |
//4 | |
for (key, value) in closureFunctions { | |
print("(\(key),\(value(5, 6)))") | |
} | |
//result is: | |
//(plus, 11) | |
//(minus, 1) | |
//(add, 11) | |
//(subtract, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment