 Hi my name is Kajal and I started learning word shift recently and I came across various features over here so I had to talk with Shubh and wanted to discuss more closer in this session. Closing is something like it's a functionality block and you can pass it around the code. It's just like function and it is available in various other programming languages like if you have been doing JavaScript or you have been doing any other language like Seesha, then you might have stayed over there. Okay, so within Swift there are three different types of closure. First global function, the second one is nested and third one is closure expression. So if you have any global function that is as same as like that, that's a kind of closure. Special case of closure which basically does not captures a variable whereas in nested function, I'll show you that how it captures the variable scope and there are closer expressions as well. Okay, so if we take up this example and let's try to see that what exactly it is. Normally a function is having this particular presentation where it accepts an empty string and returns a string over here. So we have got function pointers which is also available in other languages and let's try to understand. So here I have two methods in example. One is say hello and the other one is sum. You can see the signatures like the first one is accepts string as an input and it returns a string as an output. That is fn and the fn2 is basically sum and you can see the string representation where it accepts two input as parameter and returns an integer. Okay, so these are a simple case of functions like you can say as a global function and generally in day to day life we don't bother much about it. Now we have a case of nested function. Nested function is something like within JavaScript also you can see that you can have function within a function. In the same way Swift can also have a function within a function. Okay, and I have a very simple example like we have operation like addition, subtraction, multiplication, division. And we have a method called get operation and we supply the parameter that what kind of operation we want. And it returns a function which accepts a type and input as an integer returns the calculated value. So these are quite plain functions like add subtract as it is going to see from the signature. We have a local variable over here. I will talk more about it. Okay, so add is a function which basically adds the value to the result and returns the final value. Okay, and the same case applies to other methods. All right, so for division we have got a simple check that if it is greater than zero then it will compute the value otherwise it returns zero. Okay, so there is a switch case on operation that is exactly what we want to return. Here the methods are only they exist as signatures just like a class. But they are not actually invoked at the moment. So based on the operation which is requested in get operation the appropriate functions return back. Okay, so now a special case over here you can see is like I have created the demo itself within a function. Okay, so this indicates that we can have a function within a function within a function. So this feature is very good in functional programming and most of the new languages which are coming in the market right now like you can see Swift, Rust and those who have experience with Haskell as well. So it is quite helpful. So in this demo you can observe that you have requested for an addition operation. Now we hold a pointer to function add and initial value is zero. So if you do a function add it will give you values 10. If you second time if you make a call to function add it will return a value of 20 because it keeps a hold a local copy of f and add holds an instance of result. We have requested subtract again in the second operation. Now subtract will have its own copy of result. So both of the function they will hold their own copy of the result variable. Okay, so if you subtract three from zero you'll get minus three. Now there are cases when you want to share the state of variable or you want to have just a single variable. Languages like C they have static variables but in case of function you cannot have a static variable directly. So there's a way to solve this problem that instead of this result you can create a struct which has a starting number. Okay, let's change this. Let's change this result to... So here you can see that both add and subtract they both are sharing the state where two method calls to this addition. Make it 20 then you subtract a value from it. It will make three. So in case if you wish to share the state you can make use of struct within a function. So this is the case of nested function where you have seen that you have captured the external variable even though you have taken an instance of... Function add. Now one interesting thing over here is like all the closures are reference type. So if you reassign this something else like let fn1 goes to fm add and if you make a call fn1 so it will make it 22. Otherwise also if you don't use this struct instead of this struct even if you use this variable then also this fn add and fn1 they both will share the same state. Now the next one is basically I have closure. I have taken a few content from Apple's documentation which is online and which is quite what a nice book to go through. So generally like most of the use what we have of closure is to define a predicate. A predicate is something basically which works on a verb and you have a criteria and it simply evaluates whether that condition is true or false. Like we have got filter, map, reduce, sort this kind of activity which you want to perform. You want to convert based on certain criteria you want to convert your data to uppercase, lowercase or you want to just filter out a few of the text. This kind of stuff. So you can create appropriate predicate. Here I have created a predicate called increasing order. It simply checks, compares two numbers. So one number is small. If checks that number one is smaller than number two it returns two definitely otherwise false. So I have this predicate and just make a call to this closure. So here I have this number. There are six numbers and when I'm making a call to sort method. It is taking the array and sorting into increasing order. There are basically two methods available in sort. One is sort and the other one is sort in place. Sort in place basically sorts the existing array, doesn't create a copy. And this sort method returns the sorted error. Now we have provided the predicate to the sort method. If you check this sort method basically it accepts a predicate of two parameters which returns a boolean array. Now the predicate itself should return, it has got a bigger predicate which returns the actual element. Let's go back. We can simplify our predicates into much more manner because mostly in functional programming we have mostly the short notations of the syntax what we use. I tried to sort it in this way. So instead of providing a complete method I have given it a closer expression. A closer expression is something like it has a signature and there is in which is a keyword and then the body follows. So I have then exactly the same thing but I have put in over there and this curly bracket is at this particular position. So that's another way of doing it. You can simplify. Swift has a feature called inferring the data type. So Swift automatically infers the data type of the parameter from the content of array. The type of content which is there in the score array, it basically takes the same data type, assumes that it's the same one and it applies the operator and calculations of the same data type. So you can simplify it further like I have simplified it as like the return type can also be inferred. So you don't need to mention the return type. So this is even shorter. And if the return keyword is also optional and if it is a single line statement then you can simply remove the return keyword from here as well. Now this quite interesting part where we have a shortened argument notation. So instead of taking it as number one or number two, you can use the parameter position as a variable name. Just prefix it with the dollar sign. So Swift compiler already knows that sort takes a method with two arguments. So you don't need to mention the arguments over here, number one or number two. It infers the data type and also the compiler knows that you are here, you meant the first argument and by dollar one you meant the second argument. And you have an operator over there. So you can simply provide this expression and the compiler automatically generates the same code what it generates for the previous expression. So there is another function called operator function which is even shorter than this where you can simply provide the operator sign. Now this basically works on like this. We can even simplify it like the compiler knows that okay this is a less than operator. A less than operator cannot work without two arguments. So you definitely need two arguments. The provided number of arguments are also two. So it simply assumes that on the left hand side you are keeping the first argument. On the right hand side you are keeping the second argument. And whatever is the comparison result it returns back to the short method. So they all work in the same way and they all gives you the same result. So that is the simplification of a method which is quite long like this and you can make it short. This is quite helpful like you don't need to create utility methods for your classes or for your objects. Now there is something called trailing closure. Trailing closure means there is another catch over here. If you have a method which accepts or which takes closure as an input and if that input closure is the final argument that is the last argument within that method like sort takes just one method over here which is a closure and if you have got something which takes three parameters and the third parameter is your closure. So you can simply close a bracket over here just like a method called and you can place the closure expression right after the method called. It is called trailing closure. It's something like suppose if you have got some object dot method argument one then there is named argument x argument 2 and then there is a closure. The third parameter is closure. So what you can simply do is you can create the closure body here and you can simply write whatever you want to do as a final argument. So it looks like a body of this method but basically it's a you can treat it like this. So instead of getting puzzled into bracket matching it's better if you can use directly as a final argument. You don't need to put the label over there as well. Now these moon brackets are also optional. So what you can do you can simply put the curly braces and Swift knows that this is a method and there are no more parameters. Now if you want to get rid of moon brackets it is compulsory that your sort method or whatever method it is should have just one parameter and which takes the closure as an argument. So that's something with closure. There is something called auto closure as well but frankly I am still looking out for a right use case where I can make use of auto closure. Just now we discussed a lot of different ways to make use of closure and these are the six different ways. I took it from all the website. It was quite well explained. So this is a map reduce function where it maps each of the elements and gives it to the closure function and the closure function multiplies that element and it turns back the multiplied value. All of these six expressions return the same result. So we talked about capturing values. The capturing values is something like we discussed in the operations method where it has got to add, subtract, multiply and divide where it captured the variable which was in the outer scope. The result variable and we also get to know about it like each closure is a reference type. You can pass it on further and it will remain as it is and the captured value within the closure will also remain captured within the scope of the closure. We talked about trading closure which means if you have a function called and the last argument is a closure then you can make use of trading closure using the curly brackets. There are three more things like no escape, auto closure and auto closure by skipping. Normally if you don't want to make the call of closure at that moment within that function call. For example suppose a sort also wants to take an average at the end but not at the time when it is sorting that could be more than one function call. Then you can make use of escaping closures. Maybe later I will come up with a demo of that. I am just trying to find the right case for this with a good example. Questions? Thank you. I will have some sort of questions from the pledge and these are very, very basic questions.