View Single Post
Old Aug 15th, 2006, 1:42 PM   #8
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
To which I add:
def make_acc(start=0):
    def acc(inc):
        acc.curr += inc
        return acc.curr
    acc.curr = start
    return acc
... and now the circle is complete <evil grin>

As to where you'd use an accumulator generator - you could use it with a language where functional programming is more pleasant than OO, and you want to store state for (eg) an accounting program - ie you need to store many totals. You could use an accumulator generator for each. Even with an OO program using one probably decreases the chances of bugs - "=" rather than "+=", for instance. You could probably also use it usefully for something domain-specific-ish in (eg) Ruby since you don't need brackets to call it.

-T. *handwave* "These are not the droids you are looking for" *handwave*
hydroxide is offline   Reply With Quote