Skip to content

Instantly share code, notes, and snippets.

@Ochornma
Created May 15, 2021 15:00
Show Gist options
  • Save Ochornma/e0b5f22f48ccdc86d2e01e3681d4d400 to your computer and use it in GitHub Desktop.
Save Ochornma/e0b5f22f48ccdc86d2e01e3681d4d400 to your computer and use it in GitHub Desktop.
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