Hey everybody what's going on this is Caleb with Debb slopes dot com.
And in this video we're going to start talking about transformations K transformations are ways that
we can modify and transform data in our swift and thankfully Thanks to our swift being functional it
allows us to basically take a stream of inputs and run them through a function the outputs of that function
we can run into another function so on and so forth.
So we're going to talk about map today.
Now map is a function and I'm going to show you something now from the developer documentation on Apple
dot com.
And the map is a function that returns an array containing the results of mapping a given closure over
the sequences elements.
So we can call map on arrays.
We can also call map on observables K because remember it's a sequence an asynchronous because remember
an observable is an asynchronous sequence.
So pretty cool.
Basically how it works is you can call map on an array or a dictionary actually in Iowa 11 as well but
you can call map on an array and what it's going to do is inside the map closure.
Think of it like a for loop.
It's going to cycle through every element and you can apply some type of transformation to the elements.
Then once map is finished it returns the array of the updated elements.
OK that's the cool thing about map.
So let's go ahead and close this and let's actually learn how it works in real life.
So pull open your X playground and we're going to right click on our latest playground and create a
new playground page.
We're going to call this map.
All righty.
Now just like before we're going to need to select all here and delete all of that information and then
we're going to just copy our three lines that we've been using and paste them in like that.
All righty.
So very cool.
Now let's begin by creating a Dispose bag like so let dispose.
Bag equals dispose bag.
Put some parentheses there to instantiate it.
And we now have a Dispose bag we can use.
So what we're going to do is we are basically going to create an observable of type and number.
We're going to give it a few numbers and then we're going to use map to transform all of the elements
in that sequence into something else.
So we're going to actually begin by creating a number formatter which is going to take our numbers and
when we run them through the map closure we're going to transform them from the actual numerical value
into the textual word or of that number like if instead of the number one we will spell out o and e
k.
That's what a number formatters going to let us do.
So let's create that now by typing let formatter equals number formatter.
And you're noticing that things showing up.
We need to import foundation so that we have the foundational elements of swift.
So go ahead and type import foundation and you'll notice it'll turn blue eventually.
But now we have access to number four matter.
Hey let's maybe try building and see if that helps.
Maybe not maybe go here maybe go back looks like it's not going to let us I'm going to save and quit
X code here pull it back open and let's open that back up again.
And here we go.
There we go.
So now you can tell that it's properly instantiated and we need to set the number style now because
this is a format so it can help us change the style so type formatter number style.
And we can use the enumeration number style basically to pull out spell out k specifies a spell out
format for example one two three four point five six seven eight is represented as one thousand two
hundred thirty four point five six seven eight.
That's the cool thing is it spells it out like you would say it.
That's the format we're going to use.
So now we're going to create an observable of type and S number and we're going to give it an array
with three values K because remember map is going to cycle through a sequence so we need to have multiple
values or well I guess with an observable can be 0 or more values just because it's an observable it's.
It's counted as a sequence or an array which is why it works with map.
So go ahead and type observable of type with the brackets they're of type and as number k and we're
going to use our DOT of function.
OK.
Now Dot of like you saw here of says creates a new observable instance with a variable number of elements
meaning we can include as many as we want.
So let's include just a couple of numbers here maybe 123 maybe 47 and maybe 9.
OK.
So we now have an observable of type and a number and it has three values inside of it.
It's a sequence of three at the moment.
Now what we can do is we can call dot map and it says that it projects each element of an observable
sequence into a new form.
So it's going to allow us to map those values onto a new form.
So go ahead and type map and instead of pressing enter and using the closure here what we can do is
we can simply press space and put two brackets k that will work the same exact way.
So it's just kind of a shortcut there.
Now I'll show you something in a second we're going to fix it now in our X Swift.
What is common is after the declaration to go before the period there and press enter and what it's
going to do is it's basically going to put all of our functions kind of indented underneath this observable
and it'll still work the same exact way.
It's just a little bit indented and a little more easy to read the flow of events because remember it's
a synchronous it goes from top to bottom.
So now that we are in our map this is sort of like our for loop if we had like four number in observable
and that's number whatever it would cycle through and we could modify that number principle for each
value.
But instead what we're going to do is inside map we're going to call formatter dot and we're going to
use the string function.
OK.
And it's going to basically take a number and it will turn it into a string so string from an S number.
And what is the N.S. number that we're going to pass in in our function.
We can use a temporary variable dollar signs 0 and what it's going to do is it's going to cycle through
all the numbers that get passed in and it's going to basically pass 123 in the place of dollar signs
zero.
Then once that's done it goes back through 47 gets passed in here then 9 gets passed in here.
What this is doing is this is basically the input here 1 2 3 4 7 and 9 those that input gets passed
into map and then at the end basically map produces a basically map produces a new element right with
a string value instead of a and s number value.
What we can do now after we have mapped all the values to turn them into the text based string we can
call a dot subscribe.
Do you see how we're sort of chaining these functions together.
We can call subscribe and then pass on next k just like we have been in previous videos reusing on next
because as soon as we use one value here we're passing a value here that produces an on next call then
47 gets passed that produces another on next call and then 9 finally produces the final on next call.
Now what we are doing here we're using map to create a new array of values.
And so what we can do is when we go into our subscriber block we're passing in that array so we pass
in the values here to map K and we go through the map function and then we pass an array of all of our
new text values into subscribe K.
What we can do then is we can print dollar signs zero and that's going to print all the values that
we just passed in look at this.
One hundred twenty three forty seven nine.
Ok that's cool.
It's printing out every value as we go through.
Then of course at the end once we have subscribed we need to tell it that we need to deallocate the
memory that it's holding.
So we can type add two disposable bag or add disposable to bag and we can pass in our dispose bag that
will handle the memory management for us.
So like I said before for every dot you want to put it on its own line that just kind of helps to consolidate
and space it out.
Now there is an issue though as you can see we are getting an optional value and that's because this
observable We don't know when the end of it is going to be.
So we don't want to pass in a nil value and so because of that it comes in as optional.
But what we can do is we can use the ternary operator to basically set a blank string value if a nil
value comes in.
So if this is good it's going to set the value to be the string based value of that number.
Otherwise we want to return a blank string.
So go ahead and type two question marks there for the ternary operator basically saying otherwise or
else we'll pass it a blank string and watch what this changes in the console.
As you can see it's now printing out a non-optional value of string for each of our numbers.
That is how map is super super cool.
We're passing in an observable here.
We're passing in these three values.
They're getting formatted and turned into a string and then we can do something with all those values.
OK.
That's super cool.
We can then at the end add it to our dispose bag and the memory is deallocated at exactly the right
time and we don't even have to think about it.
This is just one way.
That map is helpful and map is not a an Arctic swift exclusive feature map is also available in regular
swift So if you're using a regular Swift and you want to perform a very clean iteration over an array
and transform all the values.
This is a really amazing way you could do that.
So this is map this is how map works especially in ARC swift with observables.
And we're going to go ahead and move on to the next video where we'll talk about flat map which is similar
but a little bit different.
So let's head on over to the next video.
Awesome job with this one guys and we're really making a lot of progress in understanding based foundational
arcs Swift.
Soon we're going to be building apps and really cool projects with it.
So let's head over to that next video.
Awesome job guys.