1
00:00:06,130 --> 00:00:11,260
Everybody has a go and this is Caleb with Deb's slopes dot com and I am so excited to teach you about

2
00:00:11,260 --> 00:00:14,020
swift transformations in this video.

3
00:00:14,140 --> 00:00:20,050
Basically swift transformations are functions that you can use on various collections and sequence types

4
00:00:20,050 --> 00:00:22,090
to permeate and modify the data.

5
00:00:22,190 --> 00:00:26,440
But in a very Swiftie very kind of compressed elegant way.

6
00:00:26,470 --> 00:00:32,230
So we're going to go ahead and begin by creating a swift playground so penélope an X code and pull the

7
00:00:32,230 --> 00:00:36,740
window over and we're going to click get started with a new playground.

8
00:00:37,070 --> 00:00:40,340
Can't just make a blank playground and I'm going to name it.

9
00:00:40,930 --> 00:00:47,000
Transformers like a popular movie and cartoon franchise.

10
00:00:47,000 --> 00:00:51,220
I'm going to go ahead and pull this off in full screen and I'm actually going to get rid of everything

11
00:00:51,220 --> 00:00:55,870
in here and just import foundation because that's all that we're going to need from the beginning is

12
00:00:55,870 --> 00:00:59,770
the just basic swift library nothing extra fancy.

13
00:01:00,010 --> 00:01:04,930
And so we're going to go ahead and begin by talking about one function.

14
00:01:05,030 --> 00:01:12,130
And it's called Map already and I'm going to copy and a little comment here explaining map and basically

15
00:01:12,130 --> 00:01:19,840
what it does is it returns an array containing the results of mapping the given closure over the sequences

16
00:01:19,930 --> 00:01:20,970
elements.

17
00:01:20,980 --> 00:01:27,040
So this is like a really super compressed very elegant way to do what a for loop might do where it will

18
00:01:27,130 --> 00:01:32,630
iterate through every item in a sequence and it will perform that same function to whatever item is

19
00:01:32,720 --> 00:01:33,790
iterating over.

20
00:01:33,790 --> 00:01:39,250
So for example we're going to create a variable called city names and I'm going to go ahead and give

21
00:01:39,250 --> 00:01:41,590
it a few different names here.

22
00:01:41,620 --> 00:01:46,780
We're going to just go ahead and copy and paste this a couple of times just to make it nice and easy.

23
00:01:47,060 --> 00:01:48,940
And so let's go ahead and just put in.

24
00:01:48,940 --> 00:01:52,240
You know Portland that's where I'm from Will do.

25
00:01:52,240 --> 00:01:55,720
San Luis Obispo San Luis Obispo.

26
00:01:55,720 --> 00:01:56,730
However you say it.

27
00:01:56,980 --> 00:01:59,140
That's where Mark is from.

28
00:01:59,140 --> 00:02:01,540
How about Tampa.

29
00:02:01,570 --> 00:02:05,160
That's where Jacob wishes he was from and these other ones I'll just do.

30
00:02:05,170 --> 00:02:09,050
I don't know Boston in Cupertino because you know Apple.

31
00:02:09,250 --> 00:02:09,990
So there we go.

32
00:02:10,000 --> 00:02:11,730
We've got some city names here.

33
00:02:11,740 --> 00:02:17,820
And here is where we're going to use maps so these are all obviously lowercase city names.

34
00:02:17,830 --> 00:02:22,690
But I want to make sure that every single item in this array is capitalized because obviously the city

35
00:02:22,690 --> 00:02:25,360
name is a proper noun and it should be capitalized.

36
00:02:25,360 --> 00:02:37,000
So let's create an array here call by saying Let capitalized city names equals city names.

37
00:02:37,000 --> 00:02:42,310
And what we're going to do is we're going to call dot map and like it says it returns an array.

38
00:02:42,310 --> 00:02:43,950
So that's why we're using.

39
00:02:44,050 --> 00:02:48,280
We're just going to load it directly into this constant and it's going to return the arrays and we're

40
00:02:48,280 --> 00:02:54,680
going to set a condition or not a condition but a type of modification we want to perform to every item.

41
00:02:54,850 --> 00:02:59,800
So the act to actually use this we can go ahead and use just curly brackets here right after map.

