Learn With Me | Intro To Elixir Lesson Nine - Maps, Structs & Keyword Lists Homework

Elixir Web Development

English (Auto-generated)

I finished the lesson nine cast yesterday on maps, trucks and keywords lists. I wasn't planning on doing a cast for this one because I kind of struggled with the lesson in general, but I spent a little bit of time kind of working through trying to figure out some components of it. So I figured I would do a cast after all and kind of show you my solutions. I have, I've kind of done these two already on my own a little bit. Um but I haven't really tackled this one so we will do it all together um and see how it goes. So as always I made a homework dot E X file where I'm gonna put the solutions. So the first one is going to be building as structured as such. So they'll destruct mimicking a person that has a first name, last name, date of birth and a list of hobbies. So I'm gonna do a death module person and then he will be abstract and we're gonna say name. Okay, it's gonna be an empty string. Oh sorry. It's gonna be first name, we need first and last name. First name is an empty string. Last name, nephew string. Hit it first. I'm just gonna shorten it because it's not really typing at all. I mean is that it's a string and not a date. Um Just because I'm just gonna put like probably a month or something like that. Um and then we'll do hobbies. I'm just going to be an empty list and that's going to be our structure there. Um What I did learn is that I can't now call destruct in this file while using the brought one. Um you see here, I can't do the elixir homework and call this truck in the file. It throws an error. So I'm actually going to use I E X. And I'm gonna go ahead and compile the file. Oh, my word see. Yeah. Mhm. Out there. So it's compiled, our person struck there and now I'm going to define a person and I will say Amy is equal to the person and I will say first name, Amy last name. Okay, 1st january and hobbies let's say um tv pulling and dogs. Why is this not ending? Okay, mm hmm. Let me try this again. Not my word. All. That's not helpful. Although today was very stressful. Okay, let's see here, let's clear this. That's okay. Mhm. Okay. So maybe it's equal to it's in first. Hey baby, Last name. 10. Your birth, february, obvious gov ducks. I do person now, so now I can do a me dot uh first name and I'll get back there. So that one was successful. Perfect. So now we will go over here and we're gonna work on merging maps. So find out how to compose a map from two maps? What happens if there's a key that exists in both of them. So let's create two maps will say map one is equal to A is one. B is two helps. A map two is equal to C. is three. D. is four. Now let's go over to our docks and we'll do elixir maps, our functions here Merge So emerges to maps in one. So we're literally just going to combine the two maps and you can see off the example here. Yeah, overwrite an existing keys so 123 and four but there's two A's here. So it's actually putting A as the second one which is what we learned in the lesson. So that answers this question. What happens if there's a key that exists in both of them? So For this we will do map.mirc map one one map I'll do io dot expect let's go ahead and put this and we will run fat and now we have one combined map right there. Perfect. So now for our last one we're going to build a module helper with functions that behave as such. Okay, so let's just start by doing the modules and so we need to print functions here. We have a map and a keyword list. So to death print. Okay see a map too. We're gonna have def print your list. Do. Okay so we're gonna do what is rap one is list and now what we want to do is say you have a map and you have a cured list. So let's do you have a map? You have a keyword list. A. is one and B is two. Okay so it says we want to try to do pattern matching. So let's see here. Let's do Okay. Okay. So we need the value to be the variable. But we're gonna keep the A. And B. Because we're gonna be expecting to receive and my prayer list with A. Or B. Here. So let's do A and let's do value A and be tell you me. Yeah. And for here we will do a w A B Phil you yeah. Yeah. Okay. You have a map and we're going to do mm hmm. So we're just going to do yeah, I would do A is sell you a Anthony is well, you know me. Okay, this is going to how do you match these against what we actually input here? Um and then we'll do the same thing for this. So this now should let's do expect I owe Donald Shipwreck. Let's do Yeah, well let's do three and four for this. Just so we can really see that the it's sure why is it getting rid of my list? This is a list that's weird. Um that could be an issue. Yes, cannot invoke local when two inside match only macros can be invoked in a match and then Oh yeah, okay. So first of all this is missing. Ah so I'm still annoyed that this isn't appearing. Okay. So we're gonna need to do when and mm hmm Perfect. So you I don't know why it gets rid of those I guess that's like allowed. Um but I or maybe I don't remember that from the bit there. All right. So you have a map. A one B. Is two, which is correct. and a three. So and let's just for that you procured list, you have a map. So it's working exactly how we wanted to be there. So, yeah. So it says use pattern matching. So we are providing it with this which is going to pattern match provided with this. It's going to pattern match value A. So it's going to essentially be this equals this. Which is going to sign three to this and four to that Or through this. And for that this one is 1 and two. Okay. I definitely found this one a little bit tricky, but hopefully um I have a better handle on it now and I'll just get better as time goes on.
65 Views 0 Likes 0 Comments

I tackle the homework for lesson 9, one of the lessons I've found the hardest so far!

Leave a comment (supports markdown format)