1
00:00:00,360 --> 00:00:02,400
Welcome to this section on generators.

2
00:00:03,120 --> 00:00:13,470
Let's consider a problem in which we want to get the explanations of numbers ranging from C zero to

3
00:00:14,850 --> 00:00:17,100
C, 100,000 or a million.

4
00:00:17,400 --> 00:00:20,400
That's one six.

5
00:00:21,180 --> 00:00:24,330
Or we could even start with a smaller number like, say, even ten.

6
00:00:24,990 --> 00:00:33,600
So if we have to go from 0 to 10, we could define E, which is simply, uh, for Iron Range ten.

7
00:00:35,260 --> 00:00:36,200
This way.

8
00:00:37,470 --> 00:00:38,130
That's fine.

9
00:00:38,190 --> 00:00:40,680
Now we're putting out E, and there we go.

10
00:00:40,680 --> 00:00:43,530
We have this list of values ranging from 0 to 9.

11
00:00:43,980 --> 00:00:51,870
And then if we want to get an explanation, we have simply to define this function exponential, which

12
00:00:51,870 --> 00:01:02,940
takes an E, and then we return a list wherein we have I, we have let's let's consider our exponent

13
00:01:02,940 --> 00:01:05,040
to be, say, a 1.01.

14
00:01:05,040 --> 00:01:09,390
So we have 1.01 to the power of E.

15
00:01:09,630 --> 00:01:10,010
All right.

16
00:01:10,020 --> 00:01:15,570
It's suppose I for I e run that and then here we go.

17
00:01:15,570 --> 00:01:21,990
We have exponent of e, we see we have 11.011.02 and so on and so forth.

18
00:01:22,770 --> 00:01:28,140
We could increase this range to say thousand or even 10,000 that would go.

19
00:01:31,970 --> 00:01:33,140
Let's take off the screens.

20
00:01:33,140 --> 00:01:33,390
Right?

21
00:01:33,410 --> 00:01:33,850
Yeah.

22
00:01:35,960 --> 00:01:36,380
Okay.

23
00:01:36,710 --> 00:01:40,550
Now we can go ahead again to increase this.

24
00:01:42,980 --> 00:01:46,520
We have an overflow because this number is quite large.

25
00:01:47,030 --> 00:01:47,420
Okay.

26
00:01:47,750 --> 00:01:49,130
So we'll see.

27
00:01:50,510 --> 00:01:50,720
Yeah.

28
00:01:50,720 --> 00:01:53,750
Everybody's opinion that's giving a reason.

29
00:01:56,780 --> 00:01:58,010
It's actually still loading.

30
00:01:58,430 --> 00:02:03,440
That's interrupt this channel that's on this.

31
00:02:06,050 --> 00:02:06,760
Run again.

32
00:02:06,780 --> 00:02:14,750
See, it takes a lot of time to load all this in memory and is still loading the data in memory.

33
00:02:15,860 --> 00:02:21,200
Let's interrupt the game and take a smaller number from that.

34
00:02:24,200 --> 00:02:24,980
Wrong again.

35
00:02:26,270 --> 00:02:32,780
This time around, it took less time and that took some time.

36
00:02:34,710 --> 00:02:35,730
There's overflow.

37
00:02:35,730 --> 00:02:44,040
So this reduced number again and again takes a while and that's what we obtain.

38
00:02:45,360 --> 00:02:48,930
Now, as you could see, there is a problem with the way we are approaching this.

39
00:02:49,710 --> 00:02:58,830
The problem is that we always have to load all this data in memory before computing this exponent for

40
00:02:58,830 --> 00:03:01,740
each element in this list.

41
00:03:04,010 --> 00:03:10,730
And given that each of those elements are actually independent, it means we could look for a more efficient

42
00:03:10,730 --> 00:03:12,530
way of dealing with this data.

43
00:03:12,920 --> 00:03:21,200
By fishing, it would mean maybe storing just the part of the list which will need for a given time

44
00:03:21,590 --> 00:03:28,670
to compute exponents and then only compute in the exponents whenever need it.

45
00:03:29,750 --> 00:03:37,160
Now, apart from this memory problem, the next problem it faces the fact that in some systems, like

46
00:03:37,160 --> 00:03:42,650
in real time systems, where data isn't already packed somewhere.

47
00:03:42,650 --> 00:03:48,140
So like if you have data in some database and that you are doing some.

48
00:03:50,110 --> 00:03:56,510
Computation on this data then is possible that you could load it up and then go this way.

49
00:03:57,010 --> 00:04:04,450
But then in some real life scenarios where this data is not necessarily located in the database but

50
00:04:04,780 --> 00:04:08,620
maybe is coming in as input streams.

51
00:04:09,460 --> 00:04:18,490
In that case, we can't actually preload this data because at a given time t the data at this one is

52
00:04:18,490 --> 00:04:19,360
inexistent.

53
00:04:20,590 --> 00:04:27,070
Now that said, oh, come on, the solution to this problem is working with generators.

54
00:04:28,240 --> 00:04:29,320
Let's see how they work.

55
00:04:30,130 --> 00:04:33,490
Now we have this list right here, which has been defined.

56
00:04:34,420 --> 00:04:37,980
Now, we could actually let's let's reduce this.

57
00:04:37,990 --> 00:04:40,350
Let's go back to it, then run this again.

58
00:04:40,360 --> 00:04:45,460
This fine would take as long as we maintain it.

59
00:04:45,520 --> 00:04:46,150
That's fine.

60
00:04:50,190 --> 00:04:58,410
We now redefine gen exponential, which takes in the number of elements we have.

61
00:04:58,410 --> 00:04:59,070
That's ten.

62
00:04:59,610 --> 00:05:02,400
So we're taking the maximum number of elements.

