1
00:00:08,050 --> 00:00:11,410
Hey everybody what's going on this is Caleb with Debb slopes dot com.

2
00:00:11,420 --> 00:00:18,380
And in this video we're going to start talking about transformations K transformations are ways that

3
00:00:18,380 --> 00:00:26,450
we can modify and transform data in our swift and thankfully Thanks to our swift being functional it

4
00:00:26,450 --> 00:00:33,650
allows us to basically take a stream of inputs and run them through a function the outputs of that function

5
00:00:33,650 --> 00:00:36,260
we can run into another function so on and so forth.

6
00:00:36,470 --> 00:00:39,220
So we're going to talk about map today.

7
00:00:39,290 --> 00:00:44,900
Now map is a function and I'm going to show you something now from the developer documentation on Apple

8
00:00:44,900 --> 00:00:45,700
dot com.

9
00:00:46,190 --> 00:00:54,740
And the map is a function that returns an array containing the results of mapping a given closure over

10
00:00:54,740 --> 00:00:56,450
the sequences elements.

11
00:00:56,480 --> 00:00:59,600
So we can call map on arrays.

12
00:00:59,690 --> 00:01:08,300
We can also call map on observables K because remember it's a sequence an asynchronous because remember

13
00:01:08,390 --> 00:01:10,720
an observable is an asynchronous sequence.

14
00:01:10,730 --> 00:01:12,230
So pretty cool.

15
00:01:12,530 --> 00:01:20,480
Basically how it works is you can call map on an array or a dictionary actually in Iowa 11 as well but

16
00:01:20,480 --> 00:01:25,880
you can call map on an array and what it's going to do is inside the map closure.

17
00:01:25,880 --> 00:01:27,660
Think of it like a for loop.

18
00:01:27,770 --> 00:01:33,410
It's going to cycle through every element and you can apply some type of transformation to the elements.

19
00:01:33,500 --> 00:01:39,440
Then once map is finished it returns the array of the updated elements.

20
00:01:39,470 --> 00:01:41,550
OK that's the cool thing about map.

21
00:01:41,600 --> 00:01:45,900
So let's go ahead and close this and let's actually learn how it works in real life.

22
00:01:46,040 --> 00:01:53,270
So pull open your X playground and we're going to right click on our latest playground and create a

23
00:01:53,270 --> 00:01:54,800
new playground page.

24
00:01:54,860 --> 00:01:56,730
We're going to call this map.

25
00:01:56,840 --> 00:01:57,750
All righty.

26
00:01:58,160 --> 00:02:03,800
Now just like before we're going to need to select all here and delete all of that information and then

27
00:02:03,800 --> 00:02:10,070
we're going to just copy our three lines that we've been using and paste them in like that.

28
00:02:10,070 --> 00:02:10,640
All righty.

29
00:02:10,820 --> 00:02:11,930
So very cool.

30
00:02:11,930 --> 00:02:17,160
Now let's begin by creating a Dispose bag like so let dispose.

31
00:02:17,150 --> 00:02:20,710
Bag equals dispose bag.

32
00:02:20,750 --> 00:02:22,850
Put some parentheses there to instantiate it.

33
00:02:22,880 --> 00:02:25,720
And we now have a Dispose bag we can use.

34
00:02:25,730 --> 00:02:32,300
So what we're going to do is we are basically going to create an observable of type and number.

35
00:02:32,390 --> 00:02:38,330
We're going to give it a few numbers and then we're going to use map to transform all of the elements

36
00:02:38,960 --> 00:02:41,830
in that sequence into something else.

37
00:02:41,840 --> 00:02:47,060
So we're going to actually begin by creating a number formatter which is going to take our numbers and

38
00:02:47,060 --> 00:02:53,600
when we run them through the map closure we're going to transform them from the actual numerical value

39
00:02:53,870 --> 00:03:02,150
into the textual word or of that number like if instead of the number one we will spell out o and e

40
00:03:02,300 --> 00:03:02,900
k.

41
00:03:03,290 --> 00:03:05,480
That's what a number formatters going to let us do.

42
00:03:05,480 --> 00:03:12,170
So let's create that now by typing let formatter equals number formatter.

43
00:03:12,620 --> 00:03:15,290
And you're noticing that things showing up.

44
00:03:15,470 --> 00:03:20,180
We need to import foundation so that we have the foundational elements of swift.

45
00:03:20,180 --> 00:03:26,940
So go ahead and type import foundation and you'll notice it'll turn blue eventually.

46
00:03:27,070 --> 00:03:30,140
But now we have access to number four matter.

47
00:03:30,140 --> 00:03:32,880
Hey let's maybe try building and see if that helps.

48
00:03:32,990 --> 00:03:40,190
Maybe not maybe go here maybe go back looks like it's not going to let us I'm going to save and quit

49
00:03:40,220 --> 00:03:46,150
X code here pull it back open and let's open that back up again.

50
00:03:46,280 --> 00:03:49,940
And here we go.

51
00:03:51,980 --> 00:03:52,470
There we go.

52
00:03:52,490 --> 00:03:58,940
So now you can tell that it's properly instantiated and we need to set the number style now because