42
00:03:00,010 --> 00:03:06,640
And then in order to access one item we're going to use this symbol here dollar sign zero and what this

43
00:03:06,640 --> 00:03:13,060
is doing this is basically a placeholder variable that the value of each item is going to get set to

44
00:03:13,060 --> 00:03:13,390
this.

45
00:03:13,390 --> 00:03:16,460
With every iteration so the first iteration it's set to Portland.

46
00:03:16,510 --> 00:03:19,000
Second iteration is set to San Luis Obispo.

47
00:03:19,020 --> 00:03:20,000
Third iteration.

48
00:03:20,010 --> 00:03:21,520
Tampa et cetera et cetera.

49
00:03:21,820 --> 00:03:26,230
And because it's set to a string I can call any function that a string can do.

50
00:03:26,230 --> 00:03:33,310
So I'm going to capitalize it by calling dot capitalized and now we have an array that has iterated

51
00:03:33,310 --> 00:03:40,150
over every word in this array and it's going to go ahead and have all of the city names be capitalized.

52
00:03:40,150 --> 00:03:47,080
So if I print capitalized city names you'll see down here we get an array with all of the city names

53
00:03:47,080 --> 00:03:47,950
capitalized.

54
00:03:47,980 --> 00:03:49,120
So cool.

55
00:03:49,210 --> 00:03:54,910
So what it did is it iterated over every item in this array and it returned a new array with the modifications

56
00:03:54,910 --> 00:03:55,460
applied.

57
00:03:55,570 --> 00:03:56,770
That's that's how IMAP works.

58
00:03:56,770 --> 00:03:58,370
It's really really simple.

59
00:03:58,390 --> 00:04:05,350
Now of course that is one thing that we can do a map that's great but there are a lot of other different

60
00:04:05,350 --> 00:04:07,090
ways that we can modify data.

61
00:04:07,090 --> 00:04:09,580
Another one is called flat map.

62
00:04:09,580 --> 00:04:14,020
And so in order to show you that I'm going to paste in this description and let's read it.

63
00:04:14,200 --> 00:04:20,800
It returns an array containing the concatenated results of calling the given transformation with each

64
00:04:20,860 --> 00:04:22,700
element of this sequence.

65
00:04:22,930 --> 00:04:28,750
So what flat map is going to do is it's going to take maybe nested data.

66
00:04:28,750 --> 00:04:34,240
So an array with arrays inside or a dictionary with dictionaries inside and what it's going to do is

67
00:04:34,240 --> 00:04:40,420
it's going to remove one layer of complexity so if there were you know maybe an array with three arrays

68
00:04:40,420 --> 00:04:45,460
inside it would basically pull all the values out of those three arrays and just smoosh them together

69
00:04:45,700 --> 00:04:47,290
into one array.

70
00:04:47,290 --> 00:04:53,380
Now of course this is sort of a specific use case but there are there are very specific benefits to

71
00:04:53,380 --> 00:04:53,890
using it.

72
00:04:53,890 --> 00:04:58,810
So let's go ahead and see how this works we're going to create what I just said is you know a nested

73
00:04:58,810 --> 00:04:59,090
array.

74
00:04:59,080 --> 00:05:03,450
So let's go ahead and say var collections.

75
00:05:04,450 --> 00:05:07,230
And we're going to make a couple of different collections of numbers here.

76
00:05:07,420 --> 00:05:14,320
So let's do you know one two three and then let's not do separate numbers here.

77
00:05:14,320 --> 00:05:15,480
There we go.

78
00:05:15,550 --> 00:05:19,220
And next we're going to go ahead and do another array.

79
00:05:19,900 --> 00:05:28,140
1 5 7 following that we're going to go ahead and do another one.

80
00:05:28,300 --> 00:05:32,800
Let's do 24 Six.

81
00:05:33,160 --> 00:05:35,960
I've done one like seven times so there we go.

82
00:05:36,070 --> 00:05:39,800
So now we have three separate collections in this array.

83
00:05:39,880 --> 00:05:43,570
And we're going to use flat map to squish them down into one single array.

84
00:05:43,570 --> 00:05:51,570
So let's go ahead and type Let single collection equals collections dot flat map.

85
00:05:51,830 --> 00:05:52,440
OK.

