Complete the code to define a simple closure that adds two numbers.
let add = { (a: Int, b: Int) -> Int in return a [1] b }The closure adds two integers using the + operator.
Complete the code to call the closure with arguments 3 and 5.
let result = add([1])Closures with multiple parameters are called with parentheses and comma-separated values.
Fix the error in the closure that captures a variable and increments it.
var count = 0 let increment = { count [1] 1 }
The '+=' operator increments the variable by 1 inside the closure.
Fill both blanks to create a closure that filters even numbers from an array.
let numbers = [1, 2, 3, 4, 5] let evens = numbers.filter { $0 [1] [2] 2 == 0 }
The modulo operator '%' checks the remainder, and '==' compares it to zero to find even numbers.
Fill all three blanks to create a closure that maps strings to their lengths if length is greater than 3.
let words = ["apple", "cat", "banana"] let filteredLengths = words.reduce(into: [:]) { dict, word in if word.count [1] [2] { dict[[3]] = word.count } }
The closure checks if the word length is greater than 3 and adds it to the dictionary with the word as the key.