Accumulator factory

From Rosetta Code
Revision as of 23:25, 28 December 2009 by rosettacode>Dkf (Draft of a task on closures and stateful functions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Accumulator factory is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A problem posed by Paul Graham is that of creating a function that takes a single (numeric) argument and which returns another function that is an accumulator. The returned accumulator function in turn also takes a single numeric argument, and returns the sum of all the numeric values passed in so far to that accumulator (including the initial value passed when the accumulator was created).

The detailed rules are at http://paulgraham.com/accgensub.html and are reproduced here for simplicity.

Before you submit an example, make sure the function
  1. Takes, and returns functions that take, exactly one argument.
  2. Works for any numeric type-- i.e. can take both ints and floats and returns functions that can take both ints and floats. (It is not enough simply to convert all input to floats. An accumulator that has only seen integers must return integers.)
  3. Generates functions that return the sum of every number ever passed to them, not just the most recent.
  4. Returns a real function, meaning something that you can use wherever you could use a function you had defined in the ordinary way in the text of your program.
  5. Doesn't store the accumulated value or the returned functions in a way that could cause them to be inadvertantly modified by other code.
E.g. if after the example, you added the following code (in a made-up language):
<lang pseudocode>x = foo(1);

x(5); foo(3); print x(2.3);</lang>

It should print 8.3.

The purpose of this task is to create a function that implements the described rules. It need not handle any special error cases not described above. The simplest way to implement the task as described is typically to use a closure, providing the language supports them.

Tcl

Works with: Tcl version 8.6

<lang tcl>package require Tcl 8.6

  1. make the creation of coroutines without procedures simpler

proc coro {name arguments body args} {

   coroutine $name apply [list $arguments $body] {*}$args

}

  1. Wrap the feeding of values in and out of a generator

proc coloop {var body} {

   set val [info coroutine]
   upvar 1 $var v
   while 1 {

set v [yield $val]

       if {$v eq "stop"} break

set val [uplevel 1 $body]

   }

}

  1. The outer coroutine is the accumulator factory
  2. The inner coroutine is the particular accumulator

coro accumulator {} {

   coloop n {

coro accumulator.[incr counter] n { coloop i { set n [expr {$n + $i}] } } $n

   }

}</lang> Sample usage (extra characters over Paul's example to show more clearly what is going on): <lang tcl>% set x [accumulator 1]

accumulator.1

% $x 5 6 % accumulator 3

accumulator.2

% puts ">>[$x 2.3]<<" >>8.3<<</lang>