86
00:05:52,780 --> 00:05:57,190
And what we can do is we can go ahead and just use those curly brackets and that's probably the simplest

87
00:05:57,190 --> 00:05:59,020
way to use flat map.

88
00:05:59,020 --> 00:06:03,130
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

89
00:06:03,130 --> 00:06:07,300
iterate over all those items and it's going to smush them down into a single array.

90
00:06:07,300 --> 00:06:14,010
So if I actually print this you'll see all the values are contained in a single array.

91
00:06:14,020 --> 00:06:15,500
That's pretty cool.

92
00:06:15,500 --> 00:06:22,090
But you know that's great but what if I wanted to do something with the values inside of this.

93
00:06:22,090 --> 00:06:28,420
The neat thing is that using these transformation functions we can actually nest them inside of each

94
00:06:28,420 --> 00:06:36,670
other so whatever happens to this we can actually call map on this and perform an action to modify the

95
00:06:36,670 --> 00:06:37,420
values inside.

96
00:06:37,420 --> 00:06:41,710
So let's say that we want it to return the square of all of these values.

97
00:06:41,710 --> 00:06:44,740
K whatever this value is multiplied by itself.

98
00:06:44,740 --> 00:06:53,180
So I can say let square collection equals collections dot flat map.

99
00:06:53,270 --> 00:06:55,810
OK and what we're going to do is the same thing as before.

100
00:06:55,900 --> 00:06:57,350
Dollar Sign 0.

101
00:06:57,580 --> 00:07:03,820
But instead of just doing that we're going to go ahead and call dot map on this because what it's done

102
00:07:03,820 --> 00:07:05,830
is it's returned an array right.

103
00:07:05,840 --> 00:07:07,120
Of all of the values.

104
00:07:07,240 --> 00:07:10,490
So we're going to take that array and we're going to map it.

105
00:07:10,540 --> 00:07:13,190
We're doing a map inside of a flat map.

106
00:07:13,360 --> 00:07:19,250
So to get each item we're going to go ahead and say Dollar Signs zero times dollar sign zero.

107
00:07:19,390 --> 00:07:24,340
We're taking the first item multiplying it by the first item and returning it to our flat map which

108
00:07:24,340 --> 00:07:26,110
is just an array of all those values.

109
00:07:26,110 --> 00:07:33,280
So if I print this the square collection you'll see that we get the square of every item two times two

110
00:07:33,280 --> 00:07:38,840
is four one times one is one 34 times 34 is one thousand one hundred fifty six.

111
00:07:38,890 --> 00:07:43,990
That is how we can actually link these together by nesting them and we can kind of multiply the values

112
00:07:44,440 --> 00:07:46,900
we did all of this in a single line of code.

113
00:07:46,900 --> 00:07:48,190
That's incredible.

114
00:07:48,190 --> 00:07:53,950
Normally this would take a lot more code and probably a pretty beefy function to get this same functionality.

115
00:07:53,950 --> 00:07:54,610
Pretty cool stuff.

116
00:07:54,610 --> 00:08:01,420
So let's go ahead let's move on to the next transformation which is called Compact map and compact map

117
00:08:01,450 --> 00:08:05,200
actually has one specific use case but it's very very cool.

118
00:08:05,200 --> 00:08:08,200
So let's go ahead and read what it says.

119
00:08:08,410 --> 00:08:15,760
It says right here it returns an array containing all of the non nil results of calling the given transformation.

120
00:08:15,760 --> 00:08:22,840
So this is a fantastic way to verify or I guess return only values that are not nil so let's go ahead

121
00:08:22,840 --> 00:08:26,170
and create an array that can have nil values.

122
00:08:26,170 --> 00:08:30,890
Let random returned names.

123
00:08:31,360 --> 00:08:34,230
And you know let's just pretend this is an array of string.

124
00:08:34,300 --> 00:08:37,200
Let's make it optional there so values can be nil.

125
00:08:37,330 --> 00:08:43,870
And this example would be for maybe if you got you know values pulled down from the Internet and maybe

126
00:08:43,870 --> 00:08:45,510
there was a missing piece of the data.

127
00:08:45,520 --> 00:08:48,640
So we'll say Steve was there.

