Sunday, June 25, 2017

Closures - Part 1 (Swift)

4:12 AM Posted by CHANDAN MAKHIJA No comments
Today, i will walk you through the most important concept in swift i.e Closures. Today i will talk about the meaning of closure, higher order function, first class function. These words may come across foreign to you. Well, let’s learn together.

What are closures ?
In the most basic terms if i say, a closure is just like a function without the keyword func and without a name. From the newcomer’s perspective, however, Closures seem naked and even incomplete.

So, let’s compare how you would add two numbers in a function vs closure. Both will take two Int parameters and return one Int. Let’s begin with a function.
func addTwoNumbers(number1: Int, number2: Int) -> Int {
return number1 + number2
}
var storedFunc = addTwoNumbersstoredFunc(2, 9) // 11
// Closure
var storedClosure: (Int, Int) -> Int = { (number1, number2) in
return number1 + number2
}
storedClosure(number1: 5, number2: 9) // 14
// Shorter
var storedClosure: (Int, Int) -> Int = { return $0 + $1 }
// Super Short
var storedClosure: (Int, Int) -> Int = { $0 + $1 }
var closureHelloWorld: () -> String = { return "hello" }
insertSomething(closure: closureHelloWorld) // "hello"
insertSomething(closure: { return "hello world"}) // "hello world"

I’ve stored the addTwoNumber function into the newly created var called, storedFunc. But, how is this possible? Well, in Swift , just like many other programming languages, Swift functions are described as a first-class function. I don’t know why it is called that way, but you can store a function to a variable/constant.
However, we don’t have to use the func keyword to store a function. Indeed, we can use closure instead.
var storedClosure: (Int, Int) -> Int = { (number1, number2) in
 return number1 + number2
}
storedClosure(number1: 5, number2: 9) // 14

The above example is identical to the first example. The in keyword is used to separate the input parameters,number1 andnumber2from the return part. Also, we’ve stated that the type of storedClosure is (Int, Int) -> Int. The pre-stated type tells the variable that it takes two parameters and return one Int.

But, the example above can be simplied, and, yes, it is still called closure.
// Shorter
var storedClosure: (Int, Int) -> Int = { return $0 + $1 }
// Super Short
var storedClosure: (Int, Int) -> Int = { $0 + $1 }

So, what the heck is $0and $1 ? Well, it’s just a syntax by Swift. It automatically recognizes two Int parameters. The first input value is $0 and the second, $1. Also, we don’t even need to put return if you are just playing with those two parameters.

So far, you’ve learned closures and functions are pretty much the same and both are first-class since you can store them into a variable. But also, they are called, higher order function. You can use closure/function as parameters and even return them.  

 If you want to add a closure as a parameter, you could do this way
Example 1:
insertSomething(closure: { return "hello world"}) // "hello world"

Example 2:


func someFunctionThatTakesAClosure(completionClosure: () -> ()) {
    // function body goes here
    if(error = false) {
       completionClosure()
    }

}

//Call it
someFunctionThatTakesAClosure({
    //Completions Stuff
    println("someFunctionThatTakesAClosure")
});

0 comments:

Post a Comment