Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 1.04 KB

Closures.md

File metadata and controls

54 lines (39 loc) · 1.04 KB

Clousures

Typical way of passing functions around

func calculator(n1: Int, n2: Int, operation: (Int, Int) -> Int) -> {
  return operation(n1, n2)
}

fun multiply(no1: Int, no2: Int) -> Int {
  return no1 * no2
}

calculator(n1: 2, n2: 3, operation: multiply)

Closure way

Example 1
func calculator(n1: Int, n2: Int, operation: (Int, Int) -> Int) -> {
  return operation(n1, n2)
}

let result = calculator(n1: 2, n2: 3) {$01 * $1})
print(result)
Example 2
// Add one to each element 
let array = [6, 2, 3, 9, 4, 1]

array.map({$0 + 1})

Closure Expression Syntax

Closure expression syntax has the following general form:
{ (parameters) -> return_type in
    statements
}