a HARD algo in Rust: Part 3!!!

Transcript

English (Auto-generated)

code caster. Well I better be hez today we're going to be doing the final detail in our snail solution on code wars, those who don't know I'm talking about? You have three videos to watch, settle in, make some coffee. But for those of you who are following along we have the snail out over them where we just keep rotating it and popping off the top to kind of loop around in a spiral shape. Wonderful. So here's my complaint. Okay we have this function called rotate vek and it's like, I mean if it only applies to vectors, right? I mean one thing we've learned from object oriented programming I guess you could say is that this belongs to this type, right? Like it's bundled, this is functionality but it's bundled to a certain type. Type of data. Specifically vectors. So instead of having a bunch a bunch of functions that are like, you know, add integer, you know, reverse vek No, we say when we want to make an array of x. We don't say, you know, array to beck and then passing the vector. No, let me say array 0.2 vek Alright. Sounds like a method syntax and we're gonna definitely be This is a pretty advanced feature of rust that we're gonna be using today. This is what traits are all about. We can find traits of a type. So let's define our own trait for vectors. That's basically gonna be hey, you can rotate these things. So we'll define a trait called rotate a ble rotate a ble. Okay. And this rotate Herbal trait is going to consist of a function all rotate. So anything that's rotate herbal can be rotated and so far right. And what is it gonna what is it gonna need in order to rotate? We'll need to know the things in a rotating which will be the same, the same thing we give, it will be just itself. Whatever whatever thing we're rotating. Beautiful. We defined our trait that's all there is to traits pretty simple. Now we have to make an implementation of that trait. So let me just look again at this documentation. I always forget how to yes so we can implement this trait for another type. So in our case we're gonna implement rotate herbal for vectors. Why? Because you can say that you can rotate it but different types of data are gonna be rotated differently. So when you say you need to rotate avec, what does that mean? And what do we rotate? We don't just rotate a vector because rotating a vector doesn't make any sense. We rotate a matrix reality. We have a vector of vectors. With some data in them. Okay we're gonna want to know what T. Is. So we're gonna say hey this is a generic and rotate Herbal. Now takes a generic, you have to do it up here to I forget. No this is good. So yeah it does want us to take a generic right there and now it's just mad because I'm not implementing rotate. So we'll just say yes, okay, We don't make them public because they are public. By default. You have to make the trade public. We just define this rotate function. Gonna take itself, what will it return? We're replacing this rotate back right here. Right? So we're gonna return whatever it returns. Turn to that and let's see what the air is now. We're not returning anything. So we're gonna wanna copy this functionality as it appears here. Yeah. Okay. And it's mad because V doesn't exist. We'd expect that. So every time we say these movies were gonna say self instead, I think that's I think that's all of them. So it's still mad because it's saying, hey, this type we're defining here isn't what's defined as the return type up here can fix that real quick. Okay. Okay. Well the winter here in my editor isn't giving any errors. So this looks good to me what would be remaining? Right, okay, well obviously we won't need this function anymore so we can just delete that. Okay, And you remember why we're doing this is because instead of saying rotate vector, we just want to have a vector V and be like V dot rotate. Right? How cool would that be? So here, instead of matrix equals rotate a vector and the vector passes matrix. Okay. We wanted to be able to say is matrix dot rotate boom, matrix equals itself. That we could just have it where it changes it in place. Right? And be like matrix dot rotate. We could have done that by making it a box and changing the things that are in that memory and all of that. All right. Like, but that seems like a lot of work. I mean we can't modify vectors so we could just say like, hey, yeah, take this actual pointer. Right? Self, delete all of the sub vectors in it and then add these new ones. And if you guys want to we can I mean, let's see how we're doing on time. No, it's not. It's not showing this time. Bug is a bug in code cast, but no big deal. Let's go ahead and try to run this and see if it works. That should give us a b c f i, h g d e, a b c f I were going down h G to the left up one D and e. That's exactly what we'd expect. So it worked. There you go. Now we can say matrix dot rotate. That's a lot more rust. Like, um, it's better now we can implement this on vectors. And if we wanted to we have a trait we could add implementations for other data types, right? Like why? Why can't we rotate in a rate? I feel like in low level languages, the instinct is to modify things in place, but I have a lot of experience in lisP. So I always want to add like make a copy and create a new version and stuff like that. I mean it's less efficient, right? You have to make a whole copy of it and modify that. But it produced prevents bugs because you're you're not changing state, right? So much as you're creating new versions of things. So there's a lot less surprises. But let's see. Yeah. You know, I guess we could write we could say you could change self. Yeah, let's do it that way. We can literally just say matrix dot rotate and it literally was just rotated in place. Like how cool would that be? Yeah. You know, I gotta say I want to try it. All right. So all we'll do is we'll just drain everything from from rotated. You see how we're somewhere in here? We use drain right now? We drain we'll just drain everything so we'll go out here. Okay? And that's because I don't see how this works. Create a vector. I want to see how you delete everything in a vector. I mean, there's a billion ways you could do this, but I want to see if the way I'm thinking of doing it actually works. Let's say V equals V dot drain. You know what? Because I still won't change it in place. Let me just look it up. Let's see Rust. Rust delete from vector, plead everything in vector. Maybe there's like a clear function. That's what I mean. If not we can always implement one. Delete it totally says it right here look at this. Oh remove I said the lead. Okay to remove all elements. Use retain everything that doesn't match kevin. So we just keep certain things right. This keeps even numbers for example. Yeah and we would just I guess return false. So we'll say the dot retain. You pass it an anonymous function on this latin american keyboard that I never know what to do with and just literally return false. We don't retain no matter what and right so this retain changes it. Right? So in other words it has to be immutable and we're not using it so I should just make it an underscore and then we should now print line. I'm pretty sure this works but I just wanna make sure fine and we'll go ahead and print out this vector. Just make sure otherwise we might waste time trying a technique. It doesn't even work semi colon classic. You know there's a dead code because I'm not using snail. Alright wait a few years for the front. There we go. We have an empty vector. So we do two things. So the original ones Self. Right. Self is the vector we started with and rotated has the new stuff. So first we're gonna clear out self and then everything that's in this new good rotated one. We're gonna put in there so we'll just say self dot drain and we'll copy and we'll have to borrow this mutable, can we do that? Yeah, it wants us to define, hey, by the way, this is always mutable and it's still mad because well we'll see if and the ad helps with that. It's not happy about it. The trait bound, blah blah blah range bounds not implemented for required by abound. Okay. It's pretty, pretty specific. Mm I mean it seems like as long as we know that it's a vector, it should be fine but turning all removed elements as an iteration. So I think it needs to know what was removed. Let's see the same range boundary size not implemented, which is weird cause it's the same closure, we pass it right there. Well let's google it. Let's see if we can figure out what's going wrong here. This is what I get for my cool idea to try to change it in place. Huh? All right. Sorry, I have a look at that one more time. Do some quick googling. That was obviously not gonna be it. Mhm Yeah, I'll just look at this. So saying this specific trait that it needs may or may not be implemented. Sure. I know. Okay, so I want, I basically want this exact error. Am I the first person ever here on the whole internet, it looks like it all that's really saying is that we need to have this range bounds on whatever T is right? So the same way where we say where T equals copy, we also want to say and range bounds, thank you, something like that basically, basically somehow we're gonna need to implement that trait or we can find a different way to delete stuff. You know I don't want I don't want to copy it into an operator, like I actually don't want that. So let's look up, delete from good, remove that looks good, that's more similar to what I want everything. Let's see how remove works. So go back to our working version of the code and I'll just try removing and let's just try the first element. Let's see if this works where we think it does that remove two. I said first element but I started zero. So okay that totally gets rid of it. Let's see what happens if I say self dot remove something. Still mad. Oh it's one of the semi colon, I did have a semicolon when I would say drain right? It'd be pretty embarrassing if not okay it looks like I can remove them. So that's good because now what I can do is I can just say I can just say, hey just delete them all, I can say for I in zero up until self length. Um go ahead and remove. Right just yeah just remove whatever its first. Right now just remove that many times the first element and since I'm not using this I'll just say underscore in reality I'm just looping that many times. Now I can push in everything and rotated so I can say four row in rotated. It's mad that I'm not recurring itself now I guess. Yeah okay so it's mad because I'm now returning a reference instead of the original real thing. A mutable reference no less. I don't see why that had to be this big issue but I can't return I mean I could make it not a reference right? I think it would be unsafe though. Can I do this? You know I cannot move it out of self. I don't wanna do that anyway but we actually don't need to return anything now for changing itself. I want to think about it right? Like think about it yeah hey I just thought about this, we are no longer returning anything because we're gonna be changing self right means we don't need to return a new version so I can get rid of all of that. and yeah if height is equal to zero we can just return yeah like now it will return nothing that's what we want and same thing up here be like yep don't return anything, we're so fancy and cool. Yeah and then we can say self dot push and just push the current role and that's it wow now that is some rust code right there. Right worrying about borrowing. Not really too much borrowing but still thinking about it. Um Yeah we're saying matrix equals this. Hey those are different type because rotate returns nothing should just be able to say matrix dot rotate now. There we go. How fancy is that? Let's bring our code back and yeah you know what actually let's leave it commented and just see if we can rotate this square and then after that we'll make sure snail still works. So we'll just go ahead and say print and we'll hit square, make sure it's a vector. Because we are still taking a vector. This is a method so to speak on a vector and just rotate come. Uh So it does need to be copy able because this is going to be copied right here. Right. The problem is that I don't know if I really know how to do this. Yeah let's see. Say where you did some undoing is already forgot how I had done this before, basically gonna be the same thing I had. There we go. That's what I want copy that move forward in time again and now let's see what we got. So okay note that we wanted to be copy able. Maybe that'll do the trick right there. I don't know it's a mystery. Okay. Right so we rotate it but we never print anything because we see what rotate returns. Doesn't return anything. Okay no problem. We will say let the equal square Quebec. Now we can say V dot rotate and now the should just be rotated right and we're mutating it because we rotated. We now change it in place before that wouldn't have been an issue. We run it. We see it as a C F I, the Middle road, G E H Finally a day G should be on bottom. That's it. We did it. It's rotating. Beautiful, everyone. Beautiful. And now we can actually try scaling and see if scaling can can use this feature. So okay. Matrix dot rotate. Wonderful. Feel like this worked earlier and I just changed something. I completely forgot everything I was doing. Okay. Oh yeah, we're making a change in place. That's what we're doing now. We can just say, right, where is my head? So we get we should get a B C. And let's go down F I H G D E. There we go. So we started out with something that was basically just pseudo code II sometimes called When you write code like that using rust as a typed python, right? Where you just do it as well as python code, you know, making copies everywhere. Now we're really getting to use some of these more advanced rust features that bring us to the language, right? Like how do you do this using rust? How do we do this for a variety of types? Making our code maintainable, Making it reusable? Now we have an easy way that we can start implementing rotation on other types. I mean if you notice here, like we convert this thing to a vector, maybe we could have implemented this for a raise. Yeah, why not? In any case this is what we've got now. Uh some pretty pretty lean code. I like it for sure. But the real test has not even begun because we have to see if this final version works on code wars. You never know what you're gonna get with this. It doesn't work. That's gonna be another video. I think I've taken too much of your time. So there we go. We have snail ified. We're gonna run a test on it. Okay. It passes all of them. There was one test I sometimes pass over here. Yeah, there you go some random tests and some fixed tests. Use random data that way you can't just hard code the answers in. Alright, excellent. Let's submit that. Yeah. And I mean if we look at solutions like area, so that's our solution. If we look at the other solutions like this one is very dense, right? But we'll see a lot of it's actually not particularly readable or using a lot of Rust features. This might as well just be like javascript code a lot of it. I mean this is very clean for sure. But the point is, you know, not using Rust just doing computer science and pseudo code that happens to be using rust syntax and what's the point? You know, Rust can do all of these cool things. So why not do it? Well, there you go. I hope this was as fun for you as it was for me. I I love Rust. So any chance I get to do some toy problems, it's a blast. But but really it's only a blast when you get to do this kind of stuff, you know, when you get to be here saying, hey, you know, we had computer science here, when you get to be here saying, yeah, let's actually use Rust. You know, let's use traits. Let's let's let's use generics, Let's use cool rust features otherwise, I mean if I'm just trying to implement pseudo code and not worry about all that stuff, I could just use python or javascript. But yeah, I want to use Rust. So let's do it. There you go. It's pretty interesting. I got to use some advanced features there. Thank you a lot for your time and good bye for now.
78 Views 0 Likes 0 Comments

The final chapter...we'll use traits to make our rotation function even fancier!

Comment
Leave a comment (supports markdown format)