[Swift] Closure, Anonymous Function
Have you ever thought that if you create your own awesome application and you put brilliant algorithms made by yourself into the application. You really hide this function, However, you declared this function as a regular function. anybody easy to access and use it.
Swift Closure solves this problem. It's an anonymous function. While it is quite hard to read it before you learn it.
1. Let Turn a Function to Closure.
1 2 3 4 5 6 7 8 9 10 | func calculator (n1: Int, n2: Int, operation: (Int, Int) -> Int) -> Int { return operation(n1, n2) } func multiply (no1: Int, no2: Int) -> Int { return no1 * no2 } calculator(n1: 2, n2: 3, operation: multiply) | cs |
This is a simple script for learning a closure in Swift. The calculator function requires three parameters and now I will make a closure function from multiply function.
{ (parameters) -> return type in
statements
}
1 2 3 4 5 6 7 8 | func multiply (no1: Int, no2: Int) -> Int { return no1 * no2 } //clousre formation { (no1: Int, no2: Int) -> Int in return no1 * no2 } | cs |
2. Put The Closure as a Parameter
1 2 3 4 5 6 7 | func calculator (n1: Int, n2: Int, operation: (Int, Int) -> Int) -> Int { return operation(n1, n2) } calculator(n1: 2, n2: 3, operation: { (no1: Int, no2: Int) -> Int in return no1 * no2 }) | cs |
When you made a closure, you can put it into a function as a parameter.
3. Simplify! More and More!
Problem is started here. You are able to make simplify a closure.
Compiler knows everything. Your closure need it's parameters and they are coming from precedence parameter in front of closure. Therefore you can remove a type in closure. Moreover, if the statement is one sentence, you can also remove return statement.
1 2 3 4 5 | func calculator (n1: Int, n2: Int, operation: (Int, Int) -> Int) -> Int { return operation(n1, n2) } calculator(n1: 2, n2: 3, operation: { (no1, no2) in no1 * no2 }) | cs |
Now, your function has closure is just one line! However, this is not done. Your can simplify it little bit more!
Can you see the no1 and no2 parameters are used twice? and eventually, you will multiply no1 and no2 so you are able to make it $0 * $1
in Swift, $0 means parameter 1, $1 is parameter 2 ... so you can replace no1 and no2 as $0 and $1. It tells compiler that your closure reuse precedence parameters.
However, if you want to use $0 and $1 you need to drag out this closure from parameter brackets()
This is finally form of closure
1 2 3 4 5 6 | func calculator (n1: Int, n2: Int, operation: (Int, Int) -> Int) -> Int { return operation(n1, n2) } let result = calculator(n1: 2, n2: 3) {$0 * $1} print(result) | cs |
Wow, it's quite different from the original version of closure. That is reason why some developers who didn't learn Swift hard to read it.
This is a challenge. Try to change this array.map(addOne) to closure formation.
1 2 3 4 5 6 7 | let array = [6,2,3,9,4,1] func addOne(n1: Int) -> Int{ return n1 + 1 } array.map(addOne) | cs |
I will answer this quiz on the reply!
Let's dive into Swift's Closure
This is answer!
50% score array.map({(n1) in n1 + 1 })
100% score array.map{$0 + 1}