2019년 1월 16일 수요일

Function Types

Swift Programming Language Guide: Function, 코드부분




/* Function Types */

func addTwoInts(_ a: Int, _ b: Int) -> Int {
   return a + b
}
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
   return a * b
}

// Using Function Types

var mathFunction: (Int, Int) -> Int = addTwoInts(_:_:)

print("Result: \(mathFunction(2, 3))")
mathFunction = multiplyTwoInts(_:_:)
print("Result: \(mathFunction(2, 3))")

let anotherMathFunction = addTwoInts

// Function Types as Parameter Types

func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
   print("Result: \(mathFunction(a, b))")
}

printMathResult(addTwoInts, 3, 5)

// Function Types as Return Types

func stepForward(_ input: Int) -> Int {
   return input + 1
}

func stepBackward(_ input: Int) -> Int {
   return input - 1
}

func chooseStepFunction(backward: Bool) -> (Int) -> Int {
   return backward ? stepBackward : stepForward
}

var currentValue = 3
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)

print("Counting to zero:")
while currentValue != 0 {
   print("\(currentValue)...")
   currentValue = moveNearerToZero(currentValue)
}
print("zero!")

// Nested Functions

func chooseStepFunction(backward: Bool) -> (Int) -> Int {
   func stepForward(input: Int) -> Int { return input + 1 }
   func stepBackward(input: Int) -> Int { return input - 1 }
   return backward ? stepBackward : stepForward
}

var currentValue = -4
let moveNearerToZero = chooseStepFunction(backward: currentValue > 0)
while currentValue != 0 {
   print("\(currentValue)...")
   currentValue = moveNearerToZero(currentValue)
}
print("zero!")