 Hi, my name is Sohail, I'm from Property Group and today I'd like to talk about functional programming. It's something that's been a trend, people are talking about it, it's a new post. You've come here from Pascal, people love to talk about this thing. But to be honest, Swift is not a functional, but it's flexible enough to play, that led us to play a little bit of functionality that Haskell brought to the world. So, let's get started. Oh, if I actually go, I just want to tell you guys, I'm not expert on any of this. I'm just, I'm learning at the same time, just want to share whatever I learned. Let's go. So, what is functional programming? It's a programming paradigm, same as the object-oriented paradigm. You have objects, that bunch of methods inside of it. Inside object-oriented, the classes are the king, in functional programming, functions are the king. And this programming paradigm, everything has to be declarative in a very expressive manner. And it's more of a coding style, how you want to approach the project, how you want to make it, how to write the right clean codes. It's more of a mindset, if I could say. And it's pretty much all about how to write nice and clean code. Nothing more about that. So, how do we do functional programming? Everything in functional programming has to be retained in a very expressive way. It has to be clean, has to be nice, so it's very readable format. So, whatever we have, it has to have very nice input and output. It shouldn't be even as an imperative programming. This is an example of non-functional. Everything's like, do this, do this, and then that, et cetera, et cetera. Say, imperative programming. What functional programming wants us to do is to be kind of clean. You have functions for every single thing you want to do. And have to be having input and output. This is not exactly pure functional. So, as a pure functional programming, we have to make sure there's no side effect. What I mean by side effect is like the previous example I had, printing function side, which is, I'm talking to, it's like I'm asking some other helper method to do something for me, which has nothing to do with the greeting. Because the greeting function wants to just append the hello words into the string. So, a more functional way would be greeting, accept the parameter, and return back in. It's very simple. Yes. Next is, we have to use a higher... No, we don't have to use, but we have the ability to use higher-order functions. Because, in suit, the functions are the first... We have to acknowledge them as the first class citizen. So, the functional programming. So, some of the system higher-order functions are in the array. It can be reduce, map, filter, flat map. I'll go to just to do a simple demo if anyone not familiar with this higher-order functions. So, we have our array. We can map through it instead of doing a normal for loop, which would be this one. This is a simple way. I want to map through every single array and add one into this array. So, if you look at it, it would be very nice. Two, four, five, six. And we have array. We can filter it to look for exactly all those numbers that are bigger. Bigger than five. Okay, six and seven. We also have array.reduce, which takes the initial value. You can say... You can put it plus as to combine them together. Sorry, initial value is... You have to put a number or any string you want to put. And here, as a plus to combine them. So, you can combine all the values to get an end result. And you just put a print. And last is flat map, which... If you have a multi-dimensional array, like an extra... Something like this, it's useful to use flat map to give you a 2D array. Okay, that's it. Anyway, it works sometimes. So, these are our higher-order functions, called as the first-class functions. Next is a very hot topic in the functional programming, our current function, which returns back a function. This function is the app which takes the input and returns back a function, which also requires another input of the integer. So, for instance, we have... We have our own variable, which is add2. And we add a number 2 and a result which can use add2 and pass another. And we can add 3 to pass a function over there and use it to pass each other and get it out too far. I guess I go to detail a little bit of this one. So, say you have a function... We call it current. We have the input integer, and it returns back a function. So, over here, I will go for a simple way, like return back a function. So, I would say return a function, which would take the input, call it b, and return back a integer. And over here, I can call return function. So, from outside, I can just call this one b2, add2. If I look at the adding, it's actually... This is a function. So, I cannot print it. I have to call adding and add another parameter into, like, 3. I'll each give it 5 over here. Or, you can actually use it like this, like that, right next to it. To intrigue. So, I can even make this one simpler. I can just put it, like, a plus b, 10... Sorry. I can make the whole thing even simpler, like this, in a very short format. A plus... This $0 is actually this function, which I haven't named. So, current can return back a function that would do something else for us. This can be useful when you are dealing with, like, a factory method that requires to do something first and use that factory method to add on more function into it. But, it depends. Your use case might not be very practical, but in functional programming, you talk about this a lot. I just want to show you how to do it in Swift. It doesn't really have to use it, but maybe it makes more sense in Haskell or other programming language. This is the query function. Next. Composite function. This function is actually accepting function as its parameters. It can be very useful, let's say you are... For example, indeed, I have a variable double, which you... Whatever parameter you are getting, which is $0, times 2, and you also have another function, add1, which $0 and plus 1, then the composite function can combine the double and add1, and it also has an additional parameter to the end. This function can combine everything together and return back the value to you. Let's go to the demo. We'll have a composite function. We'll call it function A and for simplicity, let's say this is a function here. You return it as its own function, which accept the parameter integer and return back the parameter. We have a function B, which accept the same integer and return back the integer parameter. I don't want this function to return back me... Just be very simple. Yeah, let's go like this first. So, parameter composite requires to have a function, and the function be exactly the same thing. And it will return back the function, which accept an integer and return back an integer to you. Sounds a little bit confusing, but just try to be as easy as possible. So, let's say function A, whatever result it gets, it needs to call function B, and function B requires to get the value which is getting from this one. This is called $0. Turn. That's it. We call it inside a closure. Yeah, but you have to call it as it's escaping. It's escaping. So, let's say I have a function. I would say whatever number I would give it to you, double, double function. And I can create the function, like an inline function as a variable. So, let me do it. I would say this function would be something like this. Then, that function, which whatever value it gets, times 2. I also have another function, plus 1. Whatever value you get, plus 1. So, let's put them together. Compose function. I would say my first function is double function. Yeah, double function. Double function. And my second function is a plus 1. Plus 1. So, this compose actually came back to another function, which requires to accept an integer. Which I would say, let's say 3. This would give you 8. Sorry, can't have it that way. Yeah. So, this function, first, it would go to function 8, which is double the number, and then it would go to function B, which is plus 1 the number. This is a little bit more advanced, but I can go through how to make this thing a little bit simpler in the next slide by functional training. The next important thing about functional programming is avoid mutability. In the suite world, it's very easy, as long as we're using the struct classes and we have to avoid using any sort of mutable variable. If you want to update a value from a rate, it's better to create a new one. Or it's also good to use a node tree. If you know it, it's more efficient than that. Keep on creating new array. And the last bit about functional programming which is also a very important method, which is called functional training. Imagine you have a function that is taking students as a parameter, and you want to look for find the last few students, and then from there you call the sort results, and then from there you want to call the sprint printing function, which would be the final result. It's kind of hard to look in this way, which is the custom operator is used for the suite. We can define our own custom operator, which would say this is a student, and then find the last student, and so on and so on, which is part of the functional composition of this lesson. Let me just do it before I go to conclusion. So, I have, so this is my array, and I have just a number as a sample. I have a function to sort array, remove duplicate, remove first, reverse array, and just for duplicate array. And this is how I call it. This is kind of similar to the one in the example. Reverse array, remove first, and remove duplicate and sort array. So, in order to make this one nicer, you have to go write our own precedence. Let's say I want to go for this kind of style. So, keep on going and going. So, my first one is, it's solved. I want to pass the array, and pass that one to the sort array, and then pass that one to remove duplicate, and pass remove duplicate to remove first, and remove first to sort. I feel this is more readable, but if you have a lot of custom operator, of course, it's going to be very confusing to look at who wrote this function first. So, let's write a custom operator. This is infix operator. So, you can say infix, pretty cool. Define it like this. Function, infix, function this. So, the first thing is the left variable, which I'm getting, I would call it just a value. It's a integer. Pretty simple. Then, next is a function, a custom function, I'll just call it fun. And this will get an array of integer, and returns an array of integer. So, this will take integer, and then also we have a function that knows how to handle this integer, and also returns a very nice integer for us. All right. In fact, if you don't mind, you can go for a generic function. If anyone is familiar with generic functions, if not, if I just continue this way. In our lost audience. Term, say, okay. Actually, there's no need for a generic function. So, this is a generic function, which integers accept a value, and have a function, which accepts the same kind of array of integer return back to integer, and that one also returns back to the value you want. So, if you go left to the right, I'll read it. This will pass the value, and then sort array as a function. And the return also going to be, and the return is going to be the left side for the right side. And so, I keep on continuing. Left side is going to be answered, and then add into the function to the right side until you get the result. That's all. And that's it for functional programming. It's confusing. I know I have too much advanced programming, but the thing is that, I don't think functional programming has any place in the suite at the moment, because it's not created with the suite, it's came without the languages. But at some point we can follow some of the good practices that functional programming brings, like the not query function. I don't think there's any good use case for us. But functional changing or how clean our code should be looked like in a function, have a clean, nice sync code and nice output, not any more side effects around it. And that's it. This is the source, if you guys are interested to find out more about functional programming suite. So, any questions? Yes. One of the things about functional programming languages is that they have efficient data structures for functional programming. They can do that closure, but they still have data structures that allow you to do sharing, for example. When you create new instances, when you create new instances, most of the structure is shared. So your memory usage doesn't go out of the roof. But with Swift, that is going to be quite painful because whenever you call a map or reduce, for example, you create a new instance. And when you have Arc in play here, then you have routine release, routine release, routine release, happening all the time for these objects. You don't actually have structure sharing. So wouldn't that make functional programming in general quite inefficient if you do it everywhere all the time? It depends what part of the functional programming you want to use. I would say only part of your app, not the whole thing you replace it with a functional program because it then makes it harder to read. And structure sharing, most of the part in the iOS update is already doing that. If you try to write your own link list for an array, believe the current Swift array is more efficient than writing your own link list to try to do the structure sharing for you. There's actually a benchmark for you. I was searching for the same structure sharing. I believe overall it's good to use partially not the whole thing of the app because I don't think it really belongs to this Swift program. Thank you very much.