Welcome back everybody. I left you with this nasty cliffhanger where I challenged you to write a recursive function that tried to run a future-based computation n times without recursion. Okay? Now I'm going to show you two possible ways to solve this. But of course, you know, maybe you have come up with your own way, and you can share that on the forums, that would be fun. What I'm going to do, is I'm going to use Fold r and fold left. Those are the kind of two power tools in our toolbox. And whenever we do things with recursion it's often nice to do it with a fold. Okay so let's see what the consequences Of this r because maybe using a fold is not always the correct choice. Okay so let's geek out for a bit and pretend that we are programing functional hipsters. I should put on here my my rimmed glasses and my treat coat but I remain hippy in my tie dye. but what we are going to do is we are going to use fold right and fold left. Let's refresh our memory what these are. Fold right takes your list, in this case your list with values A, B, and C and takes a neutral element E, and a function, a combination function. And this fold right Folds the let list from right to left, okay. So it starts at the right side of the list and then takes e, and applies f to c and e. And then it goes one step to the left because it's folding from right to left. And then it applies f to b and the result of c and e. And then finally it applies f to a and the result of folding from the right. So it's easy to remember. It's just like the northern winds blows from the north, fold right folds from the right. Obvious. Then if you look at fold left Well, that falls from the left, so what it does is it starts with a neutral element and it has to combine a function. And it first combines e with a and then combines that with b and then combines that with c. All right, so it associates the other way around. We're going to Use both foldleft and foldright to implement our function to retry a computation and here is the solution so what we're going to do is if we want to use fault And in this case we'll start with foldLeft. If we want to use fold we need a list. Well how do we create a list? I know. We can create a list from number of times. So what we will do is we will create a list from one to number of times and put that into a list. Then we will map over that list the block so now we get a list of n blocks. And notice that we're kind of putting that block under a unit arrow because we don't want to execute a block just yet. We want to execute them one by one because every time we execute it might have side effects so we have to be very careful to delay the execution of the block. Our neutral element has failed. So we create a new future failed, and then what we do is we fault from the left. Stating with failed and then our combined combination function takes a future, that was a thing on the left, and the block and then we try to run the future. And if that fails we are going to run the block. Okay so it looks a little bit puzzling But if you think a little bit about it, retry(3) of block will unfold to failed recoverWith block, recoverWith block, recoverWith block. If you don't believe me, let's do the derivation here. So, first, we're going to Run the List, so we get that's the simple thing, so when we run the list, now we're going to map the number of attempts over that, and so now we get a List of blocks, and remember this is super important To do this here because we don't want to accidentally execute the blocks. So we have to build this little funk that hides the block. And now we're going to foldLeft. And if you do the foldLeft here, You will see that this gives us the same result. So by doing a little bit of substitution it should be clear the result of this function is exactly what we want. All right now let's try to do it with fold right. And there's even more higher order functions, so, in this case, again, we create the list from 1 to n, and we map the block, delayed block, over that. We recreate the failure, but now, we're folding from the right, starting with failed, but we're not really starting with failed. We're starting with a function. That is delayed failed and because where you can have to kind of have turn this from the other side so what you will see is two combination function nw takes a block on the left and a function that returns a future on the right And we have to return a function that returns a future. And how do we do that, by calling a block and then falling back to the a. So here you see that everything is reversed and when you unfold this you will see this here. So this Looks like super cool, lots of high order functions, but to be honest I think that in this case the recursive solution was the simplest one. All right? So sometimes straight recursion is the way to go. And we should just leave those higher order functions to the functional programming hipsters, right? And this is one of the most important things or function of mastering programming. You have to know when you use recursion, you have to know when to use fold, you have to know when to use Foldleft because you say that you know foldleft and foldright, you know, the code looked quite different. And in one case, we even needed more higher the functions. And but, this kind of balance, that issue of, as you get more experienced, in writing code in a functional style. That's what you will learn. Kind of it will become natural to you. Just like a soccer player knows intuitively when to jump and to head in the ball into the goal. All right thank you. This was the end of composing futures. What we're going to do next is we're going to look at some even more advanced ways to deal with futures.