| antirez 2005-02-23, 8:59 am |
| antirez wrote:
> proc createAdder {x} {
> lamba {y} {expr $y+[closure get $x]} [list x $x]
> }
I forgot to include a couple of points.
The closure command can also access closures of external procedures:
[closure in $procName get/set x ...] and so on.
This makes possible to share the environment if really needed.
Example:
proc createAdder {x} {
set a [lamba {y} {expr $y+[closure get $x]} [list x $x]]
set b [lamba {incr} {closure in $a set x $incr} [list a $a]]
list $a $b
}
this returns one closure to add and one to change the increment
performed by the first closure.
Also it is worth to note that this way to do closures don't play
well with functions as values nor with lambas based on auto expansion,
because full closures need to be able to modify their environment
so they can not be immutable values.
Ciao,
Salvatore
|