Learn With Me | Intro To Elixir Lesson Ten - Enum & Anonymous Functions Challenges

Elixir Web Development
Transcript

English (Auto-generated)

Hello. So I did lesson 10 on the library and the anonymous functions um and I have tried doing this catholic four times until I realized that every time I got to a new problem. It took like a while to go through everything and it was just being too long. So I did go ahead and actually do the homework separately but I still think it's worth making a cast on it and going through the solutions because I came up with because I think everyone's gonna come up with different solutions because it's one of those things where there's so many different options so um sorry that it's kind of done a little bit of events this time, it's just a lot of it was simply looking into the things and I was just sitting there reading and it was just like five minutes of me not talking and reading things and that's not fun for anyone to watch. So um let's go ahead and get started here. Um I'm gonna copy all these over actually and I'm gonna go ahead and just copy this out. All right, so make a module, I'm gonna call it helper and we're going to define all of the things as actual functions for the sake of this because they're pretty big. So for the first one we want given a list, write a code that returns a tuple of two lists, one that contains all even numbers and the other that contains all odd numbers. So for this Yeah there is is even and is odd but their guards. So in order to use them outside of the purpose of a guard. Um we have to actually require the integer model model module macro, macro. That's what I'm looking for. Sure. It's called a macro. Oh my gosh, I have been doing this for a while, my brain is shutting down. Um So I'm gonna define the function which is going to take in the list and then we want Uh two lists, one that contains all even numbers and one that contains all odd numbers. I'm gonna be very book even and then say dot filter in order to take the list. And we're going to do filter works very similar to how it works in other coding languages like javascript and stuff like that. Um You're basically giving it its filtering out things from a list or from something innumerable. Um And if the function returns true, it'll return those elements if it doesn't it won't. So we're going to use the even odd. So I'm going to say if it includes an even number it'll return it and assign it back to this even here. So we're going to do let's try using the harder method here. So we'll say and integer is even and then we're going to give it the first Because we only have one and we're going to copy that down and we're just gonna change a couple of things, we're gonna say odd is odd. And then we're going to make our tuple which is going to be even and odd and return that here. Or do I want to inspect and we're going to do Helper got even odd, we're gonna give it this list so then something annoying happens here and I'll show you um still to kind of figure out why this happens. I find it a little confusing. Um It's just it goes back to, I'll show you first and then I'll kind of explain because right now I'm just being confusing for everybody. Um So I'm going to cd into Intro and less than 10 and then we're gonna do the elixir hallmark recommended true social upbringing, I didn't want that to your quote. Oh my gosh amy, come on. Okay, so you'll notice this is happening here. Um And that's because basically it's returning it as a char list because the list is a char list and it's I don't know why he's doing it for one and not the other. I find this a little bit confusing but the workaround for this is to specify it's the inspect that's doing it. Um So the workaround is to actually tell it what we want to return it as So inside of the inspect we're going to say that we want to return the char lists as less and now when we do this it'll pick this out two lists here which is what we're wanting. So we went to the 2nd 1. So given two lists, write a piece of code that returns the list, combining the two as such. So basically we want the first list to be our key in the second list, to be our value and for them to match them up based off of the index there. So I'm gonna write another function here called Fine. Okay. Just going to take a list and then over in the in um there is a method called zip and zip essentially does exactly what we're wanting here. So you can see that it's taking These and it's assigning the 2nd 1 here. So corresponding elements from a finite collection of innumerable into a list of troubles. So it's exactly what we're wanting to happen. So we're just gonna do and you know, God zip. And actually this, sorry, this is to um we're doing two lists. So we're going to do list one and list too. I will do with one list too. And we will do I expect Albert Dock combine this one and we will choose this one if you run it. You're getting exactly what we were hoping to get up here. Alright, so given a list of strings, write a piece of code that generates a capitalized sentence that is alphabetically ordered. So we need to do three things here. We need to capitalize the individual words reorder or the individual, the individual strings inside of the list reorder them alphabetically and then actually turn them into a sentence as opposed to a list. So we wanted to be one big string as opposed to a list. So we're gonna actually be using three different functions to achieve this. So let's do death capitalize and alpha ties. Bow ties. It looks like I'm feeling that wrong. What else? Uh Half of that. Okay, wow. And we're gonna be taking a list. So let's focus on the first one that we want to do here. So we want to capitalize each element. So to do this, we're going to do that. We're actually gonna do a map. Um We're gonna take on the list and then we're going to do each one. We're gonna use the capitalized function from strings and we're gonna give it X. And and I want to go through and capitalize each one then from there we want to for them um into alphabetical order. So we go here we go to the sort function. Um We can specify how we want them sorted. Um So the sort will resort it into order, automatically ascending order. Or you can decide how the specific way you want it to sort because we just want an alphabetical order or ascending order. We're going to just use the straight single function there. So we'll do sort, This is our next one and then and you'll see what I'm doing in a second here and then we are going to be using a third function. Um That is the same. Pretty much press every language that I pretty much know which is joined. And we're going to provide it with how we want it to be joined, which is going to be like this. So typically what one way we could do this would be to create a new variable for each iteration. But that's a lot of work. So what I'm gonna do is I'm actually going to nest these. So I'm gonna put so we're gonna sort the result of this and then we're gonna take this, we're going to join it but we want a space in between. So now if we do I owe dot expect helper but capitalizing alphabetized. Let's take this sentence and we get abc Hey and yo which is exactly what we are hoping for. Their. So number four given a map, right? A piece of code that capitalizes and pretends a at the end of every element. You should get back a map such as this. So we want to capitalize the first letter of each value in the key value pairs. And we want to append a to each value. So let's create a new function um what you call it, let's call it change map terrible name. But it will serve for now and we're going to take in a map and then we're going to now be extra confusing because we're going to map over our map. I really don't like that. They're called maps because I find it particularly confusing because I'm used to maps being a method you do on something so do that and then we're going to write our anonymous functions or anonymous function here is going to have each element is going to be At two pole inside of it. Well it's not a terrible but it's a key value pair there. So we're gonna have A. And B. And we're going to take that and we want to capitalize it, but we want to capitalize the second part of it. So we're gonna leave it as it is do string dot capitalize B. And then before we return it we want to add a space to the end or not space. Sorry, that was the other one we want to add, we want to pretend the to capital A. So we're gonna concatenate strings to capital A's. So so if I do I Oh don't expect so yeah, again let me walk through this one more time. So we're mapping over everything and we are taking in our map which and then applying the anonymous function which is separating each key value pair here which is what I've done here. And then we're saying ignore the first thing. So let's focus on the value capitalize the value and concatenate A onto the end of it. So now will you help her change map. Mhm and come up. Fn is missing and yes it is. Mhm. And perfect. We have hey a abc A and you know a which is exactly what we were wanting to get back there. Oh except that is returning a list. So we want to get back a map here and I'm dot map. Um So let's do list to map. It's like a blessed where are we here? List functions. So we're taking in a map, you should get back a map. Okay, let's look at the map here. Um So it's taking in yeah, I don't want it to be a list, I would like it to be a map mm into. Okay, so let's change this here then. So we will do in um got into, we wanted to be in so perfect. Now we have our map awesome. So let's go into the last one which is given a list of students. So we have a list of students and each student is a map, write a piece of code that returns the student map of the student with the highest score. So you want Steph highest score. And now we're picking up the list. And what we're gonna do is we're gonna use this function called max by and what we're gonna do is we're going to it can receive a whole bunch of things but it also has some defaults. So what we're gonna do is we're going to look at what we're taking the max and then we're specifying what we're looking at in it and it's going to return what the max thing within it is, So for example, here we're going to say in um dot max by and we're going to take in, I'm gonna call his students because let's just confusing, so it is taking a list, but it doesn't matter, we don't need to call it a list. So students and what we want to do is we want to get the students score. So we want to look at the score of each individual item, so we could do it like F N X ext dot score. And now if we do, I owe dot inspect helper dot score, let's go ahead and take this. It's and if we run this, We're getting back jen with a score of 15, which is correct. Um But let's actually use the other way of writing this where we're making it as shortened as possible, like you showed us here, so to do that, I'm going to do it, like, And then 1%,, and then we're gonna take this first thing and we're gonna access the score. So we're saying, look at the value of score in each individual map within the list of students when everyone is the highest. That's why we're returning so to show that that works, we're just gonna do that. There we go. So those are the solutions that I came up with for today's homework, um I'm sure there's a ton of different ways we can do it because there's so many different ways to approach stuff like this. Um So I'd love to see some other people's way of how you attack these.
31 Views 0 Likes 0 Comments

Let's take on the challenges from Lesson 10!

Comment
Leave a comment (supports markdown format)