 Hi everyone, welcome to this session today by Aditya. N ways to FizzBuzz enclosure. It's almost like a tongue twister, so I'm very interested to know more about it. So without further delay, Aditya, over to you if you can share your screen and get started. Sharing in three, two, one, launch. So the session is N ways to FizzBuzz enclosure and we have begun. That's the sort of overview. You will see some very funny words like Decomplex. All of these things will be explained. Moving on. So the idea is to tour the concepts of Closure and its standard library via FizzBuzz. The constraint is that every FizzBuzz must have a reason to exist. No time pass FizzBuzzers and no FizzBuzzers that don't actually convey anything useful. It's going to be a very rapid fire live demo and I'll keep going until the timer runs out. There is a blog post which has all the stuff, lots of details more. And I'm sure like stuff that I will skip here, all of that is in the blog post. I'll share it's there in the slide at the end. And you know as with demos, like if it fails, I will, I intend to record a separate demo and send it. And because people like to tweet things about talks, like you can tweet this one, you know, if you do FP, right, you get OOP for free and vice versa. Okay, with that. Sorry, I will move on to appease the demo gods first. Small prayer you may recite with me. All under the ultimate bless we who are here in this demo that our core be functional and our functions we pure that our data be immutable. So we may know the value of values that are systems we composable. So they may grow with and scale with grace that their states only mutate in pleasantly surprising ways that our networks and computers work for at least 20 more minutes for otherwise nothing lives. Nothing evolves in the name of the alpha the beta and the eta lambda X with X goes to X goes to lambda X with X goes to X. First, we begin with some definitions. It's very customary in the world of closure to start with dictionary meanings and definitions of things. I like doing this. What is fizz buzz? It's basically a children's word game designed to teach them division division. And the rule is that any number divisible by three is substituted with the word fizz and any number divisible by five is substituted with the word buzz. The rule is that the lowest common multiple which is 15 should get fizz and buzz both numbers here mean natural numbers starting at one. I don't know to start a indexing war. So we are one indexed for the entirety of this presentation and a little bit of closure is closure is a is a strange language. It's a postmodern revival of the Latin roots of American English. There are some weird words like complex, which means braided and twined and hopelessly so. Completed is a thing that makes close audience frown. Decomplex is the inverse, which means to unbraid or decent wine. Decomplexed is consequently a thing that makes close audience smile. And decomplecting is the activity that close audience enjoy most. And this is what we will try to do now through the whole talk. So, when you're new to closure, you might accidentally discover, you know, or you may try to hunt for familiar things in the standard library. And you may accidentally discover for because it's incredibly hard to search the word for in any documentation and find a function with that name. But once you find it, you know, you might say ooh, this comprehension nice. And I can see, you know, the Python gentlemen in the house going like this. And then you might come up with, you know, a, you know, you'll muddle around a bit and you'll come up with a little translation. This shape is very familiar. Cond is conditional. If the remainder of n divided by 15 is zero print fizz buzz. If it is if remainder of three zero print phase five is the same buzz otherwise print the number. And, and so I'm going to just in line eval all of this is like a live notebook. So, alright, so I'm going to evaluate this on my system and what it's saying is ignore this nil. It's saying that I got like a classic just buzz or whatever. Let me just go. This is a separate evaluation context. Don't worry about that. I'm going to say fizz buzz classic. And let's say one vector one, three, five and 15. Alright, so and we get nil nil nil back, you know, so what's happening here is we actually have to go to the shell. This is sort of like the Python shell, but much better. And we see that stuff has been printed out here. So printing basically causes effects and the print goes to whatever like whatever printing devices available and the process is connected to the rebel process. This is called a rebel process. It's like sort of like a separate server process. I'd already started this before all of these things are hard to explain right now but you know you'll get a closure documentation. It explains this quite nicely. So let me just go back here and so people tend to use this as a calculator. This buzz classic one, three, five, 15. Okay, and then you see out here you get the result one phase buzz blah blah, but this nil nil nil nil is happening so we'll see what that is about in a bit. I'm going to go back here. Then I'm going to close that right and let's see. Yeah. Alright, so moving on. All right. Yeah. Okay, so this in in the world of closure. Unfortunately, this rendition of fizz buzz is dead in the water. There is a reason lots of reasons actually because for his lazy and the repel is eager. These are also like common words you will hear in the world of closure eager basically means it wants to evaluate now and laziness is a form of deferred computations and you know, these mix very dangerously. So it's important to come with sort of open mind and try to unlearn old habits. And then there are always production outages, right. So it's this is a thing that trips up. Many, many people knew to closure and it's tripped up like experience people as well. It's not something. It's something we just are cautious about and worry about don't mix laziness and side effects. Anyway, so now we are going to sort of try to clean this up. And remedy some of the melodies. So this is a lazy bus and you will notice that the print talents are gone. Right. And now I can invoke lazy bus like this. Right and get back my result. Right. And now if you look at the shape of this and if you look at the shape of the result of the classic buzz. Right. So the shape is the same. The number of items are the same except we get back useful values over here and we get back something that we can't do anything with like these nails are basically pointless. And so that brings us to the dissection of this classic fizz buzz. And I would like to say that the classic fizz buzz should be considered harmful in closure. And it's important to avoid and examine and avoid its severe defects. So the first effect is that it exhibits broken behavior because it mixes purely functional calculations with print printing which is non deterministic. And this is actually not this is actually frowned upon generally in functional programming because we don't like to mix effects with the with calculations. That breaks certain guarantees like lexical lexical scope and referential transparency and other kinds of things again like jargon functional programming jargon. You can look up later. It has a broken API contract. What does this mean it means that it returns a bunch of nils. Whereas we care about the values you know one phase buzz whatever it is and if we get the values back we would like to do something useful with them. The third and more pernicious problem is that the time model is broken because it's not just a print Ellen we have something called for which is actually a lazy sequence generator. So we were at a broken aesthetic we like to do one job and do it well and printing is a second job. And if anybody has watched the message Helen Jerry's lecture series at at recorded at HP in 1986 the structure and interpretation of computer programs. They love to say that you know that's George's problem. Also I'll tell you like right now that the blog post accompanied has ideas to get fired with fizzbuzz. People like to get hired with fizzbuzz but there are ways to get fired as well. So if you're curious you can you can go and see the blog post for that. Okay, so what happened was that if we keep our functions pure like lazy bus and there's not a value judgment this basically means that it it's a historical name etc pure impure. No, no nothing to do with people and feelings but with the brutish utilitarian code. So if we do that then laziness actually becomes a friend as a nice side bonus. If you recall the children's game definition it doesn't say that do fizzbuzz for numbers one to 100 but if we see programmer fizzbuzz definitions everywhere on the Internet it's always one to 100. And that's where the children's game definition is beautiful because it talks about all numbers all natural numbers to be precise. So I'm going to evaluate this and now this is going to see what is defined range is infinite. If I try to evaluate range in the repel my repel will block because range will try to generate all possible numbers from zero to whatever is maximum on my JVM and this is a JVM closure by the way. There are many other flavors of closure. Likewise I also then I did I passed all the numbers to lazy buzz. All right, and lazy buzz like I said is a lazy sequence generator for base lazy sequence generator, except that neither of these blocked these got evaluated as these functions in the namespace we call user namespaces also are very core to how closure works. Typically all closure sessions repels start in the user namespace and that's just fallback behavior you can you can start it pretty much give it any initial namespace to start. And now this is what it means to be lazy because now we have every possible fizzbuzz out there and then if you take the first 15. Here's what we get. All right, and I think the match is right here. It seems to be right. Okay, so what's what else. That's over decomplect again. What it I mean it's just it's people like these particular words in the closure community I like this word a lot because it's also cool. What it means is we want to pull something apart now. The previous function had a lot of things going on. Right. One of this is seems to be a repeat pattern, but let's look more. So one way to sort of pull out the repeat pattern is define a local local lambda. This is like the lambda syntax fn with arguments and zero etc etc. And we bind it with let's do a name called divisible. And then we use that inside our thing and I'll evaluate this. And so I have a lead bus definition, which now overrides the previous lead bus definition, but it doesn't matter because this is all referentially transparent. What we can also do is we can override definitions of or we can create a top level definition because this is actually a distinct idea in the domain of fizzbuzz divisibility is pulled apart. And that's decomplected so we can we can pull out both these flavors of divisible and you can look up what comp does later it. This is basically a higher order function that consumes a collection of functions and returns a returns another function that calls those functions in order. And this is very hard to understand unless you play with it in the repl. The important point to note that all of these are referentially transparent and they are tiny but they add compositional firepower and we like that we like small functions. And this is sort of an example of a function that does one thing and does it well, which we have lifted out and quote unquote decomplected from the previous solution. And we can also decomplect. So the other thing that's completed is sequence making and choice making and we're going to be complete that now. What does that mean it basically means that this is this is just a calculation that this is the choice making it's a branching logic, which maps to maps left hand side to right hand side. There is no reason for this to be a hard wire to a particular sequence representation. And so we can pull that out. I will evaluate this so I'm evaluating these as I go along. And if something blows up then I'll have to go back and evaluate something so. All right, so now what we get is standalone logic which is usable as this. So I evaluated all these code blocks and it doesn't print nicely. I have to fix that it's a bug in my. In my thing. So, but we basically get this and this nil is actually is because of this namespace require which doesn't matter. Okay. Oh. Get back that. So we have standalone logic and now we can compose it with for so we haven't lost the ability to generate a sequence right and we have this year. And then we can again, this is the definition of all of his buzzes which we had seen before we have that definition again but we also get more design choices. In other words, it opens up a design space because now we can convey the idea that fizz bus is basically a mapping of a calculation to one domain to another domain which is the domain of natural numbers. And we can do this with reduce because map is just a special case of reduce and that's your homework. The interesting difference here is a map is lazy and reduces not and you let you should explore the consequences of that separately. Basically means that if I try to reduce over all the numbers my my session will block forever or until it runs out of memory or something. And this is almost too embarrassing to write but when you have pure functions and separate out laziness and separate out effects and all of these things parallelism goes from being like ridiculously hard almost impossibly hard to hard but possible and hard but And in closure it's almost like it looks small and inside if you look at the function definition also even as a newcomer if you stare at it for maybe like a couple of hours go through the standard library, you'll be able to infer what that thing is doing. And here we go again. So now I have defined these guys and then even though one is one is running parallel and one is running sequential, they do the same thing they preserve order they preserve sequential computing etc. And give me back a collection that is again so these are funnily like, although this is parallel it is referentially transparent to the sequential version. It's quite interesting. And then you can actually look at the source of any function using Rappel utilities. And this will basically print out the source code of team up into the Rappel. You can see this again later all this code is going to be available by the way. Now we will be completed. We are actually over time. You want to tell me can they find the source and the blog you were talking about. All right. Okay, so quickly I'm going to run to wait. I'm going to kill this started. All right. So there's all stuff that's being part all right solution domain problem domain. There is one which generalizes to all fizzbuzz is not just factors of three and five. And they are all possible because of lazy semantics. There's also object orientation which is pulled apart classic OOP complex all these things. And there's a lot more that I'm going to be doing on the fizzbuzz blog post so you can watch it if you want. And oh where did the what I'm sorry I put the. Okay, let me just paste that thing in over here. The link to the blog post and now I can't see it. Okay, you can also add it to your proposal. Yeah, I just put it here. Yeah, that's the right. Just keep this here and if you have interesting fizzbuzz is you can send it to me at this email ID. And like we can go through everything in the hangouts I'm actually I'm fine you know sitting at the tables and just doing repel sessions all day. Yeah, most happy to spread the closure. I'm sorry about this whole network business I mean it sucks. But we can pray but we can't control you. I know I know I tried I tried. Yeah. Thank you so much. Thanks everyone for attending the session.