An example of implementing [iterators] using [Closures]. This is just an experiment!!! -- [Todd Coram] The code below (using code from [Closures]) allows us to create a ''forward'' iterator command in pure Tcl that works with strings, lists, channels and ''ranges'' of values. This way we can write truly "generic" functions in Tcl that operate on the above without knowledge of type. Scroll to the bottom for examples. make-closure-proc iterator {_type _target} { variable type $_type variable target {$_target} variable pos -1 variable end -1 } { variable type; switch -glob -- $type { -str* { set end [string length $target] set nextbody { incr pos if {$pos >= $end} { return {} } return [string index $target $pos] } } -li* { set end [llength $target] set nextbody { incr pos if {$pos >= $end} { return {} } return [lindex $target $pos] } } -chan* { set end {} set nextbody { if {[eof $target]} { return {} } return [gets $target] } } -rang* { set end [lindex $target 1] set pos [expr {[lindex $target 0] - 1}] set nextbody { if {$pos >= $end} { return {} } return [incr pos] } } default { error "iterator: bad option \"$type\": must be -string, -list, -channel or -range" } } # if you use my lambda hack (from [Closures]): # lambda {} "variable target; variable pos; variable end; $nextbody" # otherwise the below 2 lines will work too.. proc next {} "variable target; variable pos; variable end; $nextbody" return [namespace current]::next } Here are a few examples: set i1 [iterator -string "hello world"] set i2 [iterator -list {hello world}] set i3 [iterator -channel [open somefile.txt r]] set i4 [iterator -range {1 100}] proc count {iter} { set count 0 while {[$iter] != {}} { incr count } return $count } count $i1 ;# returns 11 count $i2 ;# returns 2 count $i3 ;# returns the number of characters in the file. count $i4 ;# returns 100 We can now define a couple of ''useful'' generic functions: proc iforeach {var iter body} { while {[set $var [$iter]] != {}} { eval $body } } proc map {iter cmd} { set res {} while {[set v [$iter]] != {}} { lappend res [eval $cmd $v] } return $res } Which can be used as follows: iforeach x [iterator -string "hi mom!"] { puts "Got $x" } iforeach x [iterator -range {12 24}] { puts "$x" } proc double {x} {expr {$x * 2}} map [iterator -range {0 10}] double The map proc looks better using lambda: map [iterator -range {0 10}] [lambda x {expr {$x * 2}}] --------------