Implement reduce in Go [Golang Tutorial 2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everybody in today's video we are going to look at implementing reduce in go which is something you might get asked in a technical interview and might have to implement in your day-to-day job and i think go is actually really well suited for this if you don't know what reduces essentially you can think about it with some arbitrary array of things in this example i have an array of numbers and from left to right you're calling a function that you pass to reduce on that array so in this example i have my function it's gonna do some kind of work and from left to right we're gonna call each of these elements from the array one by one typically with reduce you're returning some kind of aggregate which then gets returned back up from the reduce method a really good example of this is actually the add function so add you could implement with two numbers so that's kind of our pseudo code function signature and you can call reduce on a long list of integers with add aggregating them all together inevitably returning the total of that list again that's a very broad overview and this is again something i've seen asked in technical interviews so without further ado let's jump over to the actual code implementation in go quick reminder if you want to support the work i'm doing please subscribe like comment what you want me to do next and share around the web you can also find me around the social medias on johncodes.com so here we are with just the most basic go program we could possibly have and what i want to do first is just get us an example in here so let's declare numbers and this is going to be a slice of ins and we're just going to say one two three four just like that and then i want to come down here and just call some arbitrary reduce with that list of numbers and obviously this is not going to be happy with that so let's just declare a reduce in here just to get that in there and i do want to go with our original example of using add so uh while we're at it let's just call funk add and that will be our add function now these function signatures are very important so add is going to take two numbers uh we're just going to say a b ins and that should be fine and then obviously it should also return an integer so let's come in here and just return a plus b this should be very simple all right so this chunk gets us most of the way there with the actual add function we also need to touch reduce up here a bit you need to usually pass in the method that you're going to use on each element of the slice so in here let's just pass in add now this is all complaining because we didn't touch up this function signature here and really this is the most important part in go how you declare this function signature so really this first thing is going to be we're just going to call this nums this is going to be a list of ins and then the second thing is going to be the function signature of the thing that we're going to call on each element in the slice so what that looks like let's just call this method because we're going to use some arbitrary method in the reduce and this is going to be a funk uh of type int int because we're giving it two integers and then it returns an int so this whole part right here is really just the function signature for add right there and then we're calling it method method is what we can call it within the reduce function block this is pretty happy but i'm noticing another thing really what we want in here is a result so we're going to get some results out of this this is going to complain because we don't actually declare in our function block any result so we're going to return an integer all right so now this is going to complain because there's a missing return let's just return uh return zero up in here just to make that happy and then i'm just gonna print line the result there so everything is happy it should all compile uh just for kicks and giggles let's get out of here and go run main dock go what we should see is it prints zero because we're not really doing anything with the reduce right now we're in good shape let's keep implementing a lot of the skeleton work has been done at this point and we can see that reduce takes some arbitrary list of numbers integers and that we can give it a method with this signature type and then it will return some aggregate the integer back to us so now we need to do the work to actually iterate the list and call that method on each element in the slice so usually what i like to do is have just some results we're going to call this result 0 and then in here we can actually do our iteration so we can for the element and if you don't know fours and go this is the actual index and we can ignore it with the underscore for element in the range of nums we can do some work so we can use the element that we're at and the aggregate the result as we go along so we can call not add because we want to actually call the method method we're going to do result and the element that we are at and also we need to remember to update our aggregate so result oops result equals the method that we call with the result the aggregate and the element and then let's call return result on that you know really what i want to do is i want to actually go rename this method i don't want to call it results anymore i want to call it ag for aggregate that works well all right cool beans thank you vimgo for doing that for me so now we can see we are calling the method on our aggregate for every element in the slice sweet all right let's give this a run and we can see we get 10 there i need to remind myself uh what this was okay yes one plus two is three plus three is six plus four is ten so in here uh expect to see ten all right very cool so now what i want to do is actually flush this out a little more we have a pretty generic method in here that we can call with other things so let's uh let's do this whole chunk again and instead of calling add again let's call some arbitrary sub some subtraction thing uh and i gotta remember zero minus one is negative one negative three negative six negative ten okay so we should expect to see ten printed to the screen and then negative ten so expect to see negative 10. all right we obviously don't have a sub method so we need that let's come down here funk sub it's going to be very similar to the add these are integers that it takes and it's going to return an integer we come down here return a minus b great so we have an add and a sub and we're calling reduce with both add and sub we should expect to see 10 and then minus 10. notice that we don't have to actually do any changes in the actual reduce because it takes the method that it can iterate the slice with you get the point let's give it a run main.go and we see 10 and minus 10. again i think a really really good exercise in just how go passes around functions an advanced topic and using function inside of a function signature and i've seen this asked in technical interviews before so a good brush up on some of your more advanced go thank you everybody for watching don't forget sub find me around the internet and i will catch you next time peace you
Info
Channel: John Codes
Views: 363
Rating: undefined out of 5
Keywords: golang, software engineer golang, softare engineer go, golang tutorial, golang 2021, learn golang, advanced golang, golang reduce, golang interview questions, golang function signature, john codes, johncodes, how to use go, implementing reduce in go, implement reduce in go, implement reduce, how to program reduce, how to program go, learn golang 2021, learn advanced golang, johncodes golang, golang tutorial 2021, go generics
Id: dVzYGKnD7Hc
Channel Id: undefined
Length: 8min 0sec (480 seconds)
Published: Tue Nov 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.