128
00:08:48,760 --> 00:08:52,960
Maybe this guy wasn't so his name was nyl wasn't in the data.

129
00:08:53,060 --> 00:08:56,760
Bob let's put a.

130
00:08:57,100 --> 00:09:00,980
There we go Bob then Larry.

131
00:09:02,470 --> 00:09:07,540
And finally we'll just say the last guy was also missing so he's nil.

132
00:09:07,900 --> 00:09:12,380
You know maybe this was some some sports game or an event or something.

133
00:09:12,460 --> 00:09:18,220
And so now that we have an array that has some nil values we can call compact map to get an array that

134
00:09:18,310 --> 00:09:20,380
only includes the non-nil values.

135
00:09:20,380 --> 00:09:21,030
Check it out.

136
00:09:21,190 --> 00:09:25,700
Let valid data equals random return names.

137
00:09:25,720 --> 00:09:32,890
Compact map and using the little syntax here with the curly brackets.

138
00:09:32,890 --> 00:09:34,180
I can just return.

139
00:09:34,180 --> 00:09:38,980
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

140
00:09:38,980 --> 00:09:39,550
rather.

141
00:09:39,790 --> 00:09:44,650
And if it's non-nil it's going to return it and appended to this new array valid data.

142
00:09:44,740 --> 00:09:46,460
If it is nil it's going to just ignore it.

143
00:09:46,510 --> 00:09:47,350
Skip over it.

144
00:09:47,350 --> 00:09:54,220
So if I print out valid data you'll see down here it'll print out only the valid data which is just

145
00:09:54,220 --> 00:10:00,250
the coolest thing I could see this being really useful particularly when pulling down data from an API

146
00:10:00,250 --> 00:10:00,700
online.

147
00:10:00,700 --> 00:10:02,470
Maybe you're not sure if there's going to be data.

148
00:10:02,470 --> 00:10:07,750
Something could come back as nil and if it is is using compact math is a really great way to prevent

149
00:10:07,750 --> 00:10:09,430
your app from crashing.

150
00:10:09,430 --> 00:10:14,830
So as you can see Steve Bob and Larry are the ones that are returned the values have been ignored.

151
00:10:14,830 --> 00:10:16,990
This is so cool so so helpful.

152
00:10:16,990 --> 00:10:22,750
So that is how compact app works the point of it is to return an array with all of the non-nil values

153
00:10:22,990 --> 00:10:24,610
so so helpful.

154
00:10:24,760 --> 00:10:26,980
We've got about two more that we're going to look at.

155
00:10:26,980 --> 00:10:31,690
One is filter and the other one is called reduce and filter is pretty straightforward.

156
00:10:31,690 --> 00:10:37,060
It does exactly what it sounds like it's going to go ahead and return an array containing in order the

157
00:10:37,060 --> 00:10:40,220
elements of a sequence that satisfy a given predicate.

158
00:10:40,360 --> 00:10:44,140
So it's going to basically return values that meet a certain condition.

159
00:10:44,140 --> 00:10:51,350
So first of all I'm going to create an array full of numbers so let's just say let digits OK.

160
00:10:51,580 --> 00:10:55,740
And that's going to be an array and we're just going to make one 30.

161
00:10:55,960 --> 00:10:59,740
I'm just going to put random numbers here.

162
00:11:03,760 --> 00:11:04,340
There we go.

163
00:11:04,420 --> 00:11:04,670
OK.

164
00:11:04,690 --> 00:11:06,310
Bunch of random numbers.

165
00:11:06,400 --> 00:11:08,140
Some are even some are odd.

166
00:11:08,170 --> 00:11:09,120
Lots of different things here.

167
00:11:09,130 --> 00:11:13,900
But what we're going to do is we're going to use a filter to basically filter out any values that are

168
00:11:13,900 --> 00:11:14,790
not even.

169
00:11:14,890 --> 00:11:22,690
So our array here is going to be called even digits and filtering out the even numbers in an array is

170
00:11:22,690 --> 00:11:25,400
actually one of the first programming challenges a lot of people do.

171
00:11:25,450 --> 00:11:28,000
And this would be a super easy way to do it if you knew about this.

172
00:11:28,000 --> 00:11:31,510
So if you call digits reduce

