Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created May 20, 2020 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevencurtis/72494e3ead36f8080514cf9e72d7cdcb to your computer and use it in GitHub Desktop.
Save stevencurtis/72494e3ead36f8080514cf9e72d7cdcb to your computer and use it in GitHub Desktop.
enumcalculator
enum Operand: String {
case addition = "+"
case subtraction = "-"
case multiplication = "*"
case division = "/"
}
func operation (operand: Operand) -> ((Int, Int) -> Int) {
func addition (a: Int, b: Int) -> Int {
return a + b
}
func subtraction (a: Int, b: Int) -> Int {
return a - b
}
func multiplication (a: Int, b: Int) -> Int {
return a * b
}
func division (a: Int, b: Int) -> Int {
return a / b
}
switch operand {
case .addition:
return addition
case .subtraction:
return subtraction
case .multiplication:
return multiplication
case .division:
return division
}
}
var function = operation(operand: Operand.addition)
print("1 + 2 = \(function(1,2))")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment