Filter, Map, Reduce - Swift - iOS Interview Questions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys we're back with another video in the iOS interview question series and today we're going to be talking about filter map and reduce not to be completely honest this question wasn't asked in a majority of the interviews however it was asked enough times that I felt it needed to be included in this series so let's dive in so at a very high level what filter map and reduced do is they iterate over a collection you know for example in an array and they make changes to it and then spit it out into a brand new array you can pretty much think of it as doing the same thing as a for loop in iterating over the array it's just a much cleaner way to write it in for example a for loop may take four or five lines of code which we'll see in a little bit whereas these filter MapReduce only take one line of code so it's a good way to clean up your code and make it less verbose alright so let's go over my playground real quick so from lines 18 and above this has nothing to do with filter map and reduce this is me just setting up my example the the story I'm going to tell you guys here but let me walk through it just so you know what's going on so on lines 5 through 9 I'm just setting up my struct which is a type device so picture an apple device it's going to have a type which is either you know iMac pro iPhone iPhone iPad Apple watch that sort of thing it has a price which is self-explanatory and then it has a color which here at the end you know space grey white black space gray space gray black and then here on line 6 11 to 16 I'm just instantiating new objects of that device type with specific values for the properties and then here on line 18 I'm throwing all those devices that I created into an array called my devices in this my devices array is what we're going to use to iterate over using you know filter map and reduce okay so first let's talk about filter here on lines 25 231 I have the old way of doing it it's just a for loop so what we want to do here is we want to filter out all the devices that are of type iPhone so you're going to get my iPhone 6 plus a my iPhone 7 up here in lines 12 and 13 because they're the only ones that have a type of iPhone so this is the old way to do it you would you know create a new array called my phones and then here on line 27 in the for loop you would iterate through my devices and then for each device if the type is equal to iPhone then you would append your new my phone's array with that device and then down here we'll go ahead and print my phones and then in the console we'll come back to this but yeah you get the gibberish but you see you have two types types iPhone price $7.99 color white so there's my white six plus and then here's the other one my black iphone 7 but this is just an example of the old way to do it now let me show you how to do it using filter and we'll do that right under the filter comment here so remember I said these these filter map and reduces they spit out the result into a brand new array so let's go and create that array let I phones equal and then we're going to iterate over the array my devices which is what we have here on line 18 so we're going to do my devices dot filter go and hit enter and if you enter here you're going to get this closure don't get me wrong you can do it this way perfectly fine but I prefer this other way that's in my opinion easier to read well once you get used to it it's easier to read at first it's kind of like what but anyway so you do open closed brackets and then just return and then dollar sign 0 which we're going to get to in a second dot type equals equals iPhone okay so what's going on here so this dollar sign 0 is basically a placeholder for the object in the array that it's iterating over it's the same exact thing if you're familiar with for loops which hopefully you are as for device in my devices so if you know the for loop this is basically every device as it goes through the array my devices is this variable called device well just imagine this device variable right here is just this dollar sign 0 so as it iterates over the array this dollar sign 0 is the placeholder for the object so this is a boolean value so basically this is only going to return if dollar sign 0 which the device type is equal to I phone so basically what this is this line is doing here is this is a boolean value so if the placeholder dot type is equal to eagled iPhone then it will append it to this new array that we created called iPhones let me repeat that because I understand it might be a little tricky so let's go back down to this for loop you know for device in my devices you know if device dot type equal is equal to iPhone upend my phones with device remember this 27 to 31 is all the way of doing it let's go ahead and comment that out to not confuse you but this is doing the same exact thing it's just all in one line of code so if dollar sign 0 which is the placeholder for the device as it's iterating over it if that type is equal to i phone then append it to this new array called iPhones so again this one line of code did the same exact thing as these you know what five six lines of code so like I said at the beginning it just cleans up your code a little bit and makes it less verbose okay let's go ahead delete this print statement down here that's printing the old my phone's from this commented out line let's go ahead and print iPhones there and then it'll be a little bit of gibberish like before but you'll be able to see the two phones we have so again here we have the iPhone at price of $7.99 color white and then iPhone price is $6.99 color black so you can see did the same exact thing we did with this for loop just in one line of code so that's the whole point of filter now map is going to be very very similar so I'll spare you the whole for loop comparison and just show you the line of code that will do the same thing for map so now let me tell the quick story all these devices here is kind of like my shopping wishlist that I want to buy and these are all the American prices for it so I made this list this is kind of like my wishlist I'm saved that money I'm going to buy it however I now I'm moving to Canada okay so now I have to adjust all these prices to Canadian prices so a quick and easy way to do that is map so what map does its map will iterate over my entire right and then apply a transform or an operation whatever you want to do to every object in that array so let's walk through some code real quick let Canadian prices and this is going to be an array of a float and it's going to equal now we're going to iterate over my devices because I want to adjust the price of every single one of my devices map and then again you could get this closure that we talked about before I prefer the Open bracket close bracket where you just put in your lips and your my parentheses floss those where you just put in your boolean value so return and then your boolean is dollar sign 0 remember that's the place holder as you're iterating over the array this time we want to adjust the price and now is where we apply our transform or operation whatever won't do to it so because Canadian prices are higher than American prices I don't know the exact conversion rate let's just call it 1.2 so what i'm doing here through my my devices arrays I'm iterating over it and then I am altering every object's price value and I'm multiplying it by 1.2 so just to prove that out let's go ahead and print Canadian prices you see now my prices have all been multiplied by 1.2 so now I have the Canadian prices for each one of my devices so map is very similar to filter instead of filtering out something you are actually just manipulating each object by an operation you can multiply it at it you know do whatever you want to it if it was text you can make it all uppercase or you know take out a letter here you can do pretty much whatever you want but the point is you are changing every object in that array okay so let's clean up a little bit let's get rid of this print statement up here to clean up my console now let's talk about reduce now I have this let's uncomment this now because this is the old way of doing it so what reduced does is reduce will combine all of your values into one so for example I have my Canadian prices here this is just a list of Canadian prices that you see down here what reduced will do in the typical example is to sum up all those prices into one but we're going to do some different stuff with that as well to show you the flexibility of reduce but the typical way of getting a total price is you have your VAR total price and then you just iterate over the prices in the array so for pricing Canadian prices you just increment your total price by that price and that gives you the total price down here on line 52 so let's take a look at that by print total price and you see here I have nine thousand one hundred seventy two point eight and that's in Canadian dollars because it's what I converted here by multiplying it by one point two so again it's you know five six seven lines of code to do that we're going to do it in one so let's check it out so again filter mapper reduce all throw their results into it a new variable so let's go and create that so let total Canadian price and that's a float equal and then we're going to do the Canadian prices collection or the array dot reduce and then as you can see we get this you know gibberish here there's a lot of different ways to write this and reduce always confuse me but I actually found a pretty simple way to do this online that really cleared it up for me so we're not even going to mess with all this stuff here and we're just going to do what's called a combined closure here and what that does is that takes in an initial value so we're gonna start at zero because we want to just get the sum of what all of these Apple devices are going to cost me to buy in Canada so zero point zero is our initial value and then we just kind of put in an operator here which is just the plus so let's actually comment out our old for loop just to avoid confusion here and then now let's go ahead and print total Canadian price and that member was like nine thousand one hundred something yep there it is nine thousand one hundred seventy two so again what this is doing is this is here's our collection of Canadian prices that we got from our map function up here on line 39 and then we just wanted to reduce that down into just sum it up so we started with our initial value and then this plus means we want to sum it up now initial value is zero so that's going to give us the sum of Apple devices but let's say I don't know again just making up a story let's say we wanted to get our total move prices to Canada and we said hey it's going to cost us you know twenty two thousand five hundred seventy eight whatever twenty two thousand five hundred sixty-seven Canadian dollars and we want to add our Apple devices to it so you just put in your initial value here about twenty two thousand and then you add you know our Canadian prices which was like nine thousand one hundred whatever and then we get that sum so again the initial value can be whatever you want it to be it can be zero if you just want that but conversely let's say I had twenty thousand dollars saved up and I wanted to just subtract off the price of all my Apple devices so I just changed that plus to a minus and then now I have my ten thousand eight hundred twenty seven which is the 20 grand - the nine thousand roughly nine thousand for my Apple devices so basically what I'm trying to get at is with this initial value and these operators I mean you can multiply and divide in this example that's going to get you some crazy numbers so I won't do that but long story short you start with initial value and then you can do your operator based on the total from your array here so the total Canadian prices so again to wrap up what filter map and reduce really do for you is it cleans up all your long messy for loops like I said it really combines something that could be five six seven eight lines of code down to one line of code so it's really nice when you're trying to make your code less verbose and more readable I like it alright so now you know how to use filter map and reduce on collections if you found this at all useful go and hit subscribe I put out new videos all the time
Info
Channel: Sean Allen
Views: 43,703
Rating: undefined out of 5
Keywords: filter map reduce swift, filter map reduce, swift filter map reduce, swift filter map, filter map swift, filter swift, filter an array swift, swift filter an array, Swift tutorial, Beginner Swift, Learning Swift, Learn Swift, Xcode, Xcode tutorial, iOS Developer, iOS Development, Swift, iOS Interview Questions, iOS Interview Questions and Answers, Swift Interview Questions, Swift Interview Questions and Answers, Software Interview Questions, Software
Id: OujkoTKEVj8
Channel Id: undefined
Length: 11min 8sec (668 seconds)
Published: Mon Jun 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.