173
00:11:35,010 --> 00:11:41,760
if you call digits filter and then use the curly brackets there what we can actually do is we can set

174
00:11:41,790 --> 00:11:46,800
a condition inside these brackets and this condition is going to be what determines whether a number

175
00:11:46,800 --> 00:11:47,550
stays or not.

176
00:11:47,560 --> 00:11:54,060
So Dollar Signs 0 if the number right member this is our placeholder variable that's when the values

177
00:11:54,060 --> 00:12:01,470
are iterating through it's being set for every value if that number Madieu low to which the Doulos symbol

178
00:12:01,470 --> 00:12:04,540
basically divides and then gives you the remainder.

179
00:12:04,590 --> 00:12:11,910
So if the remainder is 0 then you know that it's an even number right because it can be evenly divided.

180
00:12:12,120 --> 00:12:14,280
And as you can see it ran through nine times.

181
00:12:14,280 --> 00:12:21,570
And if we print out even digits we should get an array that only contains even digits just like that.

182
00:12:21,570 --> 00:12:24,890
So we filtered the array for the condition that we want.

183
00:12:24,930 --> 00:12:27,910
And you can do this with many more things not just numbers.

184
00:12:28,020 --> 00:12:32,820
I can do this with strings I could do this with names and just for an example I'm going to copy and

185
00:12:32,820 --> 00:12:39,600
paste an example in here just for time's sake I don't want to make sure this video stays concise but

186
00:12:39,600 --> 00:12:42,360
if I had an array of names like so.

187
00:12:42,540 --> 00:12:50,550
Caleb Steve Stanley Mark Maximus Ilan I went ahead and I said names not filter and I checked to see

188
00:12:50,550 --> 00:12:57,210
the first character in every item and if it's s a capital S then it goes ahead and it adds it to the

189
00:12:57,510 --> 00:12:58,600
names array.

190
00:12:58,740 --> 00:13:02,860
And as you can see when it prints out only Steve and Stanley were remaining.

191
00:13:02,880 --> 00:13:07,950
So on one line I did what would normally take a few lines of code in a for loop.

192
00:13:07,950 --> 00:13:12,600
So very very cool stuff super efficient way to filter through data.

193
00:13:12,690 --> 00:13:17,460
We have one last transformation function to check out and it is called reduce.

194
00:13:17,640 --> 00:13:23,040
And let me paste in the definition here so that you can see what it is reduce reduce it returns the

195
00:13:23,040 --> 00:13:27,920
result of combining the elements of the sequence using the given closure.

196
00:13:28,080 --> 00:13:34,110
So what we can do is we can basically take a bunch of values in an array of the same type we can call

197
00:13:34,110 --> 00:13:40,680
reduce and what it's going to do is it's going to smush them down or I guess perform a calculation on

198
00:13:40,680 --> 00:13:46,570
them and we can either say you know plus or minus multiply divide things like that.

199
00:13:46,830 --> 00:13:54,090
And so what we're going to do first is just create maybe an array of values like so and I'm going to

200
00:13:54,090 --> 00:13:58,100
make these doubles so I'm going to include some decimal points at the end.

201
00:13:58,200 --> 00:14:01,830
And so let's just do three point zero 4.7.

202
00:14:01,890 --> 00:14:04,570
Twelve point sixty five.

203
00:14:04,590 --> 00:14:11,110
How about an own nine point twelve and one eighty seven point nine.

204
00:14:11,480 --> 00:14:13,170
So we have a bunch of values here.

205
00:14:13,170 --> 00:14:17,400
They are the inferred type of double.

206
00:14:17,640 --> 00:14:20,820
And what we're going to do with these values is we're going to add them together.

207
00:14:20,970 --> 00:14:27,150
So if we say let some equals values not reduce.

208
00:14:27,150 --> 00:14:32,810
And notice here that it asks for an initial result and then next partial result.

209
00:14:33,000 --> 00:14:36,530
Things like that that is the actual technical closure.

210
00:14:36,540 --> 00:14:42,480
But there's an easier way to use it where the ending item is going to be the operator we want to apply

211
00:14:42,570 --> 00:14:43,630
to the reduction.

212
00:14:43,800 --> 00:14:48,450
So we'll say plus we want to add everything and initial result is where we started.

