Lessons 9 - Maps, Structs, and Keyword Lists

Transcript

English (Auto-generated)

here. My user, let's say, I only define the name to john what happens here is jen is now the name of this structure looking, sorry, map looking struck, so it behaves a lot like instruct the only difference or I shouldn't say the only difference. There's a, there's a few differences here is first of all, you have to define it using the struck name in this case user. So let me just first here, we use it as such. Um, so first of all, you have to define it using this user to take advantage of the structure features. Um, the key thing here is when somebody is reading our code here, this is a lot more available. I can instantly tell, oh, this is a user, this is a map, but it's really defining a map for a particular user. Well, if I just did something like this, I may get the same effects by doing this in a particular case. Like, so let's say I do something like this, I get the same effect, but sometimes it's hard to read. That's why structures are usually used when you want to structure your application in a way where it's easier to read for the user. Another very important feature of structure is that we instruct, you could actually enforce that somebody doesn't, for instance, at an attribute that doesn't exist. So I can't do something like user name, let's say, uh, I don't know Alex and then let's say something like hobbies or you know, necessity, Vancouver, it's a city was not found. So it tells me here, it kind of like gives you that compilation error checking that tells you, hey, like this truck doesn't actually define an attribute city. So you tried to enter city while the truck doesn't define it. Um, so it will give you an error. So I'll write this down for your memories to distract will throw an error. If you try to insert attribute that wasn't defined, for example, the above will throw an error for destruct we defined earlier. So this structure here that I defined here user, this will throw an error. Uh, I'll say this is because city was not defined in this truck, so I didn't find that in destruct. So I'm going to get nearer for just so when you're working with, say web applications or applications that involve a lot of user interaction and such highly encourage you to consider struck as your first way to go because as I mentioned, you define them in a way that makes it easy to check for like attributes that were defined. Um, and um, you know, that happens when you're compiling the apps so it will actually, it won't be a runtime error. It will be an error that is that gets discovered by the compiler. So when you're preparing your application to be executed, the last data structure I want to talk about here is keyword lists. So um, your lists are basically, they are basically list structured in a way that makes them useful as key value storage. Okay. Um basically it's common in functional languages to have um to have structures that use lists if you recall lists are really commonly used. So we're going to use lists and maps a lot in in our next hour. Programming lists are good because as you know, like we do not mutate the tail of the list if you remember we always append to the beginning of the list. So it keeps the tail mutated so we could have money, different variables pointing to it. It's really easy to deal with in a functional language. So for that reason, again, in many functional languages there are ways to define something like this where you have a list of two poles. Remember we learned about two poles where they're like fixed length um data structure that stores a small amount of data. So it's common to have something like this where you have a list of um two apples. So it's a list at the end of the day. But we noticed that elixir gives us a few handy things to do it. So let me let me show you here. So if I define here my keyword list, I define it like this where what did I do? Oh sorry. So I use the shorthand syntax which I shouldn't have. So I do it like this. Now notice here. Um so let me just fix that first. Excuse me for that. Huh? All right, so notice here I define a cure list. So if you have a list, each element is a tuple and the first element is a atom. That's an important thing. It's a very specific kind of the list. But as we will see later is also quite handy in certain circumstances. So what we get we actually get the list that is treated as what you might call a keyword list. So to check the type of it here is list mike your list gives us true. So it is a um it is a list at the end of the day. Uh Sorry but what we can do here we could access it using the square notations. We can't access it using the dot notation. Like we do with the map but you can access it using the square notation which is something you don't have readily available with a normal list. With a normal list. If you want to find a particular element in a list, what usually have to do you have to go through the list? Um That too in order to do it. But with a keyword list we have the ability to access an element by the keys here. So in a keyword list we can access Element by the 1st. Um let's say by let's say by the first element of each tuple which must be an atom. So again this is why it's a very particular so you use it in a very particular way. In a very particular case. So here I'll just put this example here so I can get access so I can access the value one using my my cured list and then in square bracket a we cannot we can't use the dot notation with cured lists. I mean I mean keep in mind under the hood it will be traversing the list for you. It's just a nice convenient thing to have. Is that what you want to to do? Um so you can also define a keyword list using this notation. So you got to find the same list here using the same notation you saw in here. So I could do something like a equals this and that's exactly the same thing. That's exactly the same thing. Just different syntax sugar, you can think about it. So here we could do the same thing here. This is equivalent to the keyword list we defined above. Okay, so a couple things to note here. So a few things to note about keyword lists. Yeah, keys must be addicts. Things are stored in the exact same order. You define them or you just say you define. So for example here this order is very important. And then the last thing here a key can appear multiple times but then you have to be careful in how you use the list because that may confuse you if it appears multiple times. Are you expecting to use it in a similar way to a map? So for example here let me show you this. So in a map, if I say something like a one B two, let's say here I call this M and then said I put a again and then I give it the value 10 notice here, I get a warning. It says kiai will be overridden on a map. If you do M dot A we get 10 because what happened this ladder? Uh latter key, definitely value definition kind of overwritten this one right here because in a map you could have the key only once. Now if I have a cure list and I do the same thing, let's say A one B two and then 8 10 doesn't complain. It does, you know, does actually store both of them because remember it's a list and in a list you could actually go through each element and that's usually how useless you basically traverse a list. You go through the list, item by item in some cases and this is again where you see some differences between maps and cure list, where one may be more useful than the other depending on the context. And then if you do keep your list and you try to access a, you actually get the first one, not the latter one, even though the latter one is still there, it hasn't been like overwritten or anything like this. Unlike where basically the first one was completely eliminated. You know, it's basically not even there anymore because this one kind of overridden it when you are defining the map. So a few things to keep in mind here for the keyword list. So yeah, that's basically concludes today's lesson. So we learned about maps struck's and cure lists and you know those are you know, concludes the primary data structures. We could have you know, other uh you know, variations of data structures and such. But those are really the core ones you will need for most of your Alexa are um programming. So what I'll try to do with the next couple lessons, I will try to learn a few techniques but also try to do a variety of exercises with those data structures because once you've mastered those basic types and data structures you will be on your way to coding and eggs are available. Yeah, thank you for sticking in. And I will see you next time.
190 Views 0 Likes 0 Comments

Comment
Leave a comment (supports markdown format)