1
00:00:08,040 --> 00:00:14,820
Hey everybody this is Caleb with devs Lopes dotcom and welcome to this video on flat map flat map is

2
00:00:14,880 --> 00:00:22,800
a function in arke swift that we can use to basically pass in and observable we call flat map on the

3
00:00:22,800 --> 00:00:23,700
observable.

4
00:00:23,910 --> 00:00:31,320
And then what we can do is we can cast each element of that observable into its own observable.

5
00:00:31,320 --> 00:00:37,200
And then with that we take all of those observable and merge them into a brand new observable of whatever

6
00:00:37,230 --> 00:00:43,210
element we decide to pull out of our original observable and we're going to show you how this works.

7
00:00:43,230 --> 00:00:48,960
It's a little bit different than map but we're going to show you how it works now so go ahead and pull

8
00:00:48,960 --> 00:00:52,260
open your X playground go ahead and.

9
00:00:52,290 --> 00:01:01,010
Right click on map click new playground page and then go ahead and select all delete and paste in your

10
00:01:01,020 --> 00:01:05,240
four lines that you need if you need to go back to map to get that that's fine.

11
00:01:05,280 --> 00:01:10,550
We need our swift playground Support Foundation and needs indefinite execution.

12
00:01:10,740 --> 00:01:16,110
So this should be called flat map like so.

13
00:01:16,440 --> 00:01:23,340
And what we're going to do is we're first going to create a Dispose bag by typing let dispose bag equals

14
00:01:23,880 --> 00:01:25,290
dispose bag.

15
00:01:25,950 --> 00:01:29,760
And as you can tell it's not showing up yet which shows me.

16
00:01:29,940 --> 00:01:30,360
Look at that.

17
00:01:30,360 --> 00:01:30,990
Now it's good.

18
00:01:31,020 --> 00:01:33,750
Looks like it works just fine.

19
00:01:33,810 --> 00:01:39,320
And now we have our dispose bag which we'll use to deallocate things when we need to.

20
00:01:39,750 --> 00:01:45,960
And what we're going to do to show how this works is I'm going to actually create a structure called

21
00:01:45,960 --> 00:01:50,800
game like a video game and we're going to give it a property of price.

22
00:01:50,820 --> 00:01:56,730
But instead of using a normal swift type we're going to go ahead and use the arc swift variable type.

23
00:01:56,730 --> 00:01:57,760
So let's go ahead and do that.

24
00:01:57,760 --> 00:02:05,220
Now let's call struct game and we're going to give it one property price.

25
00:02:05,280 --> 00:02:11,580
Now like I said this is going to be of type variable and we're going to go ahead and set it to be a

26
00:02:11,580 --> 00:02:15,360
variable of type double just like that.

27
00:02:15,720 --> 00:02:19,950
And now we can create an instance of games and set their price.

28
00:02:19,950 --> 00:02:20,890
Pretty cool.

29
00:02:21,090 --> 00:02:22,200
So let's go ahead and do that.

30
00:02:22,200 --> 00:02:29,520
We're going to type let overwatch and we're going to set that to be equal to game.

31
00:02:29,850 --> 00:02:32,690
But watch what happens since we're using a struct.

32
00:02:32,730 --> 00:02:36,870
We can just call price and give it a price.

33
00:02:36,930 --> 00:02:37,320
OK.

34
00:02:37,330 --> 00:02:41,460
Now it asks for a variable of type variable.

35
00:02:41,460 --> 00:02:47,950
So we're going to give it that variable and we're going to pass it a value OK just like so.

36
00:02:48,140 --> 00:02:50,400
Of fifty nine ninety nine.

37
00:02:50,450 --> 00:02:53,410
OK that's the price for overwatch then.

38
00:02:53,670 --> 00:02:56,090
We're going to create another game instance here.

39
00:02:56,370 --> 00:02:59,710
And this is a fun game for Playstation 4.

40
00:02:59,730 --> 00:03:02,680
And I think X-Box Maybe but I know for for sure.

41
00:03:02,700 --> 00:03:06,160
Star wall if you haven't played it you should go check it out.

42
00:03:06,330 --> 00:03:12,230
It's of type game and it has a price as well of type variable.

43
00:03:12,510 --> 00:03:16,830
OK and we're going to set it's variable property to be 1999.

44
00:03:16,830 --> 00:03:18,600
I'm just guessing on the price for that one.

45
00:03:18,750 --> 00:03:26,400
But now we have two instances of game and we've set a variable property with the price for that item.

46
00:03:26,400 --> 00:03:34,530
Now what we're going to do is we're going to create a publish subject so that we can add these games