213
00:14:48,460 --> 00:14:49,590
Are we starting at zero.

214
00:14:49,590 --> 00:14:55,290
Are we starting at 100 and this is helpful for maybe if you know in your app a player has a certain

215
00:14:55,350 --> 00:15:00,240
score and we're going to start at their score and then add up all of the points they scored or maybe

216
00:15:00,240 --> 00:15:02,520
all the bonuses they scored during a round of play.

217
00:15:02,520 --> 00:15:08,700
So you could pass in the initial result and then add any additional values that have been appended during

218
00:15:08,790 --> 00:15:09,840
a session of game play.

219
00:15:09,840 --> 00:15:10,950
That's just one example.

220
00:15:10,950 --> 00:15:14,120
So the initial result zero point zero.

221
00:15:14,220 --> 00:15:18,180
And if you check it out over here it prints out to seventeen point three seven.

222
00:15:18,270 --> 00:15:20,940
If you were to add this up on a calculator you'd find the same thing.

223
00:15:20,940 --> 00:15:22,610
So pretty cool stuff.

224
00:15:22,650 --> 00:15:26,190
If I were to start maybe at 100 maybe that's where my user started.

225
00:15:26,190 --> 00:15:31,040
You'll see that it changes to 3:17 because it adds 100 to the beginning score.

226
00:15:31,260 --> 00:15:32,840
Pretty cool stuff.

227
00:15:32,880 --> 00:15:36,920
Now of course you can do subtract and it'll basically start at zero.

228
00:15:36,920 --> 00:15:39,660
Then subtract all these so we end up with negative.

229
00:15:39,750 --> 00:15:41,600
We could do multiply.

230
00:15:41,940 --> 00:15:46,170
And of course since 0 multiplied by anything is zero that's what we get.

231
00:15:46,170 --> 00:15:52,140
But if we did something like one for instance we get a massive number because it multiplies everything.

232
00:15:52,320 --> 00:15:54,600
This altogether multiplied by 1.

233
00:15:54,600 --> 00:15:58,550
So I believe actually what it's doing is it's taking one times three.

234
00:15:58,560 --> 00:16:03,000
So that's three times four than times 12 then times nine times 187.

235
00:16:03,000 --> 00:16:09,360
So it's doing that or at Torrid Tivoli but that's how reduce works at smashers an array down into a

236
00:16:09,360 --> 00:16:11,010
singular value.

237
00:16:11,010 --> 00:16:12,030
Pretty cool stuff.

238
00:16:12,150 --> 00:16:14,130
But you can do it with more than just numbers.

239
00:16:14,130 --> 00:16:16,260
You can actually do it with strings as well.

240
00:16:16,410 --> 00:16:19,960
And you're probably wondering why would we want to ever reduce strings.

241
00:16:20,130 --> 00:16:23,330
Well maybe you have a bunch of chunks of strings and you want to add them together.

242
00:16:23,430 --> 00:16:23,670
OK.

243
00:16:23,760 --> 00:16:24,890
Just like this.

244
00:16:24,900 --> 00:16:29,870
My name is Caleb and I am a developer and then a period at the end.

245
00:16:29,910 --> 00:16:36,940
Now if I started this I could say hey take the Chunk's this string then reduce them into a single item

246
00:16:37,270 --> 00:16:38,520
but add this at the beginning.

247
00:16:38,520 --> 00:16:39,990
This is the starting value.

248
00:16:40,240 --> 00:16:41,560
And I'm saying you know to add.

249
00:16:41,560 --> 00:16:46,050
So what we end up with as you can see here is developer bio.

250
00:16:46,060 --> 00:16:48,370
My name is Caleb and I am a developer period.

251
00:16:48,580 --> 00:16:53,100
So what it did is it smushed the chunks together into one single sentence.

252
00:16:53,110 --> 00:16:54,040
It's pretty awesome.

253
00:16:54,230 --> 00:16:55,830
Super duper easy.

254
00:16:56,140 --> 00:16:58,780
And you know what I'm going to give you a bonus one here right at the end.

255
00:16:58,790 --> 00:17:03,450
There's another transformation that I was going to exclude but it is pretty cool.

256
00:17:03,610 --> 00:17:10,600
It's called Ziph and what it does is it creates a sequence of pairs K built out of two underlying sequences.

