Intro To Elixir | Lesson 13: Drills and Tips - Part 2

Elixir
Transcript

English (Auto-generated)

Hello friends and let us continue our elixir journey by doing some more drills So we'll continue with drills and tips part two and today I'm going to go over the fist bus. Um it's pretty hard to have learned a programming language without having encountered the fizz buzz challenge. Um uh It is the common interviews, I don't think it come in interviews anymore unless unless it's for a very junior kind of opposition that said it's an interesting one because it does have some some some twists that helps you understand a particular programming language when you're learning it for the first time in case you haven't encountered the physical Challenge works like this. So when you want to print numbers 1-100 with the following twists, if the number is divisible by three you put fizz. If the number is divisible by five you put buzz, If the number is divisible by five and 3 you put his body. So the printout should be something like this, you print 12 and then instead of three you print fizz and then four instead of five because it's divisible by five ft print buzz and then instead of six you print fizz and so on and then at some point you get to 15 which is divisible by both five and three then you put phase but like this so it's something along these lines, so I'm gonna show you two different solutions that's probably thousands of different solutions, you can come up with um I'll use one solution that doesn't leverage the imam library and one that does, so you know, the goal here isn't to come up with the most optimal efficient or performance solution just to get comfortable with the elixir syntax in the way things are done in a lecture. So I'll start by defining my module here, I call it lets buyers and then let's say here, I define a primary uh function, I call it run, which is really the function that's gonna execute the fizz buzz is gonna do the printout. So I call it basically by doing fizz buzz dot run and then this should this should do um three Fizz Buzz Challenge. Like I would say initially I will do it in a way where I just have functions that call each other in a recursive way more or less that said, you know, um as as as you might have experienced so far um in in electoral, because we can have the idea of different function heads, you are kind of calling maybe not the exact same function but you know, the same function name with a different head, which I would still possibly call it like a recursive type of a solution. So the idea is, I want to have a function and this is kind of how you can think of things in elixir is let's say here, print number or facebook's and we take in the number. So the idea is I want basically a version of that one that gets executed if the number number is divisible by three And then number is divisible by five and then number is divisible by five and three. So basically different function heads. Of course I have to modify those functions to take that into account, visit why I'm mentioning this because again your first inclination coming from different programming languages is that you may be inclined to do something like um you know to have if else, statements or something like that, which is completely okay depending on your language in the lecture is preferable to start to try things without or you know, as I will show you in the next solution like using some of the built in libraries before attempting to do a lot of loops and and and nested if statements and so on. And then finally here um I need a version of the function that the number is neither divisible by 563. And then here at the end I'll just write it as a comment here. We need a way for the function callings to stop. So in this case here when we reach 100 so at 100 we need we need to stop because um You know as we know 100 is divisible by five so you know um into a print buzz but then we needed to stop after after that awesome. So I'll give you hear a second to think about it. How can we make sure that this function executes only if numb is divisible by um three. I don't expect you to know the exact code. 100% but try to think from the concepts we've learned, how can I restrict the function to only execute when a certain thing happens? Oh the most common solution. Hopefully the one that came to your mind is using guards. So to use guards, if you recall we put when now how do I know that a number is divisible by three? Well you know I expect you to do some googling here, I don't expect you to know it. If you thought if the ampersand sign you were you would be mistaken because that doesn't exist in elixir. So the answer would be actually using a built in function which is R E M um that's just available like this as you can think of it as a global function uh that you could do a remainder of say five and it means something like 15 and 50, 15 and six. The remainder would be three because because 15 has 2 to 6 is 12 plus three which is the remainder. So it's not something is divisible. We just have to check if the outcome is zero. So we could do our E. M. 15, 5 critical zero. If using this confuses, you could think of all these public functions are available with this kernel module. So like R E M is part of this kernel module right here. 55. A lot of the ones we've used before, if you recall we used functions like his list check if something is a list turnout similar to those ones. You could also do the same thing Colonel that is left and then that so again if you want to kind of feel that you know all functions are contained within modules. You can think of those publicly available functions are all part of this kernel module that is built in with elixir. So in this case I can say well very remainder of Providing the number by three is zero. Then in this case we print she recalled we can print an electric using iota puts and then we pass it the string that we want to print so we want to print fizz. But the problem here is this will stop, it will not actually continue to execute. So how do we make it execute? Well we make it execute by calling the function again so we do print number or fizz buzz and then we can just have it the same number because it will just keep calling itself forever. You have to make it try for the next number. So for example, if we're at two here then too wouldn't be divisible. So this function will be called. If you are at three here then it will call another function which will be four but then it won't hit this one because four is not divisible by three so it's going to try some other ones here. Okay so again here it's hard to test the solution yet because um um um you know we need a way for it to stop so maybe let's go ahead and I'm gonna jump a little bit here and I'm gonna say something like well how do we stop? Well we just want to stop if death Brent fraser balls, when do we want to stop? We want to stop the number is 100. So I could simply say well if the number is 100 simply what was you know that 100 is divisible by five but it is not divisible by um It's not divisible by three. So it's only gonna print buzz. And then here maybe I'll go up and implement this function right here. Which is If the number is neither divisible by five or 3, you know what do you want to do in this case? You wanna just display the number itself, you wanna also execute the next one, The number of plus one. Okay and I'll go one up here. Um If it's divisible by five, what I can do, I can also copy this guard clause but just make this instead of three, make it five and then print here buzz similarly Or the next one And then finally if it's divisible by five and 3 Then we could simply say if it's divisible by 15 then we display His was and then we call the next one and then finally here what we need to do is we need to just have this one function starts zulu or it starts to function calls or the chain of function calls by by calling print number or fizz buzz and then give it the number one again. This is kind of a question asked about this can be a parameter if you want as well, you know if you wanted to be a parameter you could do something like this. Start num and then you put an option on here again just to remind you about um the optional parameters. If we have a parameter and we need to give it a default value, you can put these two um slashes and then put the default value here. Now my solution has actually two problems that I intentionally induced because I want you to think about it a little bit. Um Try to read this code before you try to execute it and see if you can spot these two problems. Alright but I'll do here I'll try to execute it if you want to feel free to stop this cast while you're thinking through it. I'll resume here by attempting to actually execute the code. So I've again just pasted this code here in I E x and I will execute phase bus dot run um Let's see what happens as you can tell here. This is never stopping if you get stuck in a situation like this just control C. Twice. And that will stop the program. So notice here I'm getting some fizz print out some buzz printouts but then it keeps going forever. Can you spot why it keeps going forever? You may have guessed it. You know it's just for the consistency here. I'm just gonna make all these capitalized. I know it's a small thing but just the consistency. Um You might have guessed that we never actually hit this one here simply because um 100 is divisible by five so this one will hit before them and this will call again. So we never actually hit this one right here. So I'm gonna have to cut this stopping condition and put it possibly at the very top. Okay possibly put it at the very dark so let's try this now. So I'm gonna try this solution. Oh sorry I have to enter I. X. Again best in this. Well now this actually executes, It kind of does what we want but there's a little bit of a problem here that at 15 it only prints phase, it does not actually print face buds as we might expect it. Would you guess what the problem is that is correct? It's actually having this function the one that is three and five are before this one. This one should be at the very top. Remember the order of functions is important because the lecture is gonna try to execute from the first defined function till the end. And if it doesn't meet um you know I should say like if it doesn't meet the condition for dysfunction then it will try the next one. If it doesn't meet then we'll try the next one and so on. So this is why the order of how you define your function functions are important in in a lecture modules. Let's try this year after we moved it to the top, This is our solution. So 12 phase and then when you get to 15 we get phase past awesome. Um Let me show you a few small tips. I mean again this is probably not the best solution here, we're seeing we're executing this again and again there are probably better ways to do this and also one other solution. Um But you know as you grow your experience with elixir and your comfort with elixir you're gonna come up with your own better solutions. Um But a few little tips here that if you have a function that's very short I would encourage you to just make it in line function by converting it like this. So put a comma and then put do put the one liner here and then basically you don't need an end at the end of it. Um It kind of applies to this one as well. All the other ones are multi line so I wouldn't worry about it but basically this kind of makes those functions a little bit more more concise. Alright, so let's now go and use another solution so we can at least practice using the library. And I will introduce to you here in new data structure. It's used to say less commonly than the others, but it is quite helpful, especially in a scenario like this. I could also um you know, just see how your start number Or defaults to one. Now in this case here, I'll show you that there is a data structure in in in elixir called um arrange which might have experienced in different um programming languages such as ruby arranges basically a data structure that have a start interior and and interior. And it's usually quite helpful if you need to deal with let's say a series of integers that increments by one. Uh It's a common use case. So this is why I think they can be quite helpful. So in this case here I could define this um this particular range by saying, oh it goes from the 12-10 so maybe I'll play with it a little bit and I expect to show you what we can do. So this one here is um Alright, so let's just show you here. So in um that um let's say each and then if I pass it arrange. The interesting thing is the the range is considered innumerable. Similar to lists. Similar to maps, which means we could actually loop or we could, you know iterate through um ranges and do some interesting things with them. So including the imam that each and then let's see here for each X. Let's say I want to do I want to print just the X. For each one of the ranges. And then this is a very simple way to print numbers 1 to 10. And then I get at the end this simple okay meaning that it executed correctly as a short year. Remember how I could simply do here and percent io that puts and then passes the first argument. Okay, just as a reminder from when I showed you how we could write anonymous functions. So using ranges is a very interesting way to say go from one number X too number Y. Which is exactly what we want in this particular one. So we're gonna go from start number And we want to go all the way to 100. Again we could have that as an argument as well such as and number if you want to make your face was a little bit more interesting now what I need to do here. Well I can call um you know that um map um Alright actually I couldn't do each because I don't necessarily care about getting a range back or getting some kind of the value back. So each basically just iterate and then we get an okay at the end as as a return value. So what I need here is well I need to also execute in a similar fashion different functions to do different things but what's interesting here is that I don't need those functions to call themselves because the enemy each is actually calling this function with with the different tense. So although the similar things, so I'll continue to use um the same technique by using the guard clauses. The only thing here is I can have very simple functions by doing this When it's divisible by five. No sorry 15 it's fizz buzz when it's divisible by five. I put buzz, it's divisible by three, you put fizz. Um And then of course in this case interestingly enough because this is kind of um finishing this entire Iteration with each 200 and I don't really need a way to stop it all I need to say here. Well if it doesn't hit any of those it seems like I have an extra space here, let me just fix that. It seems like it's here as well. So let me fix it. Usually if you have a winter of some sort of your writing actual um elixir files, you would have some plug ins in V. S. Code which is the one I'm using or whatever. Um editor of choice you use and they will usually remove these extra spaces inside I owe that puts and then the number here. And if you recall here the way I would call this one here well you could you could use this function thing where you could do well, function X. Or you could call it number and or something and then simply call the number on it like this and then pass it the number N. And then close up that function. Let's just this one first and then we can um improve this solution afterward. So I'll copy this year, baste it. And sure enough, our solution works perfectly. One to phase four bus and so on all the way to the end as I mentioned before, if you're seeing this redefining fizz buzz, that is completely okay warning. Because all we're doing, we're overwriting this with with our new code which is to be expected as we're learning here. If you see this warning in in in in a bigger application then you should look more carefully to see if there's two things that are overwriting each other that you do not expect them to do. So A few improvements I want to do here. Well, if you remember if we have just a simple function here, what we could do, we could use this and percent syntax and then pass it the first argument, which we can do this by doing 1% 1 and then I don't actually need the um the the the end in this particular case let's test this part of the solution. We get the same results. So it's working as expected. One last thing I want to mention here, you know, I know you're probably starting to really like the pipe operator in lecture and I love it by the way that said, if you're only using it in a very simple case, like this actually may be easier to just passing the whole thing as an argument to the function instead of actually using the pipe operator because remember the pipe operator, it passes the output as the first argument to the next function. So in this case here we could simply just pass it as the first argument to the next function, which is this range that we constructed right here um instead of actually doing um like the pipe operator, I would say pipe operator, you know, again, there's no real rule. It's more of a style, depends varies from team to team from company to company, but I would say my opinion like try to use it if you at least have two of them, if you just have a single one may is probably, you know, not worth using the pipe operator, you could just pass things as an argument and now this line has gotten short enough that we could actually use the short format of the function. So it becomes something like this by the way, it's quite common to group the functions that have the same name, but different arguments together as you can tell here. Those are easier to read as you see, they're all part of the same function, head with different arguments um Okay we executed trance find, so we get what we expect and we have two separate solutions for the face bus in the homework, what I did, I've actually just uh this is for the previous lesson, so let's go to Mhm. Oh sorry, um so what I did here is um in this particular lesson, I am sorry this particular homework for the lesson, what I encourage you to do is to take the same fizz buzz but just do different variations for it, such as constructing a list, constructing a map and constructing a cured list. Again, getting just more comfortable using the different data structures that we use in elixir. Um so please give those a try and I will see you in the next lesson.
42 Views 0 Likes 0 Comments

In this lesson we will solve the FizzBuzz challenge in two different ways to solidify our Elixir learnings.

Comment
Leave a comment (supports markdown format)