 Excellent. Thank you. At any point, if you find that it's getting smaller, just let me know, and I'll increase it. Welcome to the session on thinking in functional style. Actually, this title actually is wrong. I'm going to actually thinking in functional style and programming in functional style. I'm going to actually talk about, actually I pulled the wrong slides actually here, or set of content. Let me go fix it. So what I'm going to talk about here is a little bit about changing the way we think about things programming. We've been programming with object-oriented technology for quite a while, and then something else that's kind of evolving as we are looking at things. And this can give us a certain amount of benefits. I want to show you a few things. Normally, when I give this talk, I use multiple different languages. And the reason I normally use multiple different languages is mainly because mainstream languages quite could not do a lot of these things. But that's changed quite a bit. C Sharp has started doing this quite effectively over the past about a few years, maybe about three, four years, maybe five years, not as much, I guess. And then, of course, Java is just about beginning to do this. So as a result, I kind of changed my presentation just this morning so that I can give this entire presentation just in Java. If you don't mind that. Anybody minds that? All right then. So we're going to talk about quite a few things. Best time to ask a question or make a comment is you when you have it. So if you do have a question, keep it extremely short. I'll cut you off if you take too long. Go ahead, please. Java 80, that's correct. Thanks for clarifying that. This is something you can download and use it today, as long as you are willing to know that this is going to change quite drastically in the next several months as they are putting their finishing touches on it. Excellent. So yes, that's correct. Functional programming, I'm going to use Java as a tool because it's a style of programming. We could talk about purity and all that, but I'll get to the details. Let's get started. First of all, the question is why should we care about functional programming? One reason why we should care about it is everybody around us keeps talking about it. So what the heck is it? So that's a good thing to know about. Another reason to care about this is it is something that's been around for a very long time. But what's really happened very recently in time is back in about 2003 timeframe, this engineer went to his boss and said, this computer ran really fast before the chip melted. And so they realized they could not put more into this and the packaging could not continue. And they had to go back and produce multi-core processors. So because they had to create multi-core processors, as a result, multi-threading became quite viable. But the problem is this. We've been writing code for a while, but we really have not taken the ability or the time to create concurrent code that is safe. But it turns out functional programming inherently is thread safe because fundamentally it removes quite a number of problems that exist in imperative style of coding. So let's talk about what is functional programming, because we are talking about why we want to do it. It is a programming style. Again, talking about why, you can write less code, but you can write code with fewer errors. Now who doesn't want to write code with fewer errors? So you can write less code. You can write code with fewer error. It's also easier to paralyze this code. I'm really hoping I'll be able to get to that part by the end of the session, assuming that we can keep up the pace. So what are the things that functional programming really provides for us? One of the things it provides for us is assignmentless programming. Now you say, wait a second. Are you crazy? How could you write code with new assignments? Well, remember what structured programming is all about. We said go-to's are evil. Every time anybody even thinks of go-to, good old Dijkstra moves in his grave. You don't want to use go-to in your code. But in fact, go-to is a forbidden keyword in Java. You cannot even use it. But does that mean that there is no jumps in the code? There's no go-to in the code? The go-to happens all the time, except we don't put go-to's in the code. In a very structured, we write the code, and then the go-to happens under the covers. In a similar way in functional programming, we have assignmentless programming. Meaning we don't mutate the variables. The mutations can happen in a controlled manner under the covers. If you want to know a good word for it, we call it encapsulation. So you would say, hey, I'm looking at this library. It is doing mutation under the covers. I don't care. It's encapsulated. They take it with responsibility. We don't encap mutate it. That's kind of the point. And the second thing in functional programming is moving towards immutable state. And basically, you're not going to be modifying objects. Instead, you're going to be treating them immutable. And you're going to transform the state from one object to other. Yes, you can have concerns about performance, but there are other ways to eliminate the performance concerns. We won't go into that right now. But if we have time, we can talk about it later on. The third thing that about functional programming is that it has no side effects. And the beauty of not having side effects is that you're not going to really be mutating stuff. It becomes a pure function. It's easier to resend these functions. And as a result, it becomes a lot easier to optimize the code during execution for concurrency and parallelism and so on. But more essential in this case, the essence of functional programming also is that you create with higher order functions. So what is a higher order function? A higher order function simply says three things. Remember how you can create objects within functions? And remember how you can return objects from functions? Remember how you can pass objects to functions? In functional programming, you can do to functions what we have been doing to objects. In other words, you can create functions within functions. You can return function from functions. And you can pass functions to functions as well. In fact, the functions have raised themselves to the level to say, it's OK. I'm not going to stick to you just dealing with objects. I'll work with functions as well so they're higher order in nature. So that is basically what a functional style of programming provides for us. That's enough talking. Let's get to the code and take a look at some examples of this. How would we do this? So I'm going to total a bunch of values. And I want to total these values in an imperative style, first of all. So let's say integer, and I'm going to say, for example, numbers in this case equals to, let's say, arrays a bunch of numbers as list. And let's come up with a bunch of number values here. And I want to go ahead and total these values. Now, how would I be able to total values in a collection? Well, we all know how to do that in an imperative style. So what is imperative style? Imperative style is where you tell it every step of the way exactly how it should do things. So you would say, for int i equals 0, i less than numbers dot size. Is it size or length? Is it less than or less than or equal to? You know what this is called? This pattern is called self-inflicted wound pattern. There is absolutely no reason to suffer through that. The programming has to be much more elegant. You don't want to go through those micro-decisions, so we won't do that. So what do we do in Java before this? We say number, and then we could say numbers. This is the 4e syntax that Java provides. And then we would say total plus equals to the number that we want to add. And of course, here we could say, for example, int total and equal to 0 to begin with, obviously. And then when we want to total this value, we could simply come down here, and then we could say what the value of the total is going to be. So we could say, for example, total, and then we could simply print the value of total. So this is imperative style, but why? This is Java code purely, but this is imperative Java code. What is it doing? You're first telling it, hey, got a collection of data. First define a total value, done. Now go through every element in the collection. All right, what do I do now? Take the value and add to this total value. So this is purely imperative. And how do you smell imperative? It's a smell. You smell imperative by looking at two things. One, you're going to be mutating a variable over and over and over. If you put your ears to it, you'll hear the variable total keeps saying ouch, ouch, ouch. Because you're constantly updating it, right? And then, of course, you're telling it what to do. Repeatedly, you're updating it and giving instructions to it. So this is an imperative style of programming. Now what would a functional style of programming do? You use a declarative way of doing things. What is declarative way of doing things? It is just to tell it what you want and let it figure out what should be done. What are we trying to do really here? We're trying to total the values, aren't we? So why don't we come down to this and say, I want to say total, obviously. That's what I'm trying to do here. But why don't we, instead of going through such a process, simply say numbers.sum and ask him to really total the values? He says, I don't find what sum is. What are you talking about? Well, we're going to go to something called a stream and ask him to do a stream on it. So what is a stream, basically? Well, a stream, basically, is a new in Java 8. This gives you a iterator. Well, we are used to iterators in Java, obviously, right? But imagine this to be not just a simple iterator which has a move next and get next and stuff like that. Imagine this to be an iterator with fancy set of functions that you can use. So this iterator can give you quite a number of flavors of methods to do. So in this case, I'm going to say, give me a stream of objects. And then once you give me a stream of objects, I want to come back to it and call some methods on it. Like, for example, sum or total the values in the collection. Let me see if that's even happier to use it. Cannot find the symbol, it says. So I'm obviously having some trouble with it. But let's scale back. We could do something else a little different. Maybe this is actually better because we can learn something from how this is going to really work. So I'm actually getting a stream. And I'm going to say, what am I trying to really do? I'm really taking each of these values. And I want to go through a totaling of these values and do a collection. In other words, we are trying to perform a reduced operation. That's what we are doing, right? So let's imagine how this would work. Now, we said we're going to total everybody the values, but without mutation. You're going to sit there and say, how in the world can you find a total without really doing mutability? How in the world can you do that? Well, let's take an experiment for that. Imagine for a minute, I want to total the age of everybody in this room. Let's think about that for a minute, right? And I'm going to start with you. He's not smiling anymore. No, I'm not going to ask for your real age, right? So I'm not going to ask for your real age. But there's a rule. I'm going to pass around a post-it note. But there's a rule about this post-it note. This post-it note is a write once post-it note. You can write on it once. But once you write on it, you cannot erase it. You cannot rewrite on it. You cannot overwrite on it. So on this post-it note, I put a zero, first of all. And I gave it to him. And what is he going to do? He's going to take his age and total to the value zero, right? But he cannot change this post-it note. What can he do? Take another post-it note. Absolutely. He takes another post-it note. What does he do? He gives it to him. What's your name, sir? Taj. And what does he do? He totals his age to the number that was given. And what does he do? He passes it down to her, correct? What does she do? She takes her age, subtracts 10 from it, right? And then totals that number and passes it down, right? Makes sense, correct? And if we continue with this way in the back over there, the last person giving us the post-it note would give us the age of everybody in the room, agreed? But how many post-it notes did we torture? Did we mutate? Not a single one. Because we keep creating new post-it notes. You say, oh my god, but you created all these post-it notes. But imagine this. One of the things we have really nicely in Java and C Sharp and all these languages is what? Automatic garbage collection. You know, call me silly, but other day I was programming. And it suddenly dawned on me. How would it feel like doing this in C++, where you have to keep calling delete? That would suck, isn't it? So we can leverage that. So absolutely we can get this total at the end. So what am I going to do? I'm going to say reduce. And to the reduce, what am I going to do? I'm going to start with the value 0. What is the value 0? We just agreed that I'm going to give him the value 0, right? And then what are we going to do? We're going to pass a function over here to this. And what does this function do? The function says, I'm going to take an element value, which is the age value, if you will. And I'm going to have a carry over value that you give me. And I'm going to return from here an element plus the carry over value. So just take a look at this code for a second. What are we telling this particular code? We are telling this code. I'm going to give you a reduce method. And I'm going to say, take this element value, take this carry over, and return the element plus carry over. In other words, all that I explained in English to you is a reduced operation. I'm reducing everybody's age in this room to a total value, which would either be a value or overflow, looking at all the people with gray hair on the room. Just kidding. I've got quite enough of that myself. So basically, the idea really is we're performing a reduced operation. Now, what did we do, though, in this particular context? We did not sit there and tell him every step of what to do. Instead, we said, hey, why don't you just perform a reduction operation? And what did we send to this function? We sent an object, 0, as the first parameter. But the second parameter we sent to the reduce method is not an object, but we sent a function itself. So what is a function? Most of us would say a function has four things. A function has a name, a function has a parameter list, a function has a return type, and a function has a body. So a name, a return type, a parameter list, and a body. Of the four things, which is the most important thing. Thank you, body of the function, right? Absolutely. Hey, where's the body of this function? In what I've highlighted, the body of the function is the right of the arrow. All right, one down, three more to go. Where's the parameter list? That's to the left of the arrow. Hey, wait a second. But parameter list normally has the types of the parameters and the symbolic names we give for the parameters. Yes, that's true. So if we really, really, really, really wanted to, we could go back here and say integer or integer, depending on what you want to do, and you could do integer. But the Java compiler says, eh, you don't have to give it. And when the Java compiler doesn't want it and you still give it, that is called what? Ceremony, right? So you don't have to go through the ceremony. It's just perfectly fine. And it infers the type automatically. So two down, two more to go. Hey, what about the return type? The return type is inferred by the Java compiler as well. And if you return something that's not correct, it'll give you an error. And of course, finally, the name of the function, hey, who cares when everything is done what the name is, right? It's working. So this is an anonymous function that we are creating. Well, we are creating and passing this function down to the reduce method. Sounds good so far? Now, this is an imperative style to functional style. But we will take a look at more examples of this along the way and how this works. So I not only showed you the imperative style, but also the functional style of doing this. I want to iterate over the collection of values. How do we do that? We already kind of did that. But let's kind of look at this one more time. What we're going to do here, let's kind of go through the exercise of doing this. We could say far int i equal to 0, i less than, we could say, numbers dot size and then i plus plus. And then we could simply output the value. For example, in this case, we would say, numbers dot, actually this is an illicit, right? Get i. And notice the effort we have to put to go through this. And in fact, I can't even write this properly, right? That's a lot of effort to write this. So this is a traditional effort. It's very noisy, as you can see, right? And I apparently put, you know, I've got to check if I put the right parentheses or not. Why go through it? That's the older Java version. We could instead go through this one more time and say far, and we could say int number. And then it is less noise, as you can see. And then we could say numbers. And then we could simply say output and simply print the number itself. This is a lot more civilized way of doing things. I think I'm missing a little parentheses down here. There we go, the curly bracket. There we go. So that is basically much more civilized. But we don't even have to do that in the recent version, or the next version, which is more, you know, a functional or declarative, rather. We could simply say numbers dot. And then we could say for each. So what are we doing in this particular case? We are telling him, rather than me telling you how to really go iterate through, I don't have time for that, right? That's what you're saying as a programmer. My job is to get things done, not to sit there and tell this every step of the way. So you say, hey, here is what I want you to do for each element. You go figure out how to loop through it. Now you say, what's the benefit? Well, there are two benefits of this approach. The first benefit is you don't have to tell in the silly stuff. But the second benefit of that is because you're not telling him the silly stuff, the code under the covers can decide how to do it. It could do it sequentially in some cases, or it could do it concurrently or parallely in other cases. So by encapsulating that below, we get a lot better power on our hand, right? So by not doing things, you not only eliminate the effort of doing it, but you give the opportunity for somebody to vary the way they do it too. So less control actually gets more functionality for us, isn't it? So what am I going to do here? I'm going to say, in this case, integer over here, because I want to take an int value. And I'll say number. And what do I want to do with this number? I'm going to simply output this and say number and ask him to print it. So this is basically a number we are receiving. And I'm asking him to go through the number. And I'm asking him to print this value out for us. That's what we are doing. So there you go, it printed it. But of course, this appears a bit noisy as well. But it's far less work than what we saw before. But we could say, you know what? I really don't want to specify the type. That is kind of inferred. So you're reducing the noise quite a bit, as you can see. But Java 8 actually gives you a little bit more capability than this. If you look at this code, what are you really doing? You're saying, receive a number on my left hand and pass it down to this function on the right hand. And that is also known as doing stupid things. Because that's so obvious what we are doing. We're receiving a parameter and we are sending it. So Java 8 says, if that's what you do, don't write that much code. So we can simply remove this number from here. And we can simply replace this with a double colon. And this basically is called a method reference. And then you can get to that point as well. So this is basically going towards a more declarative style, removing the noise. There's a very good saying that says perfection is achieved not when there is something else to add, but when there is nothing more to remove. You bring it down, strip it down to the very bare minimal. And all that you have is essential code in front of you that does the work, and all this clutter and ceremonies that distract us. So that is an example of a declarative style of coding where we are using a lambda expression plus a combination moving on to a method reference that we are using. So it gives us clarity, it gives us focus. But like I said, don't discard the other benefit, which is we don't have a clue how far each is implemented. And as a result, it can be implemented in one of several ways, depending on what we are trying to do. Excellent. So we looked at some of these combinations of things. We looked at what Java 5 can do. And we also looked at what Java 8 can do. I want to double the values in a collection. And again, how would you do this in a more of an imperative style? Well, if you want to go loop through these again, you would want to double these values. Or how would you go about doubling these values and then print it? Once again, you know how you would do this in the traditional style. You would say, create me an integer, and you would call it doubled equals new array list of integers. Well, thankfully in Java 7, you don't have to put the type over here because you can reduce that noise. So that is Java 7 syntax already. Then you could say int number in numbers, basically. And then in this case, you can simply say, now that I've given the numbers, you can say double dot add, and you can say number times two. And then of course, when we are done with this, we could say, print me the doubled value for me in this particular case. Well, again, we could go through all these steps. Now, what do you think of writing code like this? How do you feel after writing code like this? Not very good. This is very low level right. And if you're working from home, this is dangerous. Because as you're writing this code, if your kid runs into the room, immediately you have to shut the monitor. Because otherwise, the kid looks at this and says, that's what you do for a living. And then no longer they won't take up our profession. So we've got to be very careful what we show to the kids. So obviously, they say, you are writing intelligent code, not kind of silly code at a low level. There's another name for this. It's called primitive obsession. We are sitting there at a very low level writing code. We don't have to do that. So instead, what we could do is we could simply say, I want to take these numbers that you give me on my hand. And I'm going to say, give me these numbers. And what do I want to do? I'm going to go to the stream, which is the fancy iterator I talked about. And then I say, do a map. Now, what is a map function? Notice what we are doing. We are transforming from a collection of numbers to a collection of double the numbers. Where is mutability in this case? That's not our concern. You have an input collection coming in, and you've got an output collection going out. That's all you care about, right? So this is purely a transformation. If you put a box around it, this is a multiply operator, and a bunch of input come in, and a bunch of output go out. And that is all you care about. So you pretty much focus on the logic you want to implement, rather than the mechanics of how do you loop through? Where do you multiply? Where do you stick it back in? All the details of implementation can be totally removed from your site. So the idea, really, is to say map. And what is the mapping I want to do? Given a number, I want to map it to a number times 2. That is the mapping that I want to perform. That is the lambda expression we are sending. So map is a function. But what we are sending to the map function itself is the value of the function that performs the multiplication operation in a sense. So rather than, again, focusing on the mechanics of looping and doing the work, you're just telling them what you want to achieve. And you are only providing what you can provide. Who here knows what you're trying to do? You, right? You know that you want to multiply or subtract or you want to square it, but everything else can be taken care by the system under the covers. Now that I have a collection of these values, I want to print it. So in this particular case, I'm going to simply say, given a number, I want to transform it. So this number is the doubled number. And I'm going to simply say, take an integer dot 2 string value and then convert it to a number, right? We're saying convert it. And at the end of this, I'm going to collect it back to what? To a list. So I'm going to say to a list. So you started out with a collection. And then you are coming back into a collection. So I'll explain what this means in just a second. Of course, for me to use this, I need a few more things to import. So I'm going to import Java dot util dot stream dot star. And I'm actually going to bring a bunch of static methods in here as well from a collector. So I'm going to say static. And I'm going to say collectors. And let's go ahead and use that. So what am I doing in this particular case? Just brought a bunch of import. Let's make sure this is working before we continue any further. The output is the same, but what did we just do? We simply said, given us numbers, get me a stream. Remember what stream is? It's a very fancy iterator that you got. And then on the iterator you said, transform the collection of numbers to the double of those numbers. That's what you said. Then you said, well, I have these numbers. I can then further transform that to a collection of strings if I wanted to. But the point really is you could go on and do more work. You can do any number of work. This is like saying, I can give you something. You can do part of the work and then pass it on to him to do more work. And then you can continue until you say, we are done. And you can get the result and bring it back. And then finally I say, I've gone through the sequence. I no longer need the sequence anymore. I don't need this iterator, but I want a collection back on my hand. So you're collecting the end results into a list one more time, and you could then go on process other things in the list if you want to. So this is all intermediate operations you're performing along the way to get the results that you are interested in. So we looked at more of a functional style of doing this as well. So we are slowly marching on into another interesting thing we can do, which is function composition. A function composition is a very powerful way of doing things. The beauty of function composition is, how does the business workflow look like in problems we solve? Normally the problem statement says, take this particular information, do this operation with it. Get the customer's invoice, process all the entries, validate each of the invoice entries, send it to this particular system for verification of the particular end and on and on and on. So we take all these requirements. And then what do we do? Then we unleash the programmers on it. And then they go write the code, and then say we are done, and you open the code, and there's like 1,000 lines of code sitting in there. And you say, what do you think? Does it work? Is this what you wanted? And nobody can read it anymore. Not even the programmers who wrote it can't read it anymore. This is one of the things that fascinates me. When Java came out, we were saying, how do you upfuscate this? I always ask, why do you want upfuscation tools? Java programmers do a good job already, right? Nobody can understand the code. So it really becomes hard to understand this code. It's all cluttered up, right? Well, what if the code kind of reads like the problem statement? And you're looking at the code and saying, yep, yep, yep, yep, that's exactly what I wanted to do. Now you can have people look at this and say, eh, not quite. Or they come back to you and say, no, we want to insert something right in the middle. Not a problem. You insert it right there in the middle. And it gives you, this by the way in good design, this is called the difference between opacity and transparency. Opacity is where you look at the code and your head hurts, right? And you cannot see through it. Transparency is where it's very clear, it's obvious. So you're able to look at it and say, yep, I get it, this is what the code is doing. So that is basically where you can lead with function composition. How would we benefit from this? Let's take a look at an example of how we would do this. So here's the problem I'm gonna take. Find the total of even numbers in a list. Actually let's make it even better. This is one of the advantages of changing the top on the line, right? So find the total of, find the total of double of even numbers in a list. How about that? That's even a little bit more in terms of flow. So I wanna find a total, all right? But what? Of double of some numbers. But what numbers? Only even numbers in a certain number collection. So how would we do this in imperative style? Well, we know how to do that in imperative style already, right? You would loop through it and maybe in one shot you would check if it is an even number. And then you would say, well, actually it's kinda right at least a pseudo code for it if you wanna kinda look at how that would look like. So what would you do? You would, first of all, you would write one loop, like we already have a loop here, we could do this, right? So what would you do in this code? You would simply say, for each number, so you would first define a total, right? So it's a int total equals zero to begin with. So you got the total value on your hand. Then you would say, if number is even, so you could say number pound two equal to zero. Well, if it is, what would you do? Then you would say total plus equals number times two. Well, that's what you would do, right? And then when you're done with it, you would actually print it out. So that's a total value, assuming I did it right, it says 24. And you look at this code and say, hey, that is simple. Well, okay, that is very arguable if this is simple. Why is this arguable? And the reason is, oftentimes we confuse the word simple to some other word we really mean to use. That word is called familiar, right? This is not a simple code, this is a familiar code. But what is familiarity? Familiarity is what you are used to, right? I may be familiar with something, and you may share that. We both are familiar with it. But on the other hand, he may say, I'm not familiar with that, I'm familiar with something else. So now is it simple? Well, that's still subjective and questionable, right? So this is the code that's familiar. But why is it familiar? It is familiar because we all have done this for a long time. So don't confuse the word simplicity with familiarity. In fact, I would argue this code is really not simple. It is actually hiding behind certain things. Now look at this for a second. What are we doing? We first of all took a total value, and then we loop through it. Then we check if the number is even, then we double the value, then we total it. Now what is the, so when you describe this, how many times do you use the word end and then? And if you do all of that, what are you doing? You are violating the so-called principle called the single responsibility principle. This piece of code has multiple responsibilities. It's got the responsibility to total, it's got the responsibility to double, it's got the responsibility to find if it's even all that clubbed in. Now you come back to this code and you look at this, will you be able to understand this code, right? Now, what about understandability of code? The worst person to read a code is the person who writes it, would you agree? Because you write it, you know everything, you look at it, and you always understand it. But how do you normally feel when you look at somebody else's code in your company? You come and sit in the desk, you open this code, and you didn't write this code, you look at this, and you have a thousand self-complaints about it, right? You start saying, I don't like this, I don't like this, I don't like this. In fact, this even happens when you keep saying this until you realize this is actually the code you wrote and don't remember. Now it doesn't look that bad anymore, right? So you kind of get that feeling. But what if this is actually clear, you can read it, you can understand it. So let's see how we could do this in a more of a declarative style, in a functional style. So notice what I'm gonna do. I'm gonna go back to this code and say, I wanna output. Well, what do you wanna output? I wanna output a total of values, that's what I wanna output. From what? I wanna take numbers first of all, and I wanna get all the even numbers first, right? So stream, and from the stream, what do I wanna get? I wanna put a filter operation on him. So what is a filter? You're saying, I've got, so think about this for a minute. I've got a bunch of coins with me, and you come to me and say, hey Venkat, you got all these coins, but I wanna separate these coins. I only want 50 prices out of it, right? How would you do this? Man, you could sit there and mechanically look at each. That's one of the best ways to get old and die, right? Instead you say, here's a filter, throw your coins in there, we'll just kinda shake it up, and the right coins fall out. Well, that's your filter method, right? So the filter method basically says, send this collection through this fancy elevator we looked at, and only filter out the values that we really care about. Well, what are we really caring about? So given a number, tell me if a number mod two is equal to zero. You see this little function, right? Little function let or functionoid, whatever you wanna call it, we call it lambda expression, that we are passing to this function, is gonna return a billion, a or nay. If it says yes, that value is included in the future, if not it gets dropped out, right? So think about this as a pipeline, values go through this pipeline, if the value is true, it continues on the pipeline, if not it gets dropped out, right? So it continues further, what do I wanna do now? All right, now I filtered it, and if you really, really care about writing readable code, you could do this a little differently. Why don't we do that? We do care about readable code, right? So why don't we say this? Let's say public static Boolean, and what is the Boolean here? Is even, right? And is even what? I'm gonna say is even of a number. So let's go ahead and say int number, and in this case I'm gonna simply say return, and number mod two is equal to zero. So now I can simply come over here and say, sample colon colon is even. Notice how readable this code kind of becomes. You are saying take this collection and filter is even, right? You know I hear people tell me, oh functional code is unreadable. What they're telling you really is they are unfamiliar with it, right? Now of course you can write bad code in anything. If I really am adamant about writing bad code in functional programming, nobody can stop me, right? Well hopefully colleagues of mine will stop me. But the point really is, we could actually write readable code, and you can say filter through and only get me, did I put it in the wrong place? Yeah keep an eye on me, don't say yeah later on. These guys are watching me do wrong things. There, yeah, absolutely, right? So now what do I do? Now that I've filtered out even values, I need to double these values. You could write them into a separate function if you want to. In this case I'm gonna apply a map function, and I'm gonna say given a number, get me the double of that number. Or we could write a static function or an instance function that says double it, and then you can just call it. Now that we are done with it, what do I wanna do? I wanna total the values because I'm really trying to find the sum of all the values together, isn't it? So notice how that worked. Did somebody raise their hand with a question? Yeah please. That's a great question, awesome. I'm so glad you asked that question. Brilliant question. He said, when you looked at the first version, you iterated only once through the list. So what you're implying to me is, hey come on over here, what about performance? Are you iterating through this several times? A brilliant question. So why don't we do something else to answer that question? I'm so thrilled you asked that question. So let's go back here. So let's go back to this guy here, and let's say sample dot double it, double it. So let's go ahead and create a double it. So public static, and this is gonna be integer, and we'll call it double it, and that takes a number, and we're gonna simply go ahead and return a number times, oops, number times two. So let's make sure it's still working. All right, seems to be working fine, right? But let's do something you should really not do, but just for your purpose, I'll do this. And I'll simply say here, is even called on number. I'm just gonna print it. Now by doing this, this function is no longer pure, right? So keep that in mind, don't do this too often. And then over here, I'm gonna say double it is called over here, right? So let's take a look at this and see what happens. If you kind of look at this with your naked eyes, your observation is absolutely correct. But you see, I have glasses, right? So I'm looking through my glasses, and I'm not seeing what you're seeing. What I'm seeing is this stream, the stream I said is a fancy iterator, but it's got another capability, and I have only seen my children have that capability as well. They are lazy to the bone, right? You go tell them do something, they say, yep, it's done. And they actually have not done anything. They're still sitting there playing video games, right? And then you say, let me see your work. They quietly sneak to the room and get the work done now. That is exactly what the stream does as well. It is a lazy collection. So when you said stream filter, he's like, yeah, I'm done. He didn't do a job. Then you call map on him, he says, yeah, I'm done. He didn't do a job. Then you call some on him. He's like, oh my goodness, let me go do this now. So this entirely clubs it back to one collection quietly under the covers. So it did not do multiple iterations, even though to our naked eyes, it appears it does, but this is a lazy evaluation, extremely smart implementation under the covers. Remember, you're passing a stream object to filter. It doesn't do the job. Instead, it returns another fancy collection, which didn't do the job, but caches the operation and says, I will do this later on when I really, really have to do this, right? And then you then call map. It doesn't do the work. Instead, it passes another fancy collection which caches that operation. Then you call some. He says, oh, I got three operations lined up. That's one looping and does it. So if you notice the output in this case, what's gonna happen is, it is not gonna evaluate all of that. I'll come back to this in a second to show you a better example in a minute. So I run this. Notice it says isEven called so many times. It called isEven on every single value, correct? Right, agreed, right? But rather than putting the sum, I wanna find first. Now, what does find first do? It says, find me the first double of an even number. Now, if I naively do this, what will happen? I'll go through every element and check if it is even agreed. Then I will start an iteration one more time and I will double each of the values. And I'll give you this collection. Then I would say, ah, I'll cherry pick the first one. And you look at me and say, this is dumb. You wasted all this effort. Let's see what happens now. I'm gonna say, get me the first element over here. And let's see, when we run this, let's see what happens. In this case, I'm gonna go, ah, let's see what he's complaining about. They get operation on optional int. It is gonna be getInt. So let's go ahead and try this out. And eh, cannot find getInt operation also. You know what, let's not worry about that for a second. Let's just print the optional for a second. Why worry about that? That's not the problem we're trying to solve. Notice what happened. He said, is one even? Yes, it is. No, it is not. What about two? Yes, it is. And he didn't go to three. He didn't go to four. How come he knew that? Because this has been clubbed together under the covers, right? He said, yeah, I found the first even. And he says, hey, map, take care of it. Map says, double it. And gives it a fine first. And fine first says, I'm done. Hey, what about all the other work? I go home, right? So notice how this optimization happened. So this is one of the beauties, is that it knows to optimize under the covers. So you've got the performance while you have the clarity. A awesome question. So that is exactly what's happening in this particular case. Excellent, please. Sorry, say that again. Let's not worry about it. I will avoid syntax related questions here. You see what I'm saying? And we can take it offline. Let's keep the focus on the paradigm. Awesome. So, all right, that's great. Let's talk about kind of clubbing this all together. Yes, please. A little louder. I'm not good at hearing. Execution time. Tell you what, let me build up another example which will kind of bring the clarity of execution time. How about that, right? So let's do that. And if you really want to look at this again, we'll take it offline. But I do have an example with performance. I'm really trying to rush through that to get to that point. But good questions keep them coming though. Excellent. So what do I want to do? I want to go to do a certain operation and we'll see how difficult or easy it is to do this. So let's give it an example. I want to go to a bunch of ticker symbol values that I want to work with. So let's get rid of all of this for a second. And I'm going to go to just the main method. And I have a bunch of ticker symbols. So we'll call it tickers. So tickers is just a bunch of ticker symbols that I have on my hand. It's called tickers.symbol. So I'm going to say list of string. Keep an eye on it. I'll make mistakes while I type. I'll call it tickers equals tickers.symbol. So symbols. So I've got tickers and I've got the ticker symbols. All that I'm doing is referring to it. That's all I'm doing. So let's start with this. I print all the ticker values. It just prints the collection for us. I've got a bunch of stock prices. Now what do I want to do? My problem statement is this. Find me the highest priced stock, which is less than $500. I always have to quantify that. Otherwise, I always get Google or Apple as a result. So I'm going to say give me a stock price, which is $500 or less, but give me the highest price. Make sense? So what are the steps involved in doing this? Think about it. I've got the symbols. What's the first thing I need to do? Get the prices. I've got a collection of prices. What's the next thing I have to do? Filter out the ones which are $500. What's the last thing I have to do? Find the maximum. Got it. Let's see how we do this in an imperative style. We would first of all say, in this case, I would say a list of stock prices. Now what is a stock price? I have a stock price. You would actually call it stock info. You would, in a familiar term, call this as a Java bean. So all that it does is it contains a stock info. You can create it. That's all it contains. So nothing really spectacular. Just a data object. So stock info. And this is going to be a stock prices. Let's call it. So equals to what? New array list of stock info. That's all we got here. So I created an array list of stock info. Now that I have this data object, what do I want to do? The key here is that I want to really fill up this collection. So we would start with string ticker. And then we could say in tickers. This is the ticker symbols we have. And then I want to go find the stock price for each and every value. How do you do that? Well, you've got to have a service to provide you the stock prices. Well, it turns out there are a couple of services out there, but we could use Yahoo service. And I'm going to go to Yahoo and say, hey, Yahoo, can you give me the stock price for me, please? So I have a stock util over here. The stock util is a convenience class. What does a stock util do? It says, go ahead and get me the price for me from Yahoo Finance. If you're curious about how I get the price from Yahoo Finance, it is just a code that parses the response from the web service. It is not a good idea. It is not manners anymore to show parsing code in public. So go to your privacy of your home or office. Keep it away from the children and take a look at the parsing code. That's fine. But that's the code we're going to look at. So what does get price do? It just gives me a stock info. So I'm going to simply call stockutil.getPrice. And this is going to give me a ticker. And it's going to give me the price for it. Now, what do we do with this value? I'm going to stick that into the stock prices over here. So stock prices, I'll call it stock prices in a second, dot add, and I'm going to add this to the ticker prices. So we'll call it stock prices. I'm making sense so far. Okay, we got this value here. Now, what is the next thing I want to do? You said the next thing I have to do is go ahead and pick the values which are less than 500, right? That's what you said. Now, what would I do to do that? I would say here a list of stock info one more time. And we would say stocks less than 500 equals muArrayList one more time. And in this case, I have to repeat the loop. But this time it's going to be stock info, we'll call it stock, in what? In stock prices. And this time around, I'm going to say if, well, I already have a function for it. So stock util one more time. And in the stock util, I'm saying is price less than, right? So I'm just going to call it. So I'm going to say if stock util is price less than $500, so this is going to be stock, 500, then what do I want to do? If it is really, I'm going to simply say at this point, go ahead and go ahead and add it. So stocks less than 500 dot add. And this is going to be the stock. Am I making sense? You say, wait a second, but why are you doing it in two loops rather than one loop? Does anybody have an answer for it? Single responsibility principle. We talked about it, right? So if you club everything together, you look at that and you scratch your head. What's this doing? It does this, and that, and the word and, and then reveals that you're not doing single things. Why are you not writing code like this normally? Performance. You're like, hey, no, no, no, I won't do that, obviously, because that would suck in performance. So we are compromising readability of code for performance. Make sense? But in this case, of course, I compromise performance for readability. And you're going to say you would never be in the enterprise if you do this, right? Yeah, that's kind of the reason to do this in a better way. All right, we got that through. What's the next thing I want to do? Now that we got through that, we got to pick the highest price from this. So I'm going to say stock info. We'll call it as high priced equals. And this is going to be a new stock info. And this is going to be just an empty value with the price 0, 0. Start with a lower value, lower bound. And then I'm going to loop through this one more time. In this case, stock info. We'll call it stock. But this is from stocks less than 500. And this time, I'm going to say high priced equals. And once again, go to the stock URL. I've got a method called pick high, which will pick the highest priced value from both of them. So I will simply say, over here, stock util.pickhigh from what? Pick high from the high priced and the stock itself. So when we are done with all of this, assuming we did this all this correctly, I want to finally print out the high priced. But before we go run this, normally you would never see me write what I just did. Every step of the way, I'll be running this code. But I also know that the internet connection is extremely poor. I'm actually following what is called the anti-pattern that actually Neil talked about in his presentation. And the internet connection is pretty darn slow. That's why I kind of wrote the code in length before I ran it. So basically, what I'm going to do here is simply say long start equals. And I'm going to say system dot nano time. And I'm going to measure the time for the beginning. And let's just do this right here, right after this. And then I'm going to come down here and say long end equals system dot nano time one more time. And now we will say output end minus start divided by 1.0 e9. So basically, we're asking him for the time that it takes to do this entire thing. I'm running this code. It's not given a compilation error at this point, which is scary. But this usually takes anywhere from 20 seconds to three days to run. So we got some time to kill. So questions so far. Keep the question very short. 10 seconds. I'm giving you 10 seconds. Keep it short. So there's normal programmers and polyglot programmers. I want normal programmers to become polyglot. How about that? Again, the familiar, the normal, is a new norm. That's it. Any other questions? All right. So we went through this. And like I said, the reason we break this into multiple loops is to give the clarity. You would not do this in a regular imperative style because of performance concerns, right? How much time did it take? 61 seconds. And Apple still came through. Sorry. So far so good. Can somebody note this down so we can make sure the parallel version we write is the same? I'm sorry. The functional version we write is the same. So 61 seconds. Give or take. And then $430.47. Did anybody write it down? Thank you. Now let's go and write this again. So let's go back to this code and try this one more time. How many lines of code did I write? Anybody notice? About 20 lines of code? Something like that? All right. So let's close all of that. Let's try this a little differently. I'm going to say public static. So what am I going to do in this method? I'm going to say find price. So we'll just say find price. And this is going to take a stream of strings, right? So take a stream of strings. We'll say tickers. So what am I going to do in this code? I'm going to say, all right, you gave me a ticker, right? So tickers, what's the first operation I should do? Help me out. Tell me in words I understand in functional style. Three-letter word. Map. That's correct. So we're going to do a map. And map of what? Given a ticker, you want to do what? Get price, right? Stock, you tell. Get price. Are you with me? All right. What's the second thing you want me to do? Filter. Stock, you tell. And what's a filter operation? I want to start with this new stock info, empty and zero, right? And I want to give him a function. What function do you think I should give him? Big chi, right? Are you with me? And when we got all of this value, my last step is to print it out. So let's go back here and print it out with me. We want to know how much time this takes. So this is going to be start equals system.nano time. And of course, at the end, I want to know what the time is. We'll include the time of printing also here, no big deal. And we'll say system.nano time and output. Thank you for fixing. I love pair programming. Start divided by one point. I'll finish this and go fix E9. And this is going to be system. Does it look OK so far? Are you happy? So this is purely a declarative style, right? So look at the reading of this. We came in and we said, go find the ticker. Go get the map of all the tickers to stock prices. Filter out only the prices less than 500. Reduce it down to the high price. So far so good. You can easily inject new things in the middle if you want it to, correct? So let's see what this guy did. So let's get back over here. I don't need any of this stuff anymore. We'll comment this out also. So let me make sure I have the main empty right now, except for the tickers, correct? So let's go ahead and call find price tickers.stream. Happy? Assuming I wrote all of this correct. It should give us hopefully the same price. The market is closed right now. And it should hopefully take about the same time give or take. That's kind of unpredictable depending on internet speed. But we can put in a buffer of values to make sure that happens. So which of those two would you want to write? The first one, of course, if you're a consultant getting paid by the number of lines of code you write, right? But on the other hand, if you want to get your work done and go home, the first one is better. If you want other people to understand what you're doing, the first one is better, right? Now notice the transition from one to the other. If you only focused on that one loop, that's hard to go this route. But we said, separation of concern, do these steps. Ignoring the performance for a minute, then you're saying, ah, I can plug each of those into a separate step and do this particular operation. So that becomes a lot easier to work with when we do it that particular way. So that is an example. You say, all right, what benefit did we get so far? Well, the benefit we got so far is we are able to write readable code. We're able to write less code. You say, okay, okay, well, all right, all right. It's less code, all right, but I like programming. Shouldn't I be writing a lot of code? Well, if you really like programming, you want to write a lot of production code, not a lot of code that doesn't do anything, right? So that is great. The second benefit you get out of this is that the code is easier to understand, right? It's easier to understand. And again, like I said, don't look at that 83 seconds. That doesn't make sense because the network is the delay here, right? And if you're really interested, we could put a little dummy value and try it out and you will see it. But you say, all right, but is that all the benefit? In fact, the performance will be exactly the same because we didn't do anything different in this case. So, but here is the beauty. You say, all right, this is all great, but before we go further, let me ask you one question. Look at this for a minute. What are you doing here? Going to Yahoo and getting the price, agreed? What are you doing on the next line? If I can see the line number from here, that line with the one on it, filter. You are stripping out the values. That's a math operation. What are you doing in Reduz? You're comparing prices. Which of these is the slow operation? Going to Yahoo, I would assume. Right, yeah? You're going all the way to Yahoo. If you say the math is really slow, you need a better processor, right? You're going to Yahoo is the slowest part. Doing the math is the fastest one. Which of the one is the most compensious? The Reduz, isn't it? You gotta compare and select one. Now you go to Yahoo and you're waiting, twiddling your thumbs and you're saying, hey, what are you waiting for? Waiting for Yahoo to respond. Why don't you do something else? What, like get a cup of coffee? Why don't you send multiple requests to Yahoo at the same time? What's the benefit? Well, Yahoo is going to take some time, right? Two seconds, whatever that is, depending on the network delay. Why wait till that comes back to send the next request? Why don't you just take all of them and, pluf, send it to Yahoo? Yahoo will say, whoa, last time we came with one request at a time, now you got 20 requests at the same time. Guess what, that's Yahoo's problem, not yours. So why not do that, right? Now, imagine you're at work and somebody comes with this brilliant idea. What do you do? You look at them and say, what are you smoking? Do you know how hard it is to paralyze code? And especially if they tell you this when performance is critical, which doesn't always happen early enough unless you follow it because advice to keep that in your eyes all the time. What happens? You're like, no, we can't change this right now. It's too hard and it explodes on your face. Bugs creep in, right? Or you go back and say, really, is that what you wanna do? Yeah, okay. So how about, did I spell it right? Parallel stream. What did I do? I just changed that one word from stream to parallel stream. Why? Because my functions are pure, remember? They don't mess with data. They're not changing stuff behind us, right? So we didn't do any of that. So we are running this. Again, we are incurring the network delay here, but typically this would take a lot less time than going through. And what I will do is I'll change the time delay and mark up a value for you. That way we won't be dealing with the network. Network is completely unpredictable here, as you know, right? You've been trying that. So when this fails or when this finishes, I will go mark out the value and then we'll see how much time it takes. And then we can kind of measure the two and see, okay. That's 40, that's not really realistic. Let me go fix this. So I told you I won't show you the Yahoo finance, so close your eyes so you don't see this. So basically I want to mark up this value. So let's go ahead and kind of get rid of all of this for a second. And I'm gonna simply mark up a value, right? So I'm gonna say try thread.sleep one second, right? One second and then catch exception. So we kind of gave them a delay and then return 12.12. So all of them are the same price. You already saw the code is working. So it doesn't matter what the value here shows anymore, right? So let's go back to this guy. Do the stream thing first of all, run it. And actually now that I've marked this up, we should be able to run all of them at the same time, right? I've given a second delay, so that's a 20 second. I don't remember how many stock prices I had, but doesn't matter, right? So that is the sequential execution. If you're interested, you could try the other commentator code and run this, but trust me on that, they take exactly the same time, right? No difference. How much time does it take? 20 seconds, correct? Obviously. Let's go back to this and say parallel stream and let's see what it does. So this guy is gonna go send out all those requests concurrently and there is the 3.0 second, right? How does that sound? Sounds reasonable? That's all I have. Thank you.