47
00:03:34,890 --> 00:03:40,980
into the published subject and eventually we're going to print out the game's price but I'm going to

48
00:03:40,980 --> 00:03:48,180
show you how we can take one observable like a published subject and we can call flat map on it so that

49
00:03:48,180 --> 00:03:52,620
we can pass in a another observable and we can map out the values of that.

50
00:03:52,620 --> 00:03:54,990
So go ahead and type let.

51
00:03:55,030 --> 00:04:02,250
Game subject equals published subject of type game.

52
00:04:02,500 --> 00:04:03,120
OK.

53
00:04:03,470 --> 00:04:04,740
Just like that.

54
00:04:04,940 --> 00:04:10,280
And now we have a published and now we have a published subject of type game.

55
00:04:10,280 --> 00:04:16,760
So next what we need to do is we need to call our games subject and what we want to do is we want to

56
00:04:16,760 --> 00:04:25,700
extract the variable value of fifty nine ninety nine and 1999 and we want to print that to the con..

57
00:04:25,880 --> 00:04:37,410
So to do that we can type games subject subscribe and I can go ahead and type on next whoops and then

58
00:04:37,410 --> 00:04:40,670
what I can do is I can print a dollar sign zero.

59
00:04:40,710 --> 00:04:45,470
But of course nothing is going to happen because we haven't pushed in any games yet.

60
00:04:45,630 --> 00:04:47,030
So let's go do that really quick.

61
00:04:47,040 --> 00:04:54,120
Go ahead and type games subject on next and pass in overwatch and watch what happens when we pass it

62
00:04:54,120 --> 00:04:54,720
in.

63
00:04:54,720 --> 00:04:58,340
It will print out whatever subject we just passed in.

64
00:04:58,410 --> 00:04:59,210
Right.

65
00:04:59,640 --> 00:05:03,050
OK let's do that again with game subject on next.

66
00:05:03,060 --> 00:05:07,160
And we'll pass in Star wall ok really cool

67
00:05:11,190 --> 00:05:18,840
so there we go we get a print it says this is of type game with a price with a type of variable and

68
00:05:18,870 --> 00:05:20,710
a type of double k.

69
00:05:20,970 --> 00:05:27,120
But that's not what we wanted we wanted to extract the price of the game and that comes from the variable

70
00:05:27,470 --> 00:05:28,050
k.

71
00:05:28,290 --> 00:05:33,810
So what we can do instead of just subscribing and trying to print out the game itself.

72
00:05:33,810 --> 00:05:38,290
You could type game up price and you could print it out right.

73
00:05:38,520 --> 00:05:39,170
There you go.

74
00:05:39,210 --> 00:05:43,920
But the issue the variable here is of type variable.

75
00:05:43,920 --> 00:05:51,100
So the price is of type variable What can we do to get the actual value out we could type datt value.

76
00:05:51,320 --> 00:05:57,500
And when we do that we get the actual value of the game and that's cool and all but what if we wanted

77
00:05:57,500 --> 00:06:05,320
to convert this game subject of type publish subject into an observable type price.

78
00:06:05,690 --> 00:06:11,120
And what if we wanted to monitor that observable and change the price later on we could use an observable

79
00:06:11,120 --> 00:06:11,850
to do that.

80
00:06:12,050 --> 00:06:16,910
So what we're going to do instead of just subscribing right now is actually going to get rid of all

81
00:06:16,910 --> 00:06:21,430
this what we're going to do is we're going to call it a flat map.

82
00:06:21,640 --> 00:06:22,620
OK.

83
00:06:22,990 --> 00:06:30,080
Like so and we're going to put some brackets there and we're going to use flat map to basically cast

84
00:06:30,320 --> 00:06:34,140
the price into its own type of observable.

85
00:06:34,280 --> 00:06:34,890
Okay.

86
00:06:35,120 --> 00:06:39,920
And what we can do is using flat map we're going to cycle through all of the elements in our published

87
00:06:39,920 --> 00:06:40,370
subject.

88
00:06:40,370 --> 00:06:46,160
So for instance overwatch and star wall and we can use the dollar sign zero to access the game.

89
00:06:46,220 --> 00:06:46,520
Right.

90
00:06:46,520 --> 00:06:50,110
That is game that is of type game.

91
00:06:50,180 --> 00:06:57,690
Now we can pull out the price of type variable but we want to use this variable as an observable case

92
00:06:57,690 --> 00:07:06,020
so we can monitor its its value and change it and react to it accordingly so we can type Dopp as observable.

93
00:07:06,170 --> 00:07:10,820
Now the price is not only a variable it is an observable.

