Learn With Me | Intro To Elixir Lesson Thirteen - Drills #2 Homework

Elixir
Transcript

English (Auto-generated)

Hello. So welcome back to my learned with the elixir series today, we're going to be covering the homework from the drills and tips Part two video. Um The first one didn't have any homework. Um You did the solution right in the video so there's nothing for me to do for that one so I didn't skip one, don't worry. Um But this one is actually going to be building on the fizz buzz stuff we did in the actual drills and tips video. Um So make sure you watch that video, the actual lesson first or else you're gonna be missing a lot of the actual code. So in our solutions we did a big fizz buzz function um or module with some different functions um with different parameters and then the ability to run it so that it'll do the classic print fizz or buzz or fizz buzz at different points. So now what we wanna do is we want to add these variations to it. So I'm gonna go ahead and copy the first variation over to the homework file here. Perfect. And I'm actually going to bring over um we're gonna bring over the last solution that we did here as well. So of the function, return a list instead of printing. So the return list will look something like 12 fizz for buzz fizz blah blah blah blah blah. So for now what I'm going to do is let's clear this well let's do this one here so let's go ahead and run this. The terminal CB Let's not re on 14 Around 13. Um and homework. Um Sorry migraine apparently isn't working. Perfect. So we're gonna get our face busting here. So basically all we want instead of printing it individually we want to print it into a list. So here we're doing in um dot each over Every individual one. we want to change that to go into an actual list. So let's do let's go to the Mhm. In a library and what are functions here. And I think it'll be one of our map functions. So we're trying to listen to each element as a result of invoking it. So we get our range and we get our ah non dysfunction. My brain is really not thinking of words very well today. So let's do even dot map instead of doing equal the map and see what our results are there. So let's go ahead and clear this 1. 2 Blah Blah Blah returns a list but this one accepts a range better. So let's do okay okay perhaps the function expects a key value to bowl. Got it. Okay so let's do math. Every thank you And we just need to set how many we want when we want to go over it. So we want to do it for every single item. So I feel for one and let's oh my gosh of course this is our issue. So let's go back to map and see if it works better for our map one there because I feel like it should work for map. Um The issues that we're doing I. O. Puts here so we want to get rid of the io puts because we're going to be definitely overriding what we're asking you to do here. Mhm. Alright okay let's try this again. Yeah I really can't type today, can I? Yeah. Perfect. So there's our list just like we're wanting it. So that is the first portion done there, Something like this one up. Um And then I'm just gonna go ahead and comment this out there. Then We are going to go ahead and copy the second homework one which is going to see how the function returns a map that looks like this. So we will go ahead and also grab this and copy that down. Um It's of course bad practice in actual code to have more than one module profile. We'd only want one file to contain one module but for the sake of the homework I'm just gonna do it all into a single file for now. Um But do note that is of course not. Great practice. Alright so we want to return a map and we're working with just individual strings or integers depending right now. So what I'm going to do is we're going to start by turning we want to have it, we want to map key value pairs. So what I want to do is I'm gonna start by turning it into we need the start numbers to be key value pairs and we need the results of the function to be key value pairs. So I'm gonna do it will be starting um already and then I'll turn this into a list. They did this on the wrong function. Mhm. We will do starting them. Great list. And in fact, yeah, the list is undefined or private. Yeah, it's two lists. Not list. Mhm. Perfect. So we're gonna get this warning because there's multiple versions of um fizz buzz in here. We actually will just change this to be fizz buzz one because it'll stop giving us that error every time. Um but we can see we have the list now of the 1-100. So what we want to do is we want to also create a list of the output of the um fizz buzz program. So what we're gonna do is instead of creating a second variable, we're going to I'm gonna do kind of a workaround. So what we could do would be, let's say um is safe if buzz is equal to and grab this mm hmm There. Yeah. And then did that. There might be a better way to do it. Um And then we can use a function called zip. And what's it's gonna do is it takes different innumerable als and system together based off of the index essentially. So we will do in um that's it and it's automatically going to be taking in this list as the first one, but we're gonna want to also pass it for Fisma is there but sugar Curtis a list of these two poles here, which is exactly what we are wanting, but now we need it to be a map. So weak from this, We can just actually do map dot you Okay, apps will always print out of order. Um I did confirm with tim who is the creator of it, that this was not intentionally ordered? Um He just kind of didn't via habit, but it is there always going to be on ordered? Um That's how maps work. If you wanted it to be ordered, you would use a keyword list. So this is the first solution, it's a little longer. Um You could you could just do this and get rid of that there, get rid of this variable and have it save a little bit of space. There's actually an even simpler way of doing it. So let's comment this out and what we're going to do is we're going to use e a mm dot reduce function, so we're going to do them dot reduced. So let me go to this reviews function here. So the reduced function takes There's a 1-1 with three reduced function. Takes. Um Are it's like are innumerable. Um It invokes a function over it as well as the accumulator, so I know it's kind of confusing um reduced functions are a little bit hard to wrap your brain around if you've never used them before. Um But I'll show you how this is working in our code here. So what we're gonna do is we're going to give it that first. Innumerable, which is going to be our list of our numbers here. So we're going to do, we're just gonna arrange, let's put the starting um about 100 then we're going to pass it an empty map which is our accumulator. So we're accumulating it inside of this. Does that make sense? Hopefully. And now we're going to give it our anonymous function. So we're gonna do fn and it's going to take two parameters. It's going to take the element and the accumulator coming in there. And what we're gonna do is we're going to use a map function called Mac dot put. And inside of that map function, we're going to give it our accelerator, where it's going to put it accelerated celebrator accumulator, where we're going to put it, which is going to be this so we're gonna put it there, we are going to give it X. Which is this and we are going to also give it okay, the print numb or fizz buzz function down here, accepting X. Is of value, so, and see this does the exact same thing, so basically how this is working. Let me show you reduce again here, so, and folks function for each element in the innumerable with the accumulator. The initial value of the accumulators act. The function, vote for each element. The numeral function is invoked for each element in the new world with the accumulator. So the accumulator could be um This it's not they're using it to say add things to um but it's kind of a difficulty of explaining it again, I would not be the best person to explain reduce functions. Um I would recommend looking into them a little bit because they are a little difficult to kind of wrap your brain around. But what our code is essentially doing is it's taking in this range which is our numerator are innumerable is taking in this as our accumulator. And then we are having an anonymous function that's taking each element of the innumerable, which will be X will be one and two and three and four. And the map and that which is our accumulator. And then we're saying please add So let me look at that map dot put function, I can show you guys. Um So it puts the value under keon map. So what we're doing here is we're giving this is the key and then this is actually going to be our value. So you can see if it we're putting this key value, the map, the key and the value which is all we're doing here. We're giving it the map, which is our accumulator, the key, which is what's enumerating over this reduced function. And then we're actually running a different function on each of the values of the range for the visitors function. You know it's a little confusing but hopefully it makes sense. Um Both work. This is obviously a little bit harder to read. This is clearer to read it in my opinion. Um It's just more code. Alright so let me go ahead and comment that one out there and now we're gonna go ahead and do the final drill or the final homework here. Ah Alright so have a function returning keyword list. It looks like 01122 fizz 34. And then it also says sorry wrong one there. Um The and then answer the question, why did I have to use quotes around the keys for the keyword list and they couldn't and couldn't do 01122 fizz So there's a couple things to remember about keyword list here um And the first one being that of course they are going to be an ordered list. And secondly being we have the key has to be an atom. So the first thing that we need to consider is right now our ranges are just integers. So what we're gonna do is we're actually going to make sure that we're changing our integers into atoms. So I'm gonna assign let me actually grab course module here, we'll call this 12, we have all this code in here. I'm going to create a variable called num and then we're going to change every item in the range to an atom as opposed to a um integer. And you're gonna see why we have to have them have quotes. Um in a second. So the first thing we're gonna do is we can't actually change introduces two atoms. We have to change it to a string first, which also explains the quotes. So I'm gonna do that starting on range and then I'm going to pass it on to dot map. And I'm going to do a shorthand anonymous functions. We'll use that ampersand integer. And then we'll do two strings not to Charlie's two strings. And then because of the ampersand and the first item which will be the only item that we're using. Mm hmm. Um and then I'm actually just going to for the sake of this. I'm just gonna get it to print and not down here so we can see um now let's do I o not expect and we will do is buzz too dot run. But we now have the list of strengths. So now we can change the strings to and adam. So we will just do you know that map again? We're gonna work with the string Yes. All right this time. And then we're going to do to adam and we'll do that same there and one. So now we will get atoms of strings And this is why it has to be a string. We can't make an atom into this. So to prove that I will actually I'm just gonna hold this out for a second here. Um Let's go over to I. E. X. Actually. So let's say we have Let's say from is equal to one. So we have no um and now let's say we want to turn it into an atom. Um Even if we wanted to do something like concatenate it or um we were to do adam is equal to, I don't know if that would work. Actually, unexpected token. Yeah I don't think that would actually work. Mhm. Yeah so that just gives us the adam. I think you can do um Mhm Let's say hey people too. Yeah this is a string. Um Mhm. There's also strength. Something we can do it this way regardless. We couldn't do um string we can't we can't get introduced to Adam. It doesn't exist. So we have to have it be strings first. Um There's a way to kind of play around with this. Um And maybe I'll show you here is so this would be the best way to do it is equal to unexpected token column words. If we did a is equal to what should be reassigned is equal to run, we do get an atom there. That's a better way to show you. Anyways moving on, let's um comment this out. So the next thing we wanna do here is essentially the exact same thing we did up above. Um except we're not making it into a map. We're going to be making it into a um keyword list. So we're going to do, I'm gonna grab that item dot zip there. Let's do in m dot zip. And I give it for for for num should probably changes to numbs because it's a list of numbers, not a single number. And then the, you know, the map function that gives us each individual fisher buzzer number there. So now we have a list here where we can see that and then we just want to make it into a keyword list. So we're gonna say keyword No. Mm hmm. And I know it looks the same but it's a keyword list. And to show here. Um if we wanted to we could assign this variable. Mhm. And let's say list and we'll access it with what we want to get that fizz back. So we'll use adam and three adam. That's not adam because we need our strength. We look at this because if we do this, this actually is just a normal we can fix as well. This might actually be um I know you can see is list. His map is structures billion dysfunction. This way to check if keyword list. Active list or keyword list. Blazer. Okay. Mhm. I think it's just a list but it's just keywords because it's too cold. So we don't need to turn it into a keyword list. It's already a keyword list because it is like that which is my misunderstanding of it there so we can actually yeah just get rid of that there and then that one is a result there. So I hope that clarified it. I'm learning along with you guys and some of this of course is still rusty and um we're not rusty but new for me as well. Um But I do believe that the only difference here is that it's a list of key word is a list of two bowls which this one as we saw here. Yes, it's essentially going to be a list of key word pairs here. It's just displaying it in kind of a weird way. So if we did this list and then we did zero, I could access calls for keywords, expected the key to be an adam. Got zero. Okay so we can call the index of it. Um So yeah we would just call the keyword list so it doesn't it doesn't show it like it's two poles but it is correctly the troubles. Okay perfect. It is showing it the exact same way that's showing for tams um which is what we wanted so hopefully that helped work through some of these um work lessons freezer um and join me for the next tutorial
154 Views 0 Likes 0 Comments

Let's go over the stretch exercises for the FizzBuzz questions in drill number two!

Comment
Leave a comment (supports markdown format)