
Decorators in Tcl - blacksqr
http://wiki.tcl.tk/55304
======
jepler
A great start. One weakness is that decorators are not composable in this
sketch: you can't have `@memoize @auto_index proc foo`. I don't see an easy
way to remedy this within the framework presented here.

In my personal Tcl style I'd avoid creating a proc body as the result of
substitution. Instead, factor out the implementation so that the definition of
@memoize can be:

    
    
        proc @memoize {p pname pargs lambda} {
            if {$p ne "proc"} { error "bad synax: $p != 'proc'"}
            proc $pname $pargs [list memoize_impl $pargs $lambda]
        }
    

While it's possible the author's is better for performance or some other
consideration (I didn't benchmark), I find that the style shown on the wiki
can lead to string quoting hell.

~~~
jepler
.. it's not intended to be a puzzle, so let me also show what memoize_impl
ends up looking like:

    
    
        proc memoize_impl {pargs lambda} {
            set cmd [info level 1]
            if {[info exists ::MEM($cmd)]} { return $::MEM($cmd) }
            set ::MEM($cmd) [apply [list $pargs $lambda] {*}[lrange $cmd 1 end]]
        }

