Welcome back.
The accumulator pattern is a common programming pattern where you iterate
through the contents of a list, and you accumulate a single value,
such as the sum of all of the items in that list.
It always involves an accumulator variable.
Which you'll have to initialize, usually you'll set it to be 0 or
some initial value that you're going to start with and then you'll iterate.
You'll have an iterator variable, That iterates through some sequence and
in each time, on each iteration, you'll update the accumulator variable.
When you've made it through the whole list, updating the accumulator for
each item in that list, at the end you're
going to look at the accumulator variable, and it's going to have your total value.
So the code on lines one through five is doing that.
We have a sequence on line one.
Accum is the name I've given to my accumulator variable,
naturally enough and now I'm iterating through the sequence nums.
W is going to be my iterator variable.
Accum was the name of my accumulator variable.
And then on line five, I'm going to update the accumulator.
For each item, I'm going to add that value to whatever's in the accumulator so far.
At the end, I print it out and
I should have the sum of all the values in the list.
So let's just run that, and we'll demonstrate that it really does produce
the sum of the numbers 1 through 10.
But now let me show it to you in CodeLens.
So in CodeLens, we can see what's happening at each step.
I have a sequence.
I set my accumulator value to have an initial value of zero.
Then I start iterating through the sequence.
W is bound to 1, the first element of the list.
And now I do an update, accum = accum + w.
So it used to the value 0 and now it's going to have 0 plus 1.
And we're on to the next item in the list.
Now, w is going to be two.
And the accumulator is going to get updated this time to be one plus two.
So it's sort of keeping a running sum for us.
Now it's three.
The next time we'll add in three, so it'll get to be six.
We'll add in 4, and so it'll get to be 10.
And we keep going and going and going till we get through all the items in the list.
Eventually the accumulator has the running sum of all of the items in the list.
And we can print it out.
So I'm going to hide Codelens now and give you a little challenge to predict what
would we get if instead of having, on line five the print where it is,
what if I indented it so that it's part of the for loop?
Now before I run this, why don't you try and
make a prediction of what you think it'll do.
Here we go.
So the difference is that now the print is happening inside the for loop.
That means on each iteration we're going to print the value of the accumulator,
the running sum.
So after the first iteration it had the value one, just zero plus one.
Then we added in two and we had three, and
we added in three and we got six, and so on.
So that's the basics of the accumulator pattern.
You'll be seeing that a lot in this course.
You start with an accumulator value that gets initialized to some value like zero.
You iterate through a sequence, and
your iterator variable is used on each iteration to update the accumulator.
You update the accumulator each time and at the end,
after you've gone through all of the items in the sequence, the accumulator value.
After you've gone through all the items in the sequence,
the accumulated variable will have the accumulated value from the whole list.
In this case, the sum of all of the items in the list.
We’ll see you next time.