Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:06,720
So I thought of some more analysis that I'd like to do with our orders.
2
00:00:06,720 --> 00:00:12,120
Instead of just identifying the pending and completed, what if we divide it into two groups?
3
00:00:12,120 --> 00:00:13,560
Hmm, that's a good idea.
4
00:00:13,560 --> 00:00:18,520
Now we could probably do that with a combination of select and each, but that really is
5
00:00:18,520 --> 00:00:23,040
the long way around the barn because the innumerable module gives us some higher level
6
00:00:23,040 --> 00:00:26,040
methods for doing this and other tasks like it.
7
00:00:26,040 --> 00:00:27,800
So let's have a look at that.
8
00:00:27,800 --> 00:00:31,800
So you said you wanted to divide the orders and the method for doing that is actually called
9
00:00:31,800 --> 00:00:34,240
partition in the innumerable module.
10
00:00:34,240 --> 00:00:35,520
It takes a block.
11
00:00:35,520 --> 00:00:39,280
It's going to give us all the orders as we would expect here and we want a partition
12
00:00:39,280 --> 00:00:41,720
then based on their status.
13
00:00:41,720 --> 00:00:44,920
So we're going to say the status is equal to pending.
14
00:00:44,920 --> 00:00:46,920
And let's just see what that returns to us.
15
00:00:46,920 --> 00:00:51,160
I'm going to assign this to a variable called results and then I'm going to use the P method
16
00:00:51,160 --> 00:00:55,240
and pass it that array and the P method different than the put estimate that here is just
17
00:00:55,240 --> 00:00:57,680
going to show us the internal representation of that array.
18
00:00:57,680 --> 00:01:01,440
So we're going to see the array in its rawst form.
19
00:01:01,440 --> 00:01:03,960
So we run this.
20
00:01:03,960 --> 00:01:06,240
Here's the output that we're getting right here.
21
00:01:06,240 --> 00:01:11,400
And we see that we get an array, but inside of that array are actually two sub arrays.
22
00:01:11,400 --> 00:01:13,039
Here's the first sub array.
23
00:01:13,039 --> 00:01:16,440
Notice that it has the orders that have a status of pending.
24
00:01:16,440 --> 00:01:21,840
So it's the orders that match the criteria in our block and then it has a second sub array
25
00:01:21,840 --> 00:01:26,720
right here and it contains all the orders that aren't pending.
26
00:01:26,720 --> 00:01:31,280
Okay, so our results array here is actually an array of arrays.
27
00:01:31,280 --> 00:01:36,120
And there's an easy way to destructure this into two individual arrays in Ruby.
28
00:01:36,120 --> 00:01:39,920
We can do that simply by assigning to two different variables.
29
00:01:39,920 --> 00:01:45,080
So our first variable we called our pending orders and our second variable will be the
30
00:01:45,080 --> 00:01:46,440
completed orders.
31
00:01:46,440 --> 00:01:48,160
That's everything else.
32
00:01:48,160 --> 00:01:53,160
And because this method returns an array of arrays and we have two variable names separated
33
00:01:53,160 --> 00:01:58,280
by a comma on the left hand side, it'll go ahead and separate those two sub arrays out
34
00:01:58,280 --> 00:02:01,240
into these two different variables.
35
00:02:01,240 --> 00:02:06,440
So then here we can just say, okay, here are our pending orders.
36
00:02:06,440 --> 00:02:07,440
Put us pending orders.
37
00:02:07,440 --> 00:02:08,840
That's the name of the variable.
38
00:02:08,840 --> 00:02:14,120
And then here are our completed orders.
39
00:02:14,120 --> 00:02:15,120
Just like that.
40
00:02:15,120 --> 00:02:19,160
If we run this, now we see, okay, here's the heading pending.
41
00:02:19,160 --> 00:02:23,760
We've got the two pending orders and completed, the two completed orders.
42
00:02:23,760 --> 00:02:28,800
So we've got two separate arrays now, one containing pending orders, one containing completed
43
00:02:28,800 --> 00:02:29,800
orders.
44
00:02:29,800 --> 00:02:31,720
So that gave me an idea.
45
00:02:31,720 --> 00:02:35,560
We could do something similar for big orders and small orders.
46
00:02:35,560 --> 00:02:37,160
Sure, we'll just use partition again.
47
00:02:37,160 --> 00:02:38,720
I'll do this just from the beginning.
48
00:02:38,720 --> 00:02:43,280
We'll just go, we want a big orders array, we want a small orders array.
49
00:02:43,280 --> 00:02:47,640
We're going to take our orders array here and partition it, knowing that it's going to
50
00:02:47,640 --> 00:02:51,720
give us two different things back here when we destructure that.
51
00:02:51,720 --> 00:02:53,799
O is going to be the block parameter.
52
00:02:53,799 --> 00:02:58,959
In this case, we want the order total greater than or equal to 300.
53
00:02:58,959 --> 00:03:04,480
So for all the orders where this returns true, it's going to be put in this array.
54
00:03:04,480 --> 00:03:07,799
And for all the other orders, they're going to be put inside of this array.
55
00:03:07,799 --> 00:03:09,560
So we'll just print those out.
56
00:03:09,560 --> 00:03:11,640
These are big.
57
00:03:11,640 --> 00:03:18,679
And here are smaller ones.
58
00:03:18,679 --> 00:03:22,600
Run that and sure enough, a partition them just as we would expect.
59
00:03:22,600 --> 00:03:25,839
Our big orders and our smaller ones.
60
00:03:25,839 --> 00:03:29,079
So here's another example of the partition method in action.
61
00:03:29,079 --> 00:03:32,959
Let's say we also track the country in which the order was placed.
62
00:03:32,959 --> 00:03:36,320
So the order class also has a country attribute.
63
00:03:36,320 --> 00:03:40,640
And we're interested in dividing the domestic and international orders into two separate
64
00:03:40,640 --> 00:03:41,600
arrays.
65
00:03:41,600 --> 00:03:45,239
The very clever partition method makes this really easy.
66
00:03:45,239 --> 00:03:50,440
The orders for which the block returns true, in this case, the country is USA, end up in
67
00:03:50,440 --> 00:03:52,519
the first array domestic.
68
00:03:52,519 --> 00:03:57,280
The orders for which the block returns false, any country not USA, end up in the second
69
00:03:57,280 --> 00:03:59,720
array, international.
70
00:03:59,720 --> 00:04:01,680
So it just got to know from marketing.
71
00:04:01,680 --> 00:04:06,160
And it says here that they want to get all the emails from our orders so they can email
72
00:04:06,160 --> 00:04:07,959
our customers and send them a newsletter.
73
00:04:07,960 --> 00:04:13,600
So we basically need to transform all of our orders that are in an array into an array
74
00:04:13,600 --> 00:04:14,600
of emails.
75
00:04:14,600 --> 00:04:16,320
So let's see how to do that.
76
00:04:16,320 --> 00:04:17,600
So let's start with a label here.
77
00:04:17,600 --> 00:04:21,079
These are going to be our newsletter emails.
78
00:04:21,079 --> 00:04:23,560
And we want to create a new array that contains the email.
79
00:04:23,560 --> 00:04:26,880
So I'm going to have an empty array called emails right there.
80
00:04:26,880 --> 00:04:30,640
And then what we need to do is go through all the orders in the orders array and accumulate
81
00:04:30,640 --> 00:04:33,760
those emails into the email array.
82
00:04:33,760 --> 00:04:36,720
So we already know one way to do that would be to use our each method.
83
00:04:36,720 --> 00:04:38,280
We're going to take our orders array.
84
00:04:38,280 --> 00:04:40,360
We can call each, give it a block.
85
00:04:40,360 --> 00:04:42,400
That's going to give us an order.
86
00:04:42,400 --> 00:04:44,680
Then we can append to that emails array.
87
00:04:44,680 --> 00:04:49,440
I'm going to do this all on one line by taking the orders email just like that.
88
00:04:49,440 --> 00:04:51,440
Oh, and we should downcase them.
89
00:04:51,440 --> 00:04:52,440
Sure.
90
00:04:52,440 --> 00:04:57,240
We can convert those to lowercase using the downcase method because email is just a string.
91
00:04:57,240 --> 00:04:58,400
So that takes all the orders.
92
00:04:58,400 --> 00:05:01,600
Converse them into emails and puts them in the emails array.
93
00:05:01,600 --> 00:05:03,360
And then we can just print out that array.
94
00:05:03,360 --> 00:05:07,120
And again, I'm going to use p which will give us the internal representation of that array
95
00:05:07,120 --> 00:05:09,560
using p emails.
96
00:05:09,560 --> 00:05:14,320
Print that out and we've got our array of emails printed out nicely like that.
97
00:05:14,320 --> 00:05:18,680
Now this works, but there's a much better way to do this using the map method.
98
00:05:18,680 --> 00:05:24,360
We can actually get rid of this temporary variable that has our empty array called emails.
99
00:05:24,360 --> 00:05:27,400
And we can use the map method instead of each.
100
00:05:27,400 --> 00:05:31,800
The map method is going to take everything that's in the orders array in this case.
101
00:05:31,800 --> 00:05:35,440
And then we're going to give it a block until it we want to new array containing only
102
00:05:35,440 --> 00:05:38,560
these elements or these attributes of an array.
103
00:05:38,560 --> 00:05:44,440
So rather than appending them to an array, we can just say take the orders and convert them
104
00:05:44,440 --> 00:05:47,160
into their downcase emails.
105
00:05:47,160 --> 00:05:52,080
Now the map method returns a new array that array is just going to contain our emails,
106
00:05:52,080 --> 00:05:55,320
which are going to be strings, which we can print out that way.
107
00:05:55,320 --> 00:05:58,040
So if we run this, we get exactly the same thing.
108
00:05:58,040 --> 00:06:01,960
We've got our array of custom emails.
109
00:06:01,960 --> 00:06:08,000
So what happened was the map method here called the block for each element in the array.
110
00:06:08,000 --> 00:06:12,200
And then it placed whatever the block returned in the new array.
111
00:06:12,200 --> 00:06:14,320
In this case, the block is returning.
112
00:06:14,320 --> 00:06:15,880
This is the only expression in the block.
113
00:06:15,880 --> 00:06:20,360
So it's implicitly returning the value or the result of that expression, which will be the
114
00:06:20,360 --> 00:06:22,240
downcase email.
115
00:06:22,240 --> 00:06:28,760
So what we get back is an array containing all the values returned from calling the block.
116
00:06:28,760 --> 00:06:30,840
And then we just print them out.
117
00:06:30,840 --> 00:06:32,120
Now there's a gotcha with this.
118
00:06:32,120 --> 00:06:36,280
And it's the same gotcha we saw a little bit earlier, which is if inside of this block
119
00:06:36,280 --> 00:06:40,640
you use puttest to actually print out the email inside of the block.
120
00:06:40,640 --> 00:06:45,640
Well remember, puttest is going to print out this email to the screen, but then puttest
121
00:06:45,640 --> 00:06:47,240
itself returns nil.
122
00:06:47,240 --> 00:06:49,560
So what happens if we do this?
123
00:06:49,560 --> 00:06:56,020
Well, we get back in a array of nil because puttest returns nil map just takes whatever's
124
00:06:56,020 --> 00:07:00,360
returned by the block nil in this case and puts it in the new array emails.
125
00:07:00,360 --> 00:07:03,040
So that's just something to watch out for.
126
00:07:03,040 --> 00:07:07,320
One important thing to note here is that our original orders array hasn't changed.
127
00:07:07,320 --> 00:07:10,120
Map hasn't changed it and we can print out our orders.
128
00:07:10,120 --> 00:07:11,720
I'll just use p to do that.
129
00:07:11,720 --> 00:07:13,280
Our original orders array.
130
00:07:13,280 --> 00:07:17,320
And we see that it still has all of our orders in there.
131
00:07:17,320 --> 00:07:18,320
So let's recap.
132
00:07:18,320 --> 00:07:21,040
We have an array of four order objects.
133
00:07:21,040 --> 00:07:26,080
The map method calls the block for each order and places the blocks return value in a new
134
00:07:26,080 --> 00:07:27,080
array.
135
00:07:27,080 --> 00:07:32,600
So when mows order is passed to the block, the return value of the block is mows email
136
00:07:32,600 --> 00:07:35,039
downcase or a string.
137
00:07:35,039 --> 00:07:39,280
That string is then mapped into the first position of the new array.
138
00:07:39,280 --> 00:07:44,120
The map method iterates through each successive order mapping the orders downcase email
139
00:07:44,120 --> 00:07:47,640
to the corresponding position of the new array.
140
00:07:47,640 --> 00:07:53,120
As we're calling map on an array of four orders, it returns an array of four emails.
141
00:07:53,120 --> 00:07:57,719
The return to rate is always the same size as the original array.
142
00:07:57,719 --> 00:08:00,960
So the folks from the marketing department talk to people from the finance department and
143
00:08:00,960 --> 00:08:02,880
figure out what we can do.
144
00:08:02,880 --> 00:08:08,200
So now the finance department wants to know the taxes on the orders in Colorado.
145
00:08:08,200 --> 00:08:14,000
Okay, so what we need to do is filter all the Colorado orders out of our orders array.
146
00:08:14,000 --> 00:08:18,240
And then we need a new array that just contains the tax for those orders.
147
00:08:18,240 --> 00:08:19,240
Right.
148
00:08:19,240 --> 00:08:20,240
Okay.
149
00:08:20,240 --> 00:08:22,840
Well, we can pull this off by stringing together a couple of the methods we've already
150
00:08:22,840 --> 00:08:24,040
used.
151
00:08:24,040 --> 00:08:29,440
So conveniently, we already have this tax method that computes the tax based on the total
152
00:08:29,440 --> 00:08:30,440
and the state of the order.
153
00:08:30,440 --> 00:08:33,679
So we don't have to worry about that part of the equation.
154
00:08:33,679 --> 00:08:38,000
Down here at the bottom then, we're just going to print out our taxes.
155
00:08:38,000 --> 00:08:40,360
So the first part of this is just to get all the Colorado orders.
156
00:08:40,360 --> 00:08:43,360
I'm going to assign that to an array called CO orders.
157
00:08:43,360 --> 00:08:44,480
We're going to use orders.
158
00:08:44,480 --> 00:08:46,600
We want to filter all the orders from Colorado.
159
00:08:46,600 --> 00:08:50,320
So we're going to use the select method to do that.
160
00:08:50,320 --> 00:08:55,360
And our criteria is the order state equals Colorado.
161
00:08:55,360 --> 00:08:56,520
All right, that's step one.
162
00:08:56,520 --> 00:09:00,440
The next step then is to get the taxes for all the orders from Colorado.
163
00:09:00,440 --> 00:09:03,880
So what we're going to do is we're going to take our CO orders array and we're going
164
00:09:03,880 --> 00:09:11,880
to map those orders to their tax.
165
00:09:11,880 --> 00:09:13,880
It's not an actual attribute.
166
00:09:13,880 --> 00:09:19,360
So you'll often want to use any methods that derive values like a tax here inside your
167
00:09:19,360 --> 00:09:20,640
blocks, this criteria.
168
00:09:20,640 --> 00:09:23,840
You don't have to just check individual attributes.
169
00:09:23,840 --> 00:09:27,920
Actually in this case, whatever the method returns, we'll get mapped into that position
170
00:09:27,920 --> 00:09:29,240
of the array.
171
00:09:29,240 --> 00:09:34,320
And then we'll just print out our CO taxes array.
172
00:09:34,320 --> 00:09:40,560
And you see that we've got an array of two values for and two that match the two Colorado
173
00:09:40,560 --> 00:09:42,360
orders that we have.
174
00:09:42,360 --> 00:09:47,240
Now we can actually refactor a bit to do all of this on one line and chain together method.
175
00:09:47,240 --> 00:09:49,200
And you'll often see Ruby code that does this.
176
00:09:49,200 --> 00:09:51,320
So it's good to sort of get used to it.
177
00:09:51,320 --> 00:09:56,240
What we can do is at the end of the select, remember select returns and array.
178
00:09:56,240 --> 00:10:01,080
So we can turn around and just call map on whatever's returned by select.
179
00:10:01,080 --> 00:10:04,400
And this is going to end up being our CO taxes.
180
00:10:04,400 --> 00:10:07,160
So we select all the orders from Colorado.
181
00:10:07,160 --> 00:10:11,960
That expression returns an array, which we then just turn around and call the map method
182
00:10:11,960 --> 00:10:12,960
on.
183
00:10:12,960 --> 00:10:17,560
The result of that's going to be an array of taxes and we assign that to CO taxes.
184
00:10:17,560 --> 00:10:19,680
And that works just the same.
185
00:10:19,680 --> 00:10:21,320
So here's our last request.
186
00:10:21,320 --> 00:10:23,160
This one is from the sales department.
187
00:10:23,160 --> 00:10:24,160
Uh oh.
188
00:10:24,160 --> 00:10:30,240
So we need to iterate through all the orders and just accumulate their total.
189
00:10:30,240 --> 00:10:31,240
Right.
190
00:10:31,240 --> 00:10:32,240
Okay.
191
00:10:32,240 --> 00:10:33,240
All right.
192
00:10:33,240 --> 00:10:34,880
We solved this before using code that looks something like this.
193
00:10:34,880 --> 00:10:39,959
We just had a sum variable and then we used each to iterate through the orders and
194
00:10:39,959 --> 00:10:44,560
use the plus equal operator to accumulate the order total into sum and then we just printed
195
00:10:44,560 --> 00:10:46,680
out total sales that way.
196
00:10:46,680 --> 00:10:49,959
But as you might imagine, there's an easier way to do this.
197
00:10:49,959 --> 00:10:52,199
And that's by using the reduced method.
198
00:10:52,200 --> 00:10:53,920
The reduced can be a little bit tricky.
199
00:10:53,920 --> 00:10:57,680
So let's jump over to a scratch pad and play around with some basic numbers.
200
00:10:57,680 --> 00:11:02,240
So here's on array of numbers and let's say that we just want to sum up all of these numbers.
201
00:11:02,240 --> 00:11:06,920
The way we do that is by using the reduced method on that array.
202
00:11:06,920 --> 00:11:09,240
The reduced method takes a method parameter.
203
00:11:09,240 --> 00:11:13,280
In this case, it's going to be the initial value, which we're going to set as 0.
204
00:11:13,280 --> 00:11:14,920
And then it takes a block.
205
00:11:14,920 --> 00:11:18,360
Now this method gives two block parameters.
206
00:11:18,360 --> 00:11:23,640
The first is going to be an accumulator value or a variable in which we want to accumulate
207
00:11:23,640 --> 00:11:25,680
these numbers as we total them up.
208
00:11:25,680 --> 00:11:28,720
And the second block parameter is going to be the actual number.
209
00:11:28,720 --> 00:11:30,160
This is going to be the elements in the array.
210
00:11:30,160 --> 00:11:34,040
It's going to give us each of those number elements.
211
00:11:34,040 --> 00:11:38,880
Then the block itself is going to take the sum and it's going to add the number.
212
00:11:38,880 --> 00:11:41,360
Now notice we're not using plus equals here.
213
00:11:41,360 --> 00:11:43,520
We're just saying sum plus number.
214
00:11:43,520 --> 00:11:47,560
And the reduced method will take the result of this expression.
215
00:11:47,560 --> 00:11:51,479
And each time through the iteration, we'll assign it back to this sum.
216
00:11:51,479 --> 00:11:55,719
So you're just going to go through each of those numbers, running the block and taking
217
00:11:55,719 --> 00:11:59,040
whatever the block returns and adding it to sum.
218
00:11:59,040 --> 00:12:01,239
So it'll take care of that for us.
219
00:12:01,239 --> 00:12:06,479
Now the return value of reduce is the value returned by the block the last time it was
220
00:12:06,479 --> 00:12:07,479
called.
221
00:12:07,479 --> 00:12:09,400
So we need to assign it to a variable or print it out.
222
00:12:09,400 --> 00:12:14,199
I'll just assign it to a variable called result here and we'll just print out result.
223
00:12:14,199 --> 00:12:15,199
Right?
224
00:12:15,199 --> 00:12:16,199
What do we get back when we run it?
225
00:12:16,200 --> 00:12:17,200
Well, it's 10.
226
00:12:17,200 --> 00:12:19,280
It's the sum of all those numbers.
227
00:12:19,280 --> 00:12:25,480
So you can think of reduce as reducing a collection down to a single result by running
228
00:12:25,480 --> 00:12:29,880
the code in the block for successive elements in the array.
229
00:12:29,880 --> 00:12:34,320
Now you may see reduce called without a method parameter.
230
00:12:34,320 --> 00:12:37,960
It's an optional parameter so you don't have to pass into parameter there.
231
00:12:37,960 --> 00:12:42,760
If you don't pass into parameter, then the initial value will be the first element in the
232
00:12:42,760 --> 00:12:43,760
array.
233
00:12:43,760 --> 00:12:45,000
In this case, it's going to be one.
234
00:12:45,000 --> 00:12:49,440
Now for the first iteration of this array, all it did was take zero, which was our initial
235
00:12:49,440 --> 00:12:51,320
value and added one.
236
00:12:51,320 --> 00:12:53,960
So this is going to do the exact same thing for us.
237
00:12:53,960 --> 00:12:55,320
We run that.
238
00:12:55,320 --> 00:12:56,880
Sure enough we get 10.
239
00:12:56,880 --> 00:12:59,640
You may also see a shortcut used with reduce.
240
00:12:59,640 --> 00:13:02,000
There are a couple different shortcuts you can use.
241
00:13:02,000 --> 00:13:03,000
Right?
242
00:13:03,000 --> 00:13:07,080
One of them is you can just drop the block completely and as a method parameter, you can pass
243
00:13:07,080 --> 00:13:12,360
in the name of a method or an operator to apply to each element.
244
00:13:12,360 --> 00:13:13,920
In this case, we want to add them up.
245
00:13:13,920 --> 00:13:17,479
So we use a symbol and then we say we want to use the plus operator.
246
00:13:17,479 --> 00:13:21,400
That's just going to run the plus operator against all the elements in the array.
247
00:13:21,400 --> 00:13:24,599
Numbers in this case and numbers in Ruby support plus.
248
00:13:24,599 --> 00:13:27,240
So we get the same value if we do that.
249
00:13:27,240 --> 00:13:31,520
And if you want to set an initial value, say for example zero, you can say zero comma
250
00:13:31,520 --> 00:13:33,920
and then that plus operator.
251
00:13:33,920 --> 00:13:37,319
Or if you wanted to multiply all these things, you could actually pass in, for example,
252
00:13:37,319 --> 00:13:40,280
the multiply operator, which gives us 24.
253
00:13:40,280 --> 00:13:44,760
But that only works for objects that support things like times and plus.
254
00:13:44,760 --> 00:13:48,920
In this case, we have numbers in our array so it works out just fine.
255
00:13:48,920 --> 00:13:53,560
So that's another shortcut you may see used with the reduce method.
256
00:13:53,560 --> 00:13:57,079
So we can apply this to summing up all our order totals.
257
00:13:57,079 --> 00:13:58,079
Sure.
258
00:13:58,079 --> 00:14:01,959
Instead of doing it the long way here, take our orders and set of calling each will
259
00:14:01,959 --> 00:14:03,680
call the reduce method.
260
00:14:03,680 --> 00:14:08,160
We need to pass in an initial value because remember our orders array contains order
261
00:14:08,160 --> 00:14:09,160
objects.
262
00:14:09,160 --> 00:14:13,920
So if we don't pass in an initial value, it's going to try to use the first order in
263
00:14:13,920 --> 00:14:16,719
that array and it doesn't know to use the order total.
264
00:14:16,719 --> 00:14:19,480
So we're just going to tell it start with zero.
265
00:14:19,480 --> 00:14:25,240
Then inside of the block, instead of using plus equals, we just use sum plus the order
266
00:14:25,240 --> 00:14:26,240
total.
267
00:14:26,240 --> 00:14:29,760
Remember, it's going to take that order total and it's going to want to assign it back
268
00:14:29,760 --> 00:14:30,760
to something.
269
00:14:30,760 --> 00:14:35,040
That something in a science it back to is the first block parameter.
270
00:14:35,040 --> 00:14:39,839
So we've got two block parameters, sum which is in a accumulator and order which is the
271
00:14:39,839 --> 00:14:41,599
elements in the array.
272
00:14:41,599 --> 00:14:46,280
Remember that reduce returns of value so we need to assign this to a value.
273
00:14:46,280 --> 00:14:52,760
This is going to be our sum and we print out the sum below total sales of a thousand.
274
00:14:52,760 --> 00:14:56,959
Well since we have a tax method, we could easily sum up all the tax amounts.
275
00:14:56,959 --> 00:14:57,959
Sure.
276
00:14:57,959 --> 00:14:58,959
It's a good practice here.
277
00:14:58,959 --> 00:15:00,280
Our total tax is what we're looking for.
278
00:15:00,280 --> 00:15:01,959
We're going to take our orders array.
279
00:15:01,960 --> 00:15:07,000
We're going to reduce it down to block parameters remember, sum and order, although you can
280
00:15:07,000 --> 00:15:09,520
call this whatever you want.
281
00:15:09,520 --> 00:15:16,600
We could call it total and then inside of here we use total plus order dot tax and then
282
00:15:16,600 --> 00:15:22,960
we just print out total tax.
283
00:15:22,960 --> 00:15:24,720
We run that.
284
00:15:24,720 --> 00:15:27,320
Our total tax is $22.
285
00:15:27,320 --> 00:15:31,520
So once you start learning these innumerable modules, you often find multiple ways to do
286
00:15:31,520 --> 00:15:32,520
the same thing.
287
00:15:32,520 --> 00:15:35,520
Just as an example of that, there's a different way we could do this.
288
00:15:35,520 --> 00:15:38,400
We're going to comment out the first one just so you can see it.
289
00:15:38,400 --> 00:15:44,000
Instead of trying to reduce orders down to tax totals, we could do this another way.
290
00:15:44,000 --> 00:15:45,840
We could take our orders.
291
00:15:45,840 --> 00:15:49,920
We could map them to their tax.
292
00:15:49,920 --> 00:15:53,920
That's going to give us an array of taxes.
293
00:15:53,920 --> 00:15:58,439
A array of taxes is going to be an array of numbers so we could take that array of numbers
294
00:15:58,440 --> 00:16:04,240
and reduce it down using the shortcut because numbers support the plus operator.
295
00:16:04,240 --> 00:16:05,480
That should give us the same thing.
296
00:16:05,480 --> 00:16:08,000
We just did it a different way.
297
00:16:08,000 --> 00:16:11,400
It's sure enough our tax total is still $22.
298
00:16:11,400 --> 00:16:13,200
So here's the last recap.
299
00:16:13,200 --> 00:16:18,560
The reduced method takes an initial value zero in this case as the method parameter.
300
00:16:18,560 --> 00:16:21,880
The associated block then takes two block parameters.
301
00:16:21,880 --> 00:16:26,240
The first parameter is the accumulator so we called it sum in this case.
302
00:16:26,240 --> 00:16:32,320
The second parameter is assigned successive elements in the array which in this case are orders.
303
00:16:32,320 --> 00:16:34,040
So how does reduce work?
304
00:16:34,040 --> 00:16:38,880
The first time through the iteration, sum is assigned the initial value of zero and order
305
00:16:38,880 --> 00:16:42,080
is assigned the first order object in the array.
306
00:16:42,080 --> 00:16:46,680
In the block, the value of sum and the orders total are then added together and return from
307
00:16:46,680 --> 00:16:47,840
the block.
308
00:16:47,840 --> 00:16:53,720
This return value 300 in this case is then assigned to sum to start the second iteration.
309
00:16:53,720 --> 00:16:57,920
When we go through the same accumulation for the second object and all the other objects
310
00:16:57,920 --> 00:17:03,800
in the array, the final value returned by the block 1000 in this case is then returned by
311
00:17:03,800 --> 00:17:07,800
the reduced method and total equals 1000.
312
00:17:07,800 --> 00:17:10,960
There's a lot you can do with these three methods and in the exercise you're going to
313
00:17:10,960 --> 00:17:14,240
get a chance to practice with them with a variety of examples.
314
00:17:14,240 --> 00:17:19,040
Again, in the next section, we'll shift gears a bit and learn how to write our own methods
315
00:17:19,040 --> 00:17:23,680
that call blocks which lets us write more expressive and compact code.
316
00:17:23,680 --> 00:17:24,680
See you then.
28588
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.