63
00:05:02,790 --> 00:05:09,420
And then in year we're actually going to return the exponential physical is this.

64
00:05:11,560 --> 00:05:13,120
So we're going return this.

65
00:05:13,530 --> 00:05:13,950
Yeah.

66
00:05:15,570 --> 00:05:20,190
But now, instead of doing this return, we are going to yield.

67
00:05:20,370 --> 00:05:25,860
So we generate we have this yield keyword.

68
00:05:26,250 --> 00:05:28,530
And the way it works is very simple.

69
00:05:29,280 --> 00:05:36,510
Instead of returning the whole array or the whole list, what we have is that we have this yield such

70
00:05:36,510 --> 00:05:41,790
that for every element in my list.

71
00:05:42,270 --> 00:05:43,140
So let's take this.

72
00:05:43,140 --> 00:05:44,130
Let's say this is in.

73
00:05:44,730 --> 00:05:52,050
So for every element in my list, I'm going to yield the output or I'm going to generate the output.

74
00:05:52,470 --> 00:06:01,680
So yeah, instead of coming to like define the size that we have the whole list getting into this function

75
00:06:01,680 --> 00:06:05,910
and then this function returns, also this whole list.

76
00:06:06,540 --> 00:06:14,850
Now we define in this function differently in the sense that we have this maximum number of inputs.

77
00:06:15,330 --> 00:06:23,370
And then for each and every one of them, or as they are passed in, we just simply yield the outputs.

78
00:06:24,150 --> 00:06:25,020
So run that.

79
00:06:25,290 --> 00:06:33,870
And then the way we take this values or the way we obtain the outputs from this kind of generate a function

80
00:06:34,320 --> 00:06:37,230
is by simply using the next keyword.

81
00:06:37,230 --> 00:06:40,290
So we have next gen exponential.

82
00:06:40,710 --> 00:06:43,680
What we pass is we could take, for example, ten.

83
00:06:44,730 --> 00:06:46,470
Now that's, that's okay.

84
00:06:46,470 --> 00:06:49,320
We have to specify G, for example.

85
00:06:49,680 --> 00:06:52,860
So we have this exponential, which takes M ten.

86
00:06:53,160 --> 00:06:58,590
So now we would say that the maximum number of inputs we're working with is ten, and then we'll define

87
00:06:58,590 --> 00:06:59,710
that as such.

88
00:07:00,150 --> 00:07:01,800
And then we now look for the next G.

89
00:07:04,460 --> 00:07:06,970
We have this error into the rebel.

90
00:07:07,370 --> 00:07:10,370
We arrange run again.

91
00:07:10,790 --> 00:07:11,390
That's fine.

92
00:07:11,630 --> 00:07:12,110
So we'll see.

93
00:07:12,110 --> 00:07:13,490
We have this farce elements.

94
00:07:13,880 --> 00:07:17,210
And then if we repeat this, those repeat this.

95
00:07:17,750 --> 00:07:19,040
We'll have the next element.

96
00:07:19,070 --> 00:07:20,120
Let's do some prints.

97
00:07:21,080 --> 00:07:21,800
Prints?

98
00:07:27,140 --> 00:07:30,920
See, we have this elements, this next and so on and so forth.

99
00:07:31,130 --> 00:07:37,580
Notice that because we had done oh, we had caught on this next previously we ended up having this first

100
00:07:37,580 --> 00:07:37,970
value.

101
00:07:37,970 --> 00:07:41,060
Instead we having this value here and this one.

102
00:07:41,360 --> 00:07:42,170
So let's run.

103
00:07:42,170 --> 00:07:43,350
We run this again.

104
00:07:43,370 --> 00:07:44,790
We have one zero.

105
00:07:44,810 --> 00:07:45,530
Next one.

106
00:07:46,640 --> 00:07:47,850
Let's take all this.

107
00:07:47,870 --> 00:07:49,520
One, two, three, four.

108
00:07:49,820 --> 00:07:50,070
Yeah.

109
00:07:50,990 --> 00:07:51,710
Run again.

110
00:07:52,760 --> 00:07:55,730
And we see that we have this same output right here.

111
00:07:56,630 --> 00:08:04,580
But what the difference that this time around the outputs are and just return as a whole, but instead

112
00:08:04,580 --> 00:08:08,900
they are being generated in batches of one.

113
00:08:09,950 --> 00:08:11,930
We can now go ahead and increases RN.

114
00:08:11,960 --> 00:08:15,900
So let's take for example that there we go.

115
00:08:15,900 --> 00:08:20,480
We see that unlike with this, let's take this back here.

116
00:08:24,170 --> 00:08:24,800
Run it again.

117
00:08:24,800 --> 00:08:30,790
You see, before you has to load all this in memory after loading this, we have to get into this.

118
00:08:30,800 --> 00:08:31,940
We have to get into this.

119
00:08:32,530 --> 00:08:34,070
See, that's already taking a lot of time.

120
00:08:34,400 --> 00:08:35,870
Now, let's put this a bit.

121
00:08:38,740 --> 00:08:39,220
Rob.

122
00:08:40,600 --> 00:08:45,100
So it takes some takes a while to load this in memory whereas here.

123
00:08:47,560 --> 00:08:50,170
It doesn't take that much time to load in memory.

124
00:08:51,190 --> 00:08:52,750
So you practically takes no time.

125
00:08:54,280 --> 00:08:55,270
And the job is done.

126
00:08:56,410 --> 00:09:00,610
So clearly working with generators is memory efficient.

127
00:09:01,510 --> 00:09:11,410
And also in some cases, as we said, like when we have continuous flow of data, we can actually work

128
00:09:11,410 --> 00:09:13,480
with data which doesn't exist yet.

129
00:09:13,930 --> 00:09:17,950
And in such cases we have to make use of generators like this.
