Everybody has a go and this is Caleb with Deb's slopes dot com and I am so excited to teach you about
swift transformations in this video.
Basically swift transformations are functions that you can use on various collections and sequence types
to permeate and modify the data.
But in a very Swiftie very kind of compressed elegant way.
So we're going to go ahead and begin by creating a swift playground so penélope an X code and pull the
window over and we're going to click get started with a new playground.
Can't just make a blank playground and I'm going to name it.
Transformers like a popular movie and cartoon franchise.
I'm going to go ahead and pull this off in full screen and I'm actually going to get rid of everything
in here and just import foundation because that's all that we're going to need from the beginning is
the just basic swift library nothing extra fancy.
And so we're going to go ahead and begin by talking about one function.
And it's called Map already and I'm going to copy and a little comment here explaining map and basically
what it does is it returns an array containing the results of mapping the given closure over the sequences
elements.
So this is like a really super compressed very elegant way to do what a for loop might do where it will
iterate through every item in a sequence and it will perform that same function to whatever item is
iterating over.
So for example we're going to create a variable called city names and I'm going to go ahead and give
it a few different names here.
We're going to just go ahead and copy and paste this a couple of times just to make it nice and easy.
And so let's go ahead and just put in.
You know Portland that's where I'm from Will do.
San Luis Obispo San Luis Obispo.
However you say it.
That's where Mark is from.
How about Tampa.
That's where Jacob wishes he was from and these other ones I'll just do.
I don't know Boston in Cupertino because you know Apple.
So there we go.
We've got some city names here.
And here is where we're going to use maps so these are all obviously lowercase city names.
But I want to make sure that every single item in this array is capitalized because obviously the city
name is a proper noun and it should be capitalized.
So let's create an array here call by saying Let capitalized city names equals city names.
And what we're going to do is we're going to call dot map and like it says it returns an array.
So that's why we're using.
We're just going to load it directly into this constant and it's going to return the arrays and we're
going to set a condition or not a condition but a type of modification we want to perform to every item.
So the act to actually use this we can go ahead and use just curly brackets here right after map.
And then in order to access one item we're going to use this symbol here dollar sign zero and what this
is doing this is basically a placeholder variable that the value of each item is going to get set to
this.
With every iteration so the first iteration it's set to Portland.
Second iteration is set to San Luis Obispo.
Third iteration.
Tampa et cetera et cetera.
And because it's set to a string I can call any function that a string can do.
So I'm going to capitalize it by calling dot capitalized and now we have an array that has iterated
over every word in this array and it's going to go ahead and have all of the city names be capitalized.
So if I print capitalized city names you'll see down here we get an array with all of the city names
capitalized.
So cool.
So what it did is it iterated over every item in this array and it returned a new array with the modifications
applied.
That's that's how IMAP works.
It's really really simple.
Now of course that is one thing that we can do a map that's great but there are a lot of other different
ways that we can modify data.
Another one is called flat map.
And so in order to show you that I'm going to paste in this description and let's read it.
It returns an array containing the concatenated results of calling the given transformation with each
element of this sequence.
So what flat map is going to do is it's going to take maybe nested data.
So an array with arrays inside or a dictionary with dictionaries inside and what it's going to do is
it's going to remove one layer of complexity so if there were you know maybe an array with three arrays
inside it would basically pull all the values out of those three arrays and just smoosh them together
into one array.
Now of course this is sort of a specific use case but there are there are very specific benefits to
using it.
So let's go ahead and see how this works we're going to create what I just said is you know a nested
array.
So let's go ahead and say var collections.
And we're going to make a couple of different collections of numbers here.
So let's do you know one two three and then let's not do separate numbers here.
There we go.
And next we're going to go ahead and do another array.
1 5 7 following that we're going to go ahead and do another one.
Let's do 24 Six.
I've done one like seven times so there we go.
So now we have three separate collections in this array.
And we're going to use flat map to squish them down into one single array.
So let's go ahead and type Let single collection equals collections dot flat map.
OK.
And what we can do is we can go ahead and just use those curly brackets and that's probably the simplest
way to use flat map.
And what we can actually do is just call a dollar sign 0 and what it's going to do is it's going to
iterate over all those items and it's going to smush them down into a single array.
So if I actually print this you'll see all the values are contained in a single array.
That's pretty cool.
But you know that's great but what if I wanted to do something with the values inside of this.
The neat thing is that using these transformation functions we can actually nest them inside of each
other so whatever happens to this we can actually call map on this and perform an action to modify the
values inside.
So let's say that we want it to return the square of all of these values.
K whatever this value is multiplied by itself.
So I can say let square collection equals collections dot flat map.
OK and what we're going to do is the same thing as before.
Dollar Sign 0.
But instead of just doing that we're going to go ahead and call dot map on this because what it's done
is it's returned an array right.
Of all of the values.
So we're going to take that array and we're going to map it.
We're doing a map inside of a flat map.
So to get each item we're going to go ahead and say Dollar Signs zero times dollar sign zero.
We're taking the first item multiplying it by the first item and returning it to our flat map which
is just an array of all those values.
So if I print this the square collection you'll see that we get the square of every item two times two
is four one times one is one 34 times 34 is one thousand one hundred fifty six.
That is how we can actually link these together by nesting them and we can kind of multiply the values
we did all of this in a single line of code.
That's incredible.
Normally this would take a lot more code and probably a pretty beefy function to get this same functionality.
Pretty cool stuff.
So let's go ahead let's move on to the next transformation which is called Compact map and compact map
actually has one specific use case but it's very very cool.
So let's go ahead and read what it says.
It says right here it returns an array containing all of the non nil results of calling the given transformation.
So this is a fantastic way to verify or I guess return only values that are not nil so let's go ahead
and create an array that can have nil values.
Let random returned names.
And you know let's just pretend this is an array of string.
Let's make it optional there so values can be nil.
And this example would be for maybe if you got you know values pulled down from the Internet and maybe
there was a missing piece of the data.
So we'll say Steve was there.
Maybe this guy wasn't so his name was nyl wasn't in the data.
Bob let's put a.
There we go Bob then Larry.
And finally we'll just say the last guy was also missing so he's nil.
You know maybe this was some some sports game or an event or something.
And so now that we have an array that has some nil values we can call compact map to get an array that
only includes the non-nil values.
Check it out.
Let valid data equals random return names.
Compact map and using the little syntax here with the curly brackets.
I can just return.
Dollar Signs 0 and what it's going to do is it's going to map over all of the events all of the items
rather.
And if it's non-nil it's going to return it and appended to this new array valid data.
If it is nil it's going to just ignore it.
Skip over it.
So if I print out valid data you'll see down here it'll print out only the valid data which is just
the coolest thing I could see this being really useful particularly when pulling down data from an API
online.
Maybe you're not sure if there's going to be data.
Something could come back as nil and if it is is using compact math is a really great way to prevent
your app from crashing.
So as you can see Steve Bob and Larry are the ones that are returned the values have been ignored.
This is so cool so so helpful.
So that is how compact app works the point of it is to return an array with all of the non-nil values
so so helpful.
We've got about two more that we're going to look at.
One is filter and the other one is called reduce and filter is pretty straightforward.
It does exactly what it sounds like it's going to go ahead and return an array containing in order the
elements of a sequence that satisfy a given predicate.
So it's going to basically return values that meet a certain condition.
So first of all I'm going to create an array full of numbers so let's just say let digits OK.
And that's going to be an array and we're just going to make one 30.
I'm just going to put random numbers here.
There we go.
OK.
Bunch of random numbers.
Some are even some are odd.
Lots of different things here.
But what we're going to do is we're going to use a filter to basically filter out any values that are
not even.
So our array here is going to be called even digits and filtering out the even numbers in an array is
actually one of the first programming challenges a lot of people do.
And this would be a super easy way to do it if you knew about this.
So if you call digits reduce
if you call digits filter and then use the curly brackets there what we can actually do is we can set
a condition inside these brackets and this condition is going to be what determines whether a number
stays or not.
So Dollar Signs 0 if the number right member this is our placeholder variable that's when the values
are iterating through it's being set for every value if that number Madieu low to which the Doulos symbol
basically divides and then gives you the remainder.
So if the remainder is 0 then you know that it's an even number right because it can be evenly divided.
And as you can see it ran through nine times.
And if we print out even digits we should get an array that only contains even digits just like that.
So we filtered the array for the condition that we want.
And you can do this with many more things not just numbers.
I can do this with strings I could do this with names and just for an example I'm going to copy and
paste an example in here just for time's sake I don't want to make sure this video stays concise but
if I had an array of names like so.
Caleb Steve Stanley Mark Maximus Ilan I went ahead and I said names not filter and I checked to see
the first character in every item and if it's s a capital S then it goes ahead and it adds it to the
names array.
And as you can see when it prints out only Steve and Stanley were remaining.
So on one line I did what would normally take a few lines of code in a for loop.
So very very cool stuff super efficient way to filter through data.
We have one last transformation function to check out and it is called reduce.
And let me paste in the definition here so that you can see what it is reduce reduce it returns the
result of combining the elements of the sequence using the given closure.
So what we can do is we can basically take a bunch of values in an array of the same type we can call
reduce and what it's going to do is it's going to smush them down or I guess perform a calculation on
them and we can either say you know plus or minus multiply divide things like that.
And so what we're going to do first is just create maybe an array of values like so and I'm going to
make these doubles so I'm going to include some decimal points at the end.
And so let's just do three point zero 4.7.
Twelve point sixty five.
How about an own nine point twelve and one eighty seven point nine.
So we have a bunch of values here.
They are the inferred type of double.
And what we're going to do with these values is we're going to add them together.
So if we say let some equals values not reduce.
And notice here that it asks for an initial result and then next partial result.
Things like that that is the actual technical closure.
But there's an easier way to use it where the ending item is going to be the operator we want to apply
to the reduction.
So we'll say plus we want to add everything and initial result is where we started.
Are we starting at zero.
Are we starting at 100 and this is helpful for maybe if you know in your app a player has a certain
score and we're going to start at their score and then add up all of the points they scored or maybe
all the bonuses they scored during a round of play.
So you could pass in the initial result and then add any additional values that have been appended during
a session of game play.
That's just one example.
So the initial result zero point zero.
And if you check it out over here it prints out to seventeen point three seven.
If you were to add this up on a calculator you'd find the same thing.
So pretty cool stuff.
If I were to start maybe at 100 maybe that's where my user started.
You'll see that it changes to 3:17 because it adds 100 to the beginning score.
Pretty cool stuff.
Now of course you can do subtract and it'll basically start at zero.
Then subtract all these so we end up with negative.
We could do multiply.
And of course since 0 multiplied by anything is zero that's what we get.
But if we did something like one for instance we get a massive number because it multiplies everything.
This altogether multiplied by 1.
So I believe actually what it's doing is it's taking one times three.
So that's three times four than times 12 then times nine times 187.
So it's doing that or at Torrid Tivoli but that's how reduce works at smashers an array down into a
singular value.
Pretty cool stuff.
But you can do it with more than just numbers.
You can actually do it with strings as well.
And you're probably wondering why would we want to ever reduce strings.
Well maybe you have a bunch of chunks of strings and you want to add them together.
OK.
Just like this.
My name is Caleb and I am a developer and then a period at the end.
Now if I started this I could say hey take the Chunk's this string then reduce them into a single item
but add this at the beginning.
This is the starting value.
And I'm saying you know to add.
So what we end up with as you can see here is developer bio.
My name is Caleb and I am a developer period.
So what it did is it smushed the chunks together into one single sentence.
It's pretty awesome.
Super duper easy.
And you know what I'm going to give you a bonus one here right at the end.
There's another transformation that I was going to exclude but it is pretty cool.
It's called Ziph and what it does is it creates a sequence of pairs K built out of two underlying sequences.
So what it's going to do is it's going to take two separate sequences and smoosh them together into
something that's a little more usable.
So I'm going to paste an example here for time's sake.
And so that'll show you actually how Zipp works.
But basically I made an array here of name data and then an array of badge number data.
Maybe this is an employee database Berrys number is this genos number is this divans number is this.
And what I can do is I can call zip now zip is a bit different because it is a function that we're calling
and we're not actually able to call it on one item right because it zips 2 together.
But it is a transformation because it works in the same exact ways it's transforming data into a new
data type.
And so basically when you call Zipp you'll notice it'll except let me show you here it'll accept let
me try this here zip.
There we go.
It accepts two separate sequences right.
An array a dictionary things like that.
And so I can pass in the name data like you saw before and the badge number data and what ends up happening
is we get an array of tuples or tuples However you say it and they're linked together so Barry is now
linked with his ID badge.
Gina is now linked with her ID badge.
And so this is a really great way to access that.
And let's say that we wanted to access you know Barrie's information so I could say print zipped results.
And maybe I want to go into the first item here which is Barry of course and you can see it here.
Now let's say that I wanted to access you know his name could call DOT 0 and that gets the first item
as you can see on the screen here it prints out Barry.
Same thing for his ID badge one.
And there's his ID badge.
Pretty cool stuff.
So that is how zip works.
But guys this is it.
This is transformations we have learned a lot.
Lots of really cool stuff here.
We learned about math.
We learned about flat map compact map filter reduce and zip and all of these allow you to really really
really make your code more compact and a little more efficient even.
We also learned about nesting various transformations inside other transformations to achieve you know
the intended result that we wanted which was to modify the values after squishing them together.
You know from a flat map we could have done you know a map and then a filter where we map the values
and then we filter them based on a certain condition.
The possibilities are basically endless and it's amazing that all of this can be fit onto one single
line.
Very very cool stuff.
Thank you so much for tuning in.
And this has been a quick video on swift transformations.