94
00:07:10,850 --> 00:07:15,260
So that means that we can dive into it and we can subscribe it.

95
00:07:15,560 --> 00:07:17,200
That means we can dive into it.

96
00:07:17,210 --> 00:07:22,680
We can subscribe to it and not only that but we can monitor the values and we can print them out.

97
00:07:22,790 --> 00:07:25,160
So now that the price is observable.

98
00:07:25,160 --> 00:07:26,310
Watch what I can do.

99
00:07:26,450 --> 00:07:36,260
I can call datt subscribe K I can subscribe to this observable of type price and on next K when the

100
00:07:36,260 --> 00:07:43,640
value comes in I can print dollar sign zero and that will print out the price because its now observable.

101
00:07:43,640 --> 00:07:44,550
Do you see that.

102
00:07:44,840 --> 00:07:52,130
So what I did was I passed in all of these game instances into the game subject and then I used a flat

103
00:07:52,130 --> 00:07:59,020
map to basically take the to basically take the game and I put it here.

104
00:07:59,060 --> 00:08:06,570
I pulled out its price and I set that variable to be observable then since it's observable I subscribe

105
00:08:06,570 --> 00:08:13,250
to it and when I print out the observable it print it prints out the actual value that the variable

106
00:08:13,250 --> 00:08:14,900
is which is of type double.

107
00:08:14,900 --> 00:08:16,700
Thats the coolest thing.

108
00:08:16,730 --> 00:08:21,280
So I need to do some work here though because a flat map should be down here.

109
00:08:21,350 --> 00:08:26,500
This should be tabbed and subscribe should be beneath and theres one more thing we got to do.

110
00:08:26,600 --> 00:08:28,510
We have to add this to our dispose bag.

111
00:08:28,580 --> 00:08:31,570
So go ahead and just add it to the Dispose bag there.

112
00:08:31,580 --> 00:08:40,400
Press Enter to return it down and you know let's make this pretty let's say Game price and let's encapsulate

113
00:08:40,400 --> 00:08:42,810
that there at a dollar sign.

114
00:08:43,100 --> 00:08:44,630
And there we go.

115
00:08:44,630 --> 00:08:51,200
So when it cycles through we turned the price variables into a sequence of observables and watch what

116
00:08:51,200 --> 00:08:51,620
happens.

117
00:08:51,620 --> 00:08:58,530
If I wanted to change the price of overwatch if I were to type overwatch price value and set it to be

118
00:08:58,540 --> 00:09:03,310
how about 14 ninety nine let's say it's on sale watch what happens.

119
00:09:03,320 --> 00:09:07,180
Star Wars price are over watches price drops down.

120
00:09:07,180 --> 00:09:09,370
So that's a pretty pretty good deal.

121
00:09:09,380 --> 00:09:12,960
So as you can see I can update the observable just like that.

122
00:09:12,980 --> 00:09:14,150
That's pretty neat.

123
00:09:14,150 --> 00:09:23,330
So this is flat map it's very similar to map but instead of simply passing all the values through and

124
00:09:23,330 --> 00:09:29,690
performing an operation on those values returning them as an array what we're doing instead is we are

125
00:09:29,690 --> 00:09:32,750
taking an observable like a published subject.

126
00:09:32,750 --> 00:09:38,750
We're passing it in and cycling through each individual element or in our case these two games then

127
00:09:38,780 --> 00:09:41,640
we're converting them into observables.

128
00:09:41,720 --> 00:09:47,120
And what we're doing with those is we're then passing all those observables into one single observable

129
00:09:47,270 --> 00:09:49,210
that can be cycled through and printed out.

130
00:09:49,790 --> 00:09:51,480
So it's a little bit different.

131
00:09:51,560 --> 00:09:54,670
You'll probably use map more than you will use flat map.

132
00:09:54,680 --> 00:09:57,750
But I wanted to show you how to use flat map properly.

133
00:09:57,800 --> 00:10:00,520
So this is flat map if you're confused.

134
00:10:00,620 --> 00:10:04,760
Make sure you go read the apple documentation there's a lot more information on flat map and some of

135
00:10:04,760 --> 00:10:06,680
the other use cases you could use.

136
00:10:06,770 --> 00:10:12,980
But in Arek Swift it's going to be useful for converting one type of observable into another type of

137
00:10:12,980 --> 00:10:16,280
observable by cycling through the elements and casting them as observable.

138
00:10:16,280 --> 00:10:18,050
So great job with this video.

139
00:10:18,050 --> 00:10:20,900
Let's head over to the next one where we're going to talk about filter.

140
00:10:20,900 --> 00:10:22,960
One of my favorite arcs with functions.
