1 00:00:01,460 --> 00:00:02,640 Welcome back. 2 00:00:02,640 --> 00:00:07,102 The accumulator pattern is a common programming pattern where you iterate 3 00:00:07,102 --> 00:00:11,135 through the contents of a list, and you accumulate a single value, 4 00:00:11,135 --> 00:00:13,924 such as the sum of all of the items in that list. 5 00:00:16,002 --> 00:00:18,820 It always involves an accumulator variable. 6 00:00:22,919 --> 00:00:27,366 Which you'll have to initialize, usually you'll set it to be 0 or 7 00:00:27,366 --> 00:00:32,370 some initial value that you're going to start with and then you'll iterate. 8 00:00:33,490 --> 00:00:40,238 You'll have an iterator variable, That iterates through some sequence and 9 00:00:40,238 --> 00:00:44,670 in each time, on each iteration, you'll update the accumulator variable. 10 00:00:47,480 --> 00:00:51,280 When you've made it through the whole list, updating the accumulator for 11 00:00:51,280 --> 00:00:56,210 each item in that list, at the end you're 12 00:00:56,210 --> 00:00:59,850 going to look at the accumulator variable, and it's going to have your total value. 13 00:01:02,610 --> 00:01:06,050 So the code on lines one through five is doing that. 14 00:01:06,050 --> 00:01:08,640 We have a sequence on line one. 15 00:01:08,640 --> 00:01:12,990 Accum is the name I've given to my accumulator variable, 16 00:01:12,990 --> 00:01:17,640 naturally enough and now I'm iterating through the sequence nums. 17 00:01:19,481 --> 00:01:22,800 W is going to be my iterator variable. 18 00:01:25,740 --> 00:01:29,060 Accum was the name of my accumulator variable. 19 00:01:29,060 --> 00:01:34,220 And then on line five, I'm going to update the accumulator. 20 00:01:34,220 --> 00:01:40,780 For each item, I'm going to add that value to whatever's in the accumulator so far. 21 00:01:42,220 --> 00:01:45,180 At the end, I print it out and 22 00:01:45,180 --> 00:01:47,830 I should have the sum of all the values in the list. 23 00:01:48,900 --> 00:01:53,680 So let's just run that, and we'll demonstrate that it really does produce 24 00:01:53,680 --> 00:01:55,090 the sum of the numbers 1 through 10. 25 00:01:55,090 --> 00:01:59,130 But now let me show it to you in CodeLens. 26 00:02:05,092 --> 00:02:08,089 So in CodeLens, we can see what's happening at each step. 27 00:02:09,730 --> 00:02:11,630 I have a sequence. 28 00:02:11,630 --> 00:02:15,570 I set my accumulator value to have an initial value of zero. 29 00:02:15,570 --> 00:02:17,710 Then I start iterating through the sequence. 30 00:02:19,050 --> 00:02:23,460 W is bound to 1, the first element of the list. 31 00:02:23,460 --> 00:02:27,450 And now I do an update, accum = accum + w. 32 00:02:28,910 --> 00:02:35,824 So it used to the value 0 and now it's going to have 0 plus 1. 33 00:02:38,470 --> 00:02:40,737 And we're on to the next item in the list. 34 00:02:40,737 --> 00:02:42,530 Now, w is going to be two. 35 00:02:47,675 --> 00:02:51,759 And the accumulator is going to get updated this time to be one plus two. 36 00:02:53,773 --> 00:02:56,100 So it's sort of keeping a running sum for us. 37 00:02:58,759 --> 00:03:00,399 Now it's three. 38 00:03:00,399 --> 00:03:05,120 The next time we'll add in three, so it'll get to be six. 39 00:03:05,120 --> 00:03:08,840 We'll add in 4, and so it'll get to be 10. 40 00:03:08,840 --> 00:03:14,680 And we keep going and going and going till we get through all the items in the list. 41 00:03:14,680 --> 00:03:18,900 Eventually the accumulator has the running sum of all of the items in the list. 42 00:03:20,322 --> 00:03:21,510 And we can print it out. 43 00:03:23,960 --> 00:03:29,904 So I'm going to hide Codelens now and give you a little challenge to predict what 44 00:03:29,904 --> 00:03:35,208 would we get if instead of having, on line five the print where it is, 45 00:03:35,208 --> 00:03:39,430 what if I indented it so that it's part of the for loop? 46 00:03:40,440 --> 00:03:43,120 Now before I run this, why don't you try and 47 00:03:43,120 --> 00:03:45,050 make a prediction of what you think it'll do. 48 00:03:47,914 --> 00:03:48,630 Here we go. 49 00:03:52,858 --> 00:03:56,540 So the difference is that now the print is happening inside the for loop. 50 00:03:56,540 --> 00:04:00,680 That means on each iteration we're going to print the value of the accumulator, 51 00:04:00,680 --> 00:04:02,280 the running sum. 52 00:04:02,280 --> 00:04:07,184 So after the first iteration it had the value one, just zero plus one. 53 00:04:07,184 --> 00:04:10,267 Then we added in two and we had three, and 54 00:04:10,267 --> 00:04:13,540 we added in three and we got six, and so on. 55 00:04:13,540 --> 00:04:17,190 So that's the basics of the accumulator pattern. 56 00:04:17,190 --> 00:04:19,020 You'll be seeing that a lot in this course. 57 00:04:20,150 --> 00:04:25,270 You start with an accumulator value that gets initialized to some value like zero. 58 00:04:25,270 --> 00:04:29,550 You iterate through a sequence, and 59 00:04:29,550 --> 00:04:36,100 your iterator variable is used on each iteration to update the accumulator. 60 00:04:37,250 --> 00:04:40,570 You update the accumulator each time and at the end, 61 00:04:40,570 --> 00:04:43,770 after you've gone through all of the items in the sequence, the accumulator value. 62 00:04:45,410 --> 00:04:47,970 After you've gone through all the items in the sequence, 63 00:04:47,970 --> 00:04:53,010 the accumulated variable will have the accumulated value from the whole list. 64 00:04:53,010 --> 00:04:56,390 In this case, the sum of all of the items in the list. 65 00:04:56,390 --> 00:04:57,530 We’ll see you next time.