53
00:03:58,940 --> 00:04:05,950
this is a format so it can help us change the style so type formatter number style.

54
00:04:06,290 --> 00:04:15,830
And we can use the enumeration number style basically to pull out spell out k specifies a spell out

55
00:04:15,830 --> 00:04:23,120
format for example one two three four point five six seven eight is represented as one thousand two

56
00:04:23,120 --> 00:04:27,610
hundred thirty four point five six seven eight.

57
00:04:27,680 --> 00:04:30,710
That's the cool thing is it spells it out like you would say it.

58
00:04:30,800 --> 00:04:32,730
That's the format we're going to use.

59
00:04:32,790 --> 00:04:39,500
So now we're going to create an observable of type and S number and we're going to give it an array

60
00:04:39,860 --> 00:04:47,060
with three values K because remember map is going to cycle through a sequence so we need to have multiple

61
00:04:47,060 --> 00:04:52,980
values or well I guess with an observable can be 0 or more values just because it's an observable it's.

62
00:04:53,030 --> 00:04:57,940
It's counted as a sequence or an array which is why it works with map.

63
00:04:57,950 --> 00:05:06,650
So go ahead and type observable of type with the brackets they're of type and as number k and we're

64
00:05:06,650 --> 00:05:09,260
going to use our DOT of function.

65
00:05:09,410 --> 00:05:10,060
OK.

66
00:05:10,340 --> 00:05:17,360
Now Dot of like you saw here of says creates a new observable instance with a variable number of elements

67
00:05:17,390 --> 00:05:19,760
meaning we can include as many as we want.

68
00:05:19,850 --> 00:05:28,030
So let's include just a couple of numbers here maybe 123 maybe 47 and maybe 9.

69
00:05:28,360 --> 00:05:28,930
OK.

70
00:05:29,300 --> 00:05:33,780
So we now have an observable of type and a number and it has three values inside of it.

71
00:05:33,790 --> 00:05:36,050
It's a sequence of three at the moment.

72
00:05:36,050 --> 00:05:44,180
Now what we can do is we can call dot map and it says that it projects each element of an observable

73
00:05:44,180 --> 00:05:46,390
sequence into a new form.

74
00:05:46,400 --> 00:05:50,750
So it's going to allow us to map those values onto a new form.

75
00:05:50,810 --> 00:05:56,450
So go ahead and type map and instead of pressing enter and using the closure here what we can do is

76
00:05:56,450 --> 00:06:02,360
we can simply press space and put two brackets k that will work the same exact way.

77
00:06:02,360 --> 00:06:04,600
So it's just kind of a shortcut there.

78
00:06:04,670 --> 00:06:09,890
Now I'll show you something in a second we're going to fix it now in our X Swift.

79
00:06:09,890 --> 00:06:17,630
What is common is after the declaration to go before the period there and press enter and what it's

80
00:06:17,630 --> 00:06:24,350
going to do is it's basically going to put all of our functions kind of indented underneath this observable

81
00:06:24,650 --> 00:06:27,130
and it'll still work the same exact way.

82
00:06:27,200 --> 00:06:31,760
It's just a little bit indented and a little more easy to read the flow of events because remember it's

83
00:06:31,790 --> 00:06:33,640
a synchronous it goes from top to bottom.

84
00:06:33,650 --> 00:06:41,060
So now that we are in our map this is sort of like our for loop if we had like four number in observable

85
00:06:41,060 --> 00:06:45,830
and that's number whatever it would cycle through and we could modify that number principle for each

86
00:06:45,830 --> 00:06:46,380
value.

87
00:06:46,550 --> 00:06:55,400
But instead what we're going to do is inside map we're going to call formatter dot and we're going to

88
00:06:55,400 --> 00:06:57,000
use the string function.

89
00:06:57,070 --> 00:06:57,610
OK.

90
00:06:57,740 --> 00:07:05,020
And it's going to basically take a number and it will turn it into a string so string from an S number.

91
00:07:05,360 --> 00:07:09,810
And what is the N.S. number that we're going to pass in in our function.

92
00:07:09,860 --> 00:07:16,760
We can use a temporary variable dollar signs 0 and what it's going to do is it's going to cycle through

93
00:07:16,760 --> 00:07:24,660
all the numbers that get passed in and it's going to basically pass 123 in the place of dollar signs

94
00:07:24,670 --> 00:07:25,060
zero.

95
00:07:25,070 --> 00:07:30,680
Then once that's done it goes back through 47 gets passed in here then 9 gets passed in here.

96
00:07:31,100 --> 00:07:38,930
What this is doing is this is basically the input here 1 2 3 4 7 and 9 those that input gets passed

97
00:07:39,020 --> 00:07:50,780
into map and then at the end basically map produces a basically map produces a new element right with

98
00:07:51,020 --> 00:07:54,330
a string value instead of a and s number value.

99
00:07:54,410 --> 00:08:01,100
What we can do now after we have mapped all the values to turn them into the text based string we can

100
00:08:01,100 --> 00:08:03,560
call a dot subscribe.

