Version 20 of Accumulator Generator

Updated 2009-12-28 07:30:12 by slebetman

There was an entry about this Topic (http://wiki.tcl.tk/3520 )


The Accumulator Generator is a problem used by Paul Graham to compare the relative power of different programming languages.

The Problem

We want to write a function that generates accumulators-- a function that takes a number n, and returns a function that takes another number i and returns n incremented by i.

(That's incremented by, not plus. An accumulator has to accumulate.)

The original essay: http://www.paulgraham.com/icad.html

The implementation in other languages: http://www.paulgraham.com/accgen.html

My implementation:

package require TclOO
namespace import oo::*

proc foo n {
        set obj [object new]        
        objdefine $obj method init {_n} {my variable n; set n $_n}
        objdefine $obj method acc i {my variable n; set n [expr {$n + $i}]}
        $obj init $n
        list ::apply [list i [subst -novariables {[set obj] acc $i}]]
}
proc destroy_acc acc {
        [lindex $acc 1 1 0] destroy
}

A test:

> set acc [foo 3]
> {*}$acc 1
4
> {*}$acc 10
14
> {*}$acc 0
14

> destroy_acc $acc
> {*}$acc 1
invalid command name "::oo::Obj

Comments

I'm not happy with my implementation, It works but It is still too vebose compared with other languages.

I'm not sure if garbage collection is obligatory to solve this problem.

Feel free to insert here your own better/clever approach.


WHD: How about this?

proc mkag {name n} {
    set ::${name} $n

    proc ::${name} {i} [list incr ::${name} \$i]
}

pmarin: Sorry, I don't understand your code, Can you make a similar test like the above? Note that the accumulator must work with integer and floats.

AMG: When WHD's mkag procedure is invoked, it (1) sets a global variable and (2) creates a procedure which increments that same global variable and returns its updated value. The global variable and the created procedure are given the same name, determined by the first argument to mkag. The second argument determines the increment step. Because the procedure created by mkag uses incr, it only works with integers, not floats. To make it work with either, create an alternative to incr:

proc incr {varname {increment 1}} {
    upvar 1 $varname var
    set var [expr {$var + $increment}]
}

DKF: With 8.6, you can do this using coroutines.

proc accumulator {name {n 0}} {
    coroutine $name apply {n {
        set i [yield [info coroutine]]
        while 1 {
            catch {incr n $i}
            set i [yield $n]
        }
    }} $n
}

Testing...

> set acc [accumulator foo 3]
::foo
> $acc 1
4
> $acc 10
14
> $acc 0
14

The only real issue with this code is that it doesn't give good error messages when misused.


slebetman: Here's one that is portable up to when [info body] was introduced: way before Tcl 7.0 I suspect. Using self-modifying code (mechanics and implementation is ugly but the fact that it's doable is beautiful):

  proc accumulator {name n} {
    proc $name {i} "proc $name {i} \[info body $name\]+\$i;expr $n+\$i"
  }

Usage...

> accumulator acc 3
> acc 1
4
> acc 10
14
> acc 0
14