257
00:17:10,600 --> 00:17:14,290
So what it's going to do is it's going to take two separate sequences and smoosh them together into

258
00:17:14,290 --> 00:17:15,850
something that's a little more usable.

259
00:17:15,850 --> 00:17:19,290
So I'm going to paste an example here for time's sake.

260
00:17:19,380 --> 00:17:22,750
And so that'll show you actually how Zipp works.

261
00:17:22,810 --> 00:17:28,570
But basically I made an array here of name data and then an array of badge number data.

262
00:17:28,570 --> 00:17:35,680
Maybe this is an employee database Berrys number is this genos number is this divans number is this.

263
00:17:35,890 --> 00:17:43,030
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

264
00:17:43,030 --> 00:17:48,340
and we're not actually able to call it on one item right because it zips 2 together.

265
00:17:48,460 --> 00:17:53,020
But it is a transformation because it works in the same exact ways it's transforming data into a new

266
00:17:53,020 --> 00:17:54,080
data type.

267
00:17:54,130 --> 00:18:01,510
And so basically when you call Zipp you'll notice it'll except let me show you here it'll accept let

268
00:18:01,510 --> 00:18:03,220
me try this here zip.

269
00:18:03,220 --> 00:18:03,940
There we go.

270
00:18:03,970 --> 00:18:06,430
It accepts two separate sequences right.

271
00:18:06,430 --> 00:18:08,650
An array a dictionary things like that.

272
00:18:08,710 --> 00:18:18,100
And so I can pass in the name data like you saw before and the badge number data and what ends up happening

273
00:18:18,250 --> 00:18:24,790
is we get an array of tuples or tuples However you say it and they're linked together so Barry is now

274
00:18:24,790 --> 00:18:26,200
linked with his ID badge.

275
00:18:26,200 --> 00:18:28,570
Gina is now linked with her ID badge.

276
00:18:28,570 --> 00:18:31,440
And so this is a really great way to access that.

277
00:18:31,450 --> 00:18:38,430
And let's say that we wanted to access you know Barrie's information so I could say print zipped results.

278
00:18:38,860 --> 00:18:45,090
And maybe I want to go into the first item here which is Barry of course and you can see it here.

279
00:18:45,100 --> 00:18:49,900
Now let's say that I wanted to access you know his name could call DOT 0 and that gets the first item

280
00:18:50,200 --> 00:18:52,700
as you can see on the screen here it prints out Barry.

281
00:18:52,720 --> 00:18:55,590
Same thing for his ID badge one.

282
00:18:55,870 --> 00:18:57,190
And there's his ID badge.

283
00:18:57,190 --> 00:18:57,820
Pretty cool stuff.

284
00:18:57,820 --> 00:18:59,770
So that is how zip works.

285
00:18:59,770 --> 00:19:00,920
But guys this is it.

286
00:19:00,940 --> 00:19:03,400
This is transformations we have learned a lot.

287
00:19:03,430 --> 00:19:05,600
Lots of really cool stuff here.

288
00:19:05,710 --> 00:19:07,320
We learned about math.

289
00:19:07,570 --> 00:19:14,950
We learned about flat map compact map filter reduce and zip and all of these allow you to really really

290
00:19:14,950 --> 00:19:19,560
really make your code more compact and a little more efficient even.

291
00:19:19,690 --> 00:19:25,060
We also learned about nesting various transformations inside other transformations to achieve you know

292
00:19:25,060 --> 00:19:29,700
the intended result that we wanted which was to modify the values after squishing them together.

293
00:19:29,740 --> 00:19:35,380
You know from a flat map we could have done you know a map and then a filter where we map the values

294
00:19:35,410 --> 00:19:37,960
and then we filter them based on a certain condition.

295
00:19:37,960 --> 00:19:43,510
The possibilities are basically endless and it's amazing that all of this can be fit onto one single

296
00:19:43,510 --> 00:19:43,840
line.

297
00:19:43,840 --> 00:19:45,270
Very very cool stuff.

298
00:19:45,280 --> 00:19:46,630
Thank you so much for tuning in.

299
00:19:46,630 --> 00:19:49,300
And this has been a quick video on swift transformations.