101
00:08:03,560 --> 00:08:06,050
Do you see how we're sort of chaining these functions together.

102
00:08:06,380 --> 00:08:14,890
We can call subscribe and then pass on next k just like we have been in previous videos reusing on next

103
00:08:14,920 --> 00:08:22,870
because as soon as we use one value here we're passing a value here that produces an on next call then

104
00:08:22,900 --> 00:08:29,800
47 gets passed that produces another on next call and then 9 finally produces the final on next call.

105
00:08:29,800 --> 00:08:35,110
Now what we are doing here we're using map to create a new array of values.

106
00:08:35,140 --> 00:08:42,970
And so what we can do is when we go into our subscriber block we're passing in that array so we pass

107
00:08:42,970 --> 00:08:49,840
in the values here to map K and we go through the map function and then we pass an array of all of our

108
00:08:49,840 --> 00:08:53,030
new text values into subscribe K.

109
00:08:53,290 --> 00:08:58,960
What we can do then is we can print dollar signs zero and that's going to print all the values that

110
00:08:58,960 --> 00:09:00,430
we just passed in look at this.

111
00:09:00,430 --> 00:09:03,800
One hundred twenty three forty seven nine.

112
00:09:04,000 --> 00:09:04,990
Ok that's cool.

113
00:09:04,990 --> 00:09:07,910
It's printing out every value as we go through.

114
00:09:07,960 --> 00:09:14,710
Then of course at the end once we have subscribed we need to tell it that we need to deallocate the

115
00:09:14,710 --> 00:09:15,670
memory that it's holding.

116
00:09:15,670 --> 00:09:23,320
So we can type add two disposable bag or add disposable to bag and we can pass in our dispose bag that

117
00:09:23,320 --> 00:09:25,730
will handle the memory management for us.

118
00:09:25,900 --> 00:09:32,170
So like I said before for every dot you want to put it on its own line that just kind of helps to consolidate

119
00:09:32,200 --> 00:09:33,570
and space it out.

120
00:09:33,580 --> 00:09:40,810
Now there is an issue though as you can see we are getting an optional value and that's because this

121
00:09:40,810 --> 00:09:44,620
observable We don't know when the end of it is going to be.

122
00:09:44,620 --> 00:09:49,710
So we don't want to pass in a nil value and so because of that it comes in as optional.

123
00:09:49,780 --> 00:09:56,170
But what we can do is we can use the ternary operator to basically set a blank string value if a nil

124
00:09:56,170 --> 00:09:57,460
value comes in.

125
00:09:57,460 --> 00:10:03,470
So if this is good it's going to set the value to be the string based value of that number.

126
00:10:03,490 --> 00:10:05,560
Otherwise we want to return a blank string.

127
00:10:05,560 --> 00:10:11,410
So go ahead and type two question marks there for the ternary operator basically saying otherwise or

128
00:10:11,500 --> 00:10:16,200
else we'll pass it a blank string and watch what this changes in the console.

129
00:10:16,210 --> 00:10:22,330
As you can see it's now printing out a non-optional value of string for each of our numbers.

130
00:10:22,330 --> 00:10:25,170
That is how map is super super cool.

131
00:10:25,270 --> 00:10:27,580
We're passing in an observable here.

132
00:10:27,580 --> 00:10:29,500
We're passing in these three values.

133
00:10:29,500 --> 00:10:34,740
They're getting formatted and turned into a string and then we can do something with all those values.

134
00:10:34,750 --> 00:10:34,950
OK.

135
00:10:34,960 --> 00:10:36,400
That's super cool.

136
00:10:36,400 --> 00:10:41,290
We can then at the end add it to our dispose bag and the memory is deallocated at exactly the right

137
00:10:41,290 --> 00:10:43,670
time and we don't even have to think about it.

138
00:10:43,750 --> 00:10:45,050
This is just one way.

139
00:10:45,070 --> 00:10:52,540
That map is helpful and map is not a an Arctic swift exclusive feature map is also available in regular

140
00:10:52,540 --> 00:10:59,950
swift So if you're using a regular Swift and you want to perform a very clean iteration over an array

141
00:10:59,950 --> 00:11:02,080
and transform all the values.

142
00:11:02,320 --> 00:11:04,180
This is a really amazing way you could do that.

143
00:11:04,180 --> 00:11:09,360
So this is map this is how map works especially in ARC swift with observables.

144
00:11:09,550 --> 00:11:15,220
And we're going to go ahead and move on to the next video where we'll talk about flat map which is similar

145
00:11:15,670 --> 00:11:16,720
but a little bit different.

146
00:11:16,720 --> 00:11:18,670
So let's head on over to the next video.

147
00:11:18,670 --> 00:11:23,980
Awesome job with this one guys and we're really making a lot of progress in understanding based foundational

148
00:11:23,980 --> 00:11:24,970
arcs Swift.

149
00:11:24,970 --> 00:11:27,610
Soon we're going to be building apps and really cool projects with it.

150
00:11:27,610 --> 00:11:29,520
So let's head over to that next video.

151
00:11:29,620 --> 00:11:30,630
Awesome job guys.
