Can I order? Do I need to use this one? Okay. So, hi everyone. And thanks guys. Thanks Manuel. Okay. So, here a brief introduction about myself. My name is Mohammad Amir and I'm from Pakistan. So, this is my first talk here. And I'm working at school as an iPhone developer for the past two years. So, here is a topic which I would like to present on is the functional reactive programming. So, before moving on, actually I would like to ask how many of you have been using reactive framework or familiar with the function programming concept? Okay. Have you guys used which framework like there? Because right now there are 32 reactive frameworks in Swift, which actually implement the FRD concepts. One is Rx Swift and second one is reactive folk. So, have you guys used any of them or you are familiar with some of the reactive folk? Reactive folk. Okay. That's perfect. So, actually, so today my idea is I'll be covering some of the basic concepts of functional programming and the reactive programming and my main focus would be on the reactive extension, which is Rx Swift. The reason I chose this extension because the best thing is this is an extension developed by Microsoft. And there is a community Rx community and the guys basically they have made record for more than 15 platforms like Rx Swift, Rx Java, BHP, HTML and there are like more than 15 platforms. So, the point is like if you're planning to write new apps like iOS, Android or web, so you can kind of choose this framework and you can apply the concepts like through all the platforms. So, okay, so the real challenge is these concepts are very simple to understand like everybody knows about the functional and maybe you guys are familiar with the reactive concepts. But when it comes to implementation in practice, so it's not an easy experience because it requires the change of mindset you have to think in a different way. It's kind of like you have to push your mind to think in a different paradigm. So, it's the kind of I would say it's a philosophy, it's a concept, you have to get used to it because we mostly the background basically myself I have come from the imperative background like I have been using the for-look style, AFS conditions and the states like for years and years. So, it took me a hard time just to grasp the basic concepts. So, today based on my experience I'm still learning value. So, I have learned in my talk like into two different sections. First, I'll cover some of the theoretical, the concepts of functional and reactive programming. That's the first part. And the second, I will try to focus on how we can implement these concepts in any reactive way. So, for that purpose I have made a small demo. It's a small end-to-end project. It's like a single page app in which just make an API call and just pay a list with by using MVVM architecture. So, I'll give you some idea like how you can make app by using these concepts. And importantly, like you will discuss about the unit testing stuff. So, yeah. Okay. So, I would start with the functional programming. So, I won't go into much detail for the coding part. I'll just cover the scratch surface of some of the basic concepts which can help you get started. So, what is functional programming? So, they were like the plenty of definitions on the app store like on the internet. Some people say like the functional programming is the style of programming in which you can pass down the clear years, name the expressions. You have to deal with the declarative code. You can make expressions. So, these are general definitions which we'll find out. But I think the most precise definition which I have found is like the functional programming is programming with the mathematical functions. So, all you have to do is like it's a pretty simple definition like you have to deal with the functions and you have to compose them together to get your desired output. So, that's the basic concept of functional programming. So, in this next slide, I would like to contrast like it's a very simple example to contrast between the imperative style and the functional style. So, if you see we have a for loop, right? And the for loop is it runs from one to ten and what it does is it's in each iteration it's adding value into a total, right? It total is something is a result expression which we are interested in. So, now there are two problems in the for loop. First, we are going to have states and we can't avoid it because we need some resulting value, right? And the second thing is the computation which is happening inside the for loop. There is no way you can pass down this computation to another function. For example, if I ask you like you can transform this result and you want to apply some operators like the map, flat maps and some other functional operators. So, there is no way like the flow is actually broken, right? So, one can argue that like why not we can create a function and we can put this piece of code there and then we can call this function and use this returning value as an equal to another function. So, yeah, we can do that but there's a problem. The problem is first your code gets fragmented, right? There would be fragments. That's the first problem. And the second problem is it's not, you can say, a kind of pure functional way, right? And it's kind of breaking the flow. So, if you look at the functional style, then the same thing we can do with this function and it's just doing the same thing. It's running like doing iteration from 1 to 10 and it gives you a result. But at this point, if you see it's a more declarative way and the good thing is that there are no side effects you're not dealing with any states and it's a more over-definition. It is using mathematical functions such as actually the essence of the function programming. So, these are the basic concepts in functional programming. Okay, so, I won't touch the monads and the monads, right? Because these concepts, I think it can better be explained in the context of Haskell. Haskell is actually a pure functional programming, right? So, I'll just go through some of the basic concepts like the pure functions, differential transparency, posterior function and the classicism and theory. So, let's start with the pure functions. Okay, so, let me say the function. What does pure functions mean? The pure functions mean like your function does not have any external dependencies. It does not have any side effects. So, what does it mean actually? It means like if you have a function that takes an input and it gives you an output and you are doing some computation inside, so your function cannot change any external state or any variables which are defined outside of the scope or it cannot be affected by any external state. So, that's the definition of pure functions. So, in pure functions, your output is always deterministic. Like what I mean is you can think of x square, you can think of the sine theta, sine 90, you can think of this one, cosine t is zero. So, we know the output actually. So, and also with the function, with the pure functions, you can create a lookup table like you can just input data and you can have a table out of it. And where does function move function just lookup the values and gives you data. So, these are the, okay. So, if you see, these are the simple concepts like in pure, this is a function which is simply adding some value to x. And in the first function, here, our computation is dependent on local variable which is not defined in the scope. So, this function is not pure function. Because let's say if you're passing five, then it could return 15. And if you change the x 15, then it would be 20. So, for same input, we will have different output which is actually relating the concept of pure function program. How we can make it pure, simply you can just pass it as a parameter of this function and then you can compute your other computations.  So, there is another interesting concept, this occurring. So, occurring is like if you have a function with multiple arguments, then you have to break them down into a single argument. So, what does it mean? It means that we have a function which takes x and y. So, why not break them down into two functions, f, x and g. And then compose them together so you can get the same result which you can get from function having multiple arguments. So, in the next slide, I will show you by occurring, by applying these concepts, what we are going to achieve in that. Okay. So, there is another property I would say for attribute of functional programming is the first order functions. So, first order function means your functions can be treated as a variable. So, I would like to explain this concept more conceptually. So, let's say like we have a function which takes an input as an integer and returns an integer. Right? And we have a variable x which can take an integer. So, functional programming says, or in Haskell language it says like the function which can take an integer as an input and return an integer as an input is the same like x. And what is x? x is data. So, function says your functions are like data. And data basically are like function. But if you think like in pure function, in imperative style of programming, we used to think that functions are something that creates data. But Haskell in functional programming says function does not only creates data, functions are like data. So, data functions, functions, they are the same thing. And I think we can reduce this concept from the pure function. Like there is another property in pure functions. If pure function is pure, and if your output is deterministic, then you can always replace the output with actual function. And we can also call this as a referential transparent. So, there is another concept of your functions. So, hybrid function means like you can pass this function as an argument to another function. And it can be returned as true. So, I think we have already used this functionality like we used to pass down completion handlers. So, it could be a good example. And there are some like other operators which you guys might have been using earlier, like to reduce filters. So, these are perfect examples of hybrid functions. So, what we have seen so far, function programming is not different. So, what we are doing is actually we are trying to wrap our logic into a self-contained functions. And what we are achieving from this, we are trying to create dependencies. We are going to have pure functions. We are going away from the side effects. So, in other words, with all these concepts, pure function, occurring, hybrid function, if you think like we are trying to, by having small data manipulations or to make it more granular as much as possible, we are creating a small building blocks of functions which can help us in composability and usability. For example, if you have a small component building block, which is totally independent, it does not have any side effects. So, you can always compose with other functions because it would be unaffected from the external state. So, by having these concepts, these concepts are somehow interlinked with each other. So, you are going to achieve composability, reusability, modularity and stateless. Stateless is the key. And when your program is stateless you are going to have less errors. And, of course, it gives you high level abstraction. Okay, so, for now I just gave you quick introduction about the functional programming. As I said, I will give you some coding examples. There are a lot of theories out there because my main agenda is to cover some of the functional vector concept and then we will jump into the coding part. We will see how we can apply this concept. So, reactive programming. So, okay, so the definition says reactive programming is programming with asynchronous data streams. So, in next slide, what I am going to do is I will be driving some of the, one of the core the concept of reactive programming which are observables. So, let's wait for some time. In the next couple of slides I will explain what does it mean, asynchronous data streams. So, in order to understand the reactive programming we need to understand few other concepts. So, this is actually I took this concept from Microsoft website. They have explained this. So, I think it is more easy for a human to grasp the concept. So, the concept is push versus pull model. So, the pull model says like in our apps which we usually develop. So, what we do is we deal with the data. We ask for data and then we do some processing.  So, if you think like this pattern is similar to I created a pattern of innumerable and innumerable. It is a for loop.  And then innumerable basically gives you the next value. So, if you think like in our pull model which we used to do like in our apps in imperative style of coding our application is active in the data retrieval process. We constantly call data in order to get some new information which we are interested in. And your application might be blocked while calling from the source because your app is synchronous. For example, if you are reading something from the UI text field or you are listening button clicks then you can do only one operation at the same time. Right? So, in reactive world the directive says there is a stream, there is a signal like there is a concept of observables. So, all you have to do is just to subscribe those observables and if there is any new change which comes in then a sync notification will be sent to you. So, it just like the concept is all you have to do is just subscribe sit back and then wait for the new change. All you have to do is just react to those changes. And this concept is like because it depends on you. For example if you are doing some UI related task, right?  So, nobody should tell you you have to do this operation. So, reactive process one subscribe, observe the signals and then react to changes if it is required. In this process since it's a synchronous model so your application is passive in the in the data retrieval process, right? And you don't pull from the data. Okay, so this reactive programming, this concept is on the left side we have enumerable enumerator and on the right side we have observer, observables and observer. So, these two we can relate with each other. I'll give you a simple example. This whole model is just like you visiting a library and checking out books and once you're done with the book then you will have to visit again, right? So, it's just like you interact, you do something yourself, right? Reactive models say you go to library, you register your interest in particular genre books and if something that matches your interest then they are automatically sent to you. All you have to do is just to kind of do something, right? So, you don't have to be active in that process, right? So, this is the cool concept. Okay, so this slide actually, this concept I took from Eric Major. I hope I'm pronouncing his name correctly. So, Eric is computer science scientist and he's considered one of the main founder of functional and directive principle concepts. He was working in Microsoft before but now I think he joined Facebook and he has retired really. So, these are the four, there are four essential effects in programming in any kind of programming. But in Swift, since Swift is not a pure functional programming, so we really don't care about effects. But in Haskell, if there are effects then we explicitly mention type IO anyways. Okay, so this I'm going to drive observable concepts right? So, let's say this first quadrant says like if you have a function and a synchronous wave you're calling that function and it returns t. t is basically a value and it could be a task, right? And if you're interested in manual use then it could return innumerable, it could return I to it, right? So, let's think about an async. When we say, when we are in the reactive world, right? So, and when we are in the reactive world we get some value. So, in reactive there is always latency involved because in reactive what we do is we make an API call closer network and we wait for it or we are doing some long running tasks like in the background and we get response up to some time. So, we call it feature of t because this event, this value, this signal we will get up to some time because this is a synchronous work, right? So, now let's say if we are interested in many values so it could return us observable. So, what are observables? Observables are asynchronous data streams and each event, each value in an observable corresponds to a feature of t. You understand the concept, right? So, it's nothing like when I started reactive programming I just explained the definition. So, definition was programming is a programming with asynchronous data streams. So, it's not different from the array you can say. But the concept is arrays at time zero and observables are at different times like over a period of time. You can get signal at t0, t1, t2, t3 and so on. And this is asynchronous and just imagine like if we are reactive programming and we have observables then we can do a lot of operations simultaneously without blocking our app or your. So, the main thing is when you're programming and when you're doing reactive programming so everything is a stream, everything is an observable, everything is a data stream, asynchronous data stream.  Like, even button tabs you're typing on a text field, everything you have to think in observable terms. And reactive arrays is a very powerful framework. It almost gives you all the functionality which you can do in imperative programming. Okay, so observable now when we say that we have a data stream which can be observed then the component who observed observable is called observer. So, now how we can connect between observer and observable. So, we can connect these two components when you subscribe to it. So, let's say we have an observable of integers. We have an observable of like strings which is coming from like text field or from any source. So, how we can observe you have to subscribe and then you will start getting events. So, in ARIX there are three type of events. Three possible states. Next error completed. Next is whenever there is a new element omitted then you will, it's kind of the call back or the call here. So, it will be in what and you can do processing maybe if you make a call like to fetch a list of movies or to fetch some events from the server then you can reload it or you can do all the operations. Error is something, if there is an error then your observable will terminate, right? And the completed is like when your observable ends normally. Okay, so now we understand like what are observables and how we can and what is observable, right? So, this pattern is nothing as I said like you just have to think in a different way. So, we have a data stream which can be observed, right? So, now when we say we have a data stream, right? So, most of the time we have to omit some value as well like we want to give some input, right? We want to omit some value in an observable dynamically, right? So, how it can be possible? Since as I said like observables can only be observed. So, Rx2 exposes or introduces a subject concept. Subjects is a special type of observable sequence which can act as a observer and observable at the same time. So, what does it mean? You can just think of it can act as an input as an output. So, you can observe or you can become an observer or you can omit value as well. I'll give you a very simple example. For example, we use smart phones, right? Just think about our phones are observables and we are kind of automatically subscribed to it. We get signals. I mean, automatically we get signals, right? So, but if you think like this smart phone gets signals from the BDS station. So, for BDS station this phone act as an observer and BDS is observables, right? But for us this phone act as observables and we are observable. So, this phone which is an intermediate which connects to Wi-Fi gets signals and then it returns signal back to us act as a act as a subject. So, this is like a practical example we can think of what are subjects. So, there are four different types of subjects. Public subject and okay. So, a public subject is something when you subscribe then you will start getting events. So, in other words you can say that you will start getting events subsequent to the subscription. If you are subscribing here, then you will get these events. This is original observable. If you are subscribing at this point then since these two events already have been omitted so you won't get these two events and you will get events this two months. Similarly, like if you are subscribing here at the end and if there is some error occurs then you will get error event. So, behaviour subject behaviour subject is type of observable which can give you the last omitted value. For example, if you are emitting 10 values then and you are subscribing at 11th, then you will get the 10th one and subsequent events. For example, you can this quickly if you are subscribing here and that was the last one when you will start getting all the events and you are subscribing here. This is the last one and this would be observable streams and you will get these on next events. And the same thing like if you are subscribing here since your stream was terminated somehow with error then you will get since the last one was error and then you will get even the error. The replay subject So, sometimes you are interested in getting all the previous omitted cent events Actually, since I have been using this, I haven't used replay subject. Most of the time I deal with public subject or behaviour subject but this is like if you want based on your situation or on the feature like so when you subscribe you will get all the list of omitted events which have been omitted before sometime in the past. So, by this like if you are subscribing here you will get green and blue. Okay, so these are the operators which are provided by Rx. There are many more actually I just listed few of them. But I can tell you hardly I have only used like 5 to 6 operators and these operators as I said they are very easy to understand like everyone can explain it but when you actually start implementing it then thinking in that way like which operators you have to use how you are going to compose these functions is the real challenge actually. So, the real challenge is thinking in reactive. I am emphasizing on this on this point because I mean this is what I struggle and I am still struggling and learning. So, I mean these are powerful operators like you can combine two signals like two different data streams by using concatenate, merge, zip for example if there are two observables you can combine with other observables and it give you like to like combine new sequence flat map map is basically of transform your signals observable into another observable and omit it and flatten the two observables and transmit as its own sequence so it can be observed again because it is a synchronous operator and the map is synchronous like it just transform. So, yeah just take it out like you can just go to reactive X it is a very active community I would say so they have like mentioned all the details like the operators which you can use so usually what I do is sometimes if I want to use any operator then I can go to RxMarble website where they have listed all the operators so I can just check the description of which operators like source me and meet my requirement so, yeah okay, so now the question is where the functional part come into play now these are the operators right and we understand the reactive part now we are going to use these operators in a functional way so that is why we need to understand that is why I just started with the function by giving you some concept like by having pure functions no states and having expressions in such a way so you don't have any side effects okay, so now we are done with the theoretical part now we are going to dive into the coding stuff so MVVM I am going to use MVVM here MVVM I found works pretty very well like with almost this reactive with any kind of reactive framework because it is a lightweight right and MVVM separates your business logic from the view controller so all you have to do is just put all the logic inside the view model and the view controller is oblivious towards happening logically like inside view model so you can think of view model X like a brain and your view controller becomes logic right so, okay before looking into the code I want to explain this concept actually I took this concept from the Kickstarter the guys basically they made iPhone app, iOS app by using reactive Coco but I have tried to use same concept in RX Swift though there is like a certain difference in RX Coco and RX Swift RX Swift is a pure functional programming so and there are like few operators which are not available or the functionalities which are missing in RX but anyhow we can use architecture point of view we can use the same concept and it's great actually you will find out if you use right architecture because we are going to deal with observables, observers if you don't use right architecture then you will end up in a mess like and you can't do unit testing so that's why I think at VVM it's best so one thing you have to think view model in terms of inputs and outputs you have to clearly define your inputs inputs are those actions which can be triggered by user or from the US stuff right from any UI interaction output is something which is a feedback like for example there is an input we do some processing and we want to give an output output is basically observed in view controller or in views right so and also I would suggest like if there are some like a series of events like if you are doing some you are making APA calls and there are some subsequent APA calls then it's better like if you also break them down break them down into multiple inputs because it can really help you in unit testing you have multiple inputs and then you can test your output and with the mock objects which I will explain okay so this is a simple demo app actually I made for this presentation so here I am using simple APA call to fetch a list of movies and it's an open source APA call so first I am going to show you how view model will look like so first every view whether it's a view controller or it's a views you have to make a view model separately for all of the views it's a best practice because sometimes for example if you are using table view and you are going to have cells then you might have to do some complex logic in cell so let's have view model for cells as well so I have separate like view model one is for the view controller and second is for the cell view model so if you see I am since these are my inputs I am using protocols these are input protocols right now my input method is view load because when the view load so I want to make an APA call right so instead of usually in normal way what we do is we make an APA call in the view data we call it function right but now since it's an MVVM so we have created an input and we are calling it from view load of view controller these are as I said our inputs would be public subject because we have to trigger something we will compose function and then we will use public subject to compose the flow or functions composition so these are outputs outputs are observables which can be observed in view controller so these are outputs I am using so just for the sake of demo or for testing I am using show indicator view movie list high indicator view ok so now if you see like I am using this model type the reason why I am doing this because I just want to expose inputs and outputs in my view controller I don't want to expose all the other methods so just like your protocol just putting them in another protocol so now we have a class this and we have to conform this protocol and when you conform this protocol the compiler will ask you to create the structure and to find out the properties and the methods which you have explained in protocol so ok so if you see like this is our function view reviewer and this is the public subject which I have created so the naming convention which I have used it if my function is view reviewer then my my public subject with the property would be which is the local it would be view reviewer property so we can use this because when we go for complex views we are going to have a lot of inputs so we have to use some naming convention as well ok so right now this public subject you can say distributed tab or I just want to invoke a certain action so if you now here this is the point this is the core logic so what I am doing is whenever we work this function this public subject so now at this point I have to make an AP call right so I am flattening this right because as I said like flattening I am going to transform to another observable and I have to observe that observable as its own sequence so here I have used this AP call there so it will return if I go to my AP manager then I will show you ok so for the APS actually I have used this spectrum it's a program the service type the best thing is like if my service manager is conformed to this type so if you use this method then you can create a mocking manager aspect so you can mock all the APS response everything else so this is my manager and if you see what I am doing is I am calling this function search movie this is what is being called from the view mode and this is my generic method request which makes an AP call and actually I am using RASalamo file because I didn't find it interesting like I just wanted to use simple memo file I am creating my own observable this is the way how you can create observable ok one thing I just forgot to explain there are two types of observable HOT observable and CODE observable this is CODE observable it means like it can only start emitting events of when there is any observable and the HOT observables will start emitting events even though there were no observables or no subscribers so since this is an AP call so I have to wait until I get the response and then we will pass it to the next step so ok so in success actually I am using this method on next this is my response right and this is my error if there is some error like you get 500 or 404 then we will call this ok if you go back so I didn't explain this materialize materialize is another convenience operator actually it is not part of the actual reactive extension so the community they have made IS Swift extension they just mean some convenience so you can use them so what does it mean what does it do materialize ok so materialize is doing nothing it just transforming your omitted events and all the notifications like when you say notification I mean error notification or completed notification so it is converting all your notification plus omitted events into as a omitted events so if you see like we have this event then if there is an error occurs or there is a completion then this materialize will treat it as an omitted so your observable won't terminate so this is I want like if there is an error then I want to get an error and I want to pass it to output observable otherwise there is no way you will have to subscribe and it is better like it is a good practice to avoid subscribing in view model description should happen on view control so if you see I am using materializing because I need to get response and error if there is any error occurs so ok if there is any error then I am mapping and I am getting localization format because I want to pass it to my output observable so if you can just look around like here at this point we have you are not using any types you are not using any variables these are expressions which we are making and what is the triggering point is the u d load property so we have expressions in a declarative form so we are just triggering one point and then the signals will start from for example at this point whenever we get response so what I am doing is I am doing that I have to hide indicator view right so I have just for the sake of testing I have made an output observable now I need to set some even there is a success file so I am flat mapping and I am just passing empty observable so it can be important in view control so the same concept I quickly go through my view model so this spectrum will remain the same throughout in your app like wherever you have to follow the same logic it is nothing like you just have to get used to it and have to follow this spectrum it is the same for example for each cell this configure is like the way we used to do for example when the cell for index path we send a specific object data to the cell so rather than holding cell in the view since we have view model so I am creating an input which is actually a public subject and since my movie my data type is moving it is my model first so I am just creating this in on next way right and these are my observables since on cell I have to show the title image date and overview so these are the things I want to display in cell so I have to make it an observable right and if you see I am just writing expressions like I am not doing anything else like whenever this we get an event in configure property then automatically these four events would be more and since we are listening this observable self is a title this is this is this is the time is over already okay okay so if you see like in view cell actually we are binding all the observables here right and same for the cell this is the place where we are binding right so quickly I will show you since I am running out of time actually so let me okay these are my this is my mocking manager service manager this conforms to service type and then in view model so this is how I am doing testing so one concept I just want to explain so I am going to use environment so the concept of environment is like for example when our app gets bigger right so we are going to have a lot of dependencies it's better like to put dependencies in some containment so you can have an access from any place so you can either inject this environment directly into the view model or you can specifically like choose which dependency you need to inject so this spectrum would be helpful in mocking like if your view model requires all the dependencies then you can mock your complete environment class so that's the best thing you can get from environment so yeah so just have a look maybe I will upload this code on github so we can just go through it so here the only thing I just want to explain is right now which I am still working on our our test cases are not stateless right because there is no way like I don't have any test of server from where I can get the output value so that's why I am subscribing here and I need to have some local variables so based on that I can decide like whether this is expected outcome or not but this is not actually in Rx Java in Rx Coco they have a test observer wrapper but I am still trying to maybe I'll write my own wrapper or I haven't found anything related to any test observe there are some other testing extensions which you can check out Rx blocking so these are the the testing extensions which I have explained Rx test are I won't explain because it might take some more time so but you can just have a look how it can make your life easier like if you use this otherwise you can simply subscribe and you can test your cases by setting values so these are the links I found helpful you can go through all of them they have a slack channel and it's very active so just one thing whenever you ask a question don't mention anyone's name because I did one mistake I mentioned someone and the guy he freaked out and he came back to me and said well you are mentioning my name I am not your personal assistant so because at that time it was critical and I had my release so I specifically asked the man to help me so we have to be careful yeah this is it yeah I hope if you have any questions yeah