Version 0 of lmap

Updated 2005-04-02 20:09:34 by suchenwi

Richard Suchenwirth 2005-04-02 - lmap is a "collecting foreach" which returns a list of its results. In Jim it is built in, but it can be easily had in pure Tcl:

 proc lmap {_var list body} {
    upvar 1 $_var var
    set res {}
    foreach var $list {lappend res [uplevel 1 $body]}
    set res
 }

Several usage examples are at Multiplication tables. Lmap is a compromise between Tcl and the classical functional programming function map, in that it takes a "quasi-lambda" which is split up into the _var name and the body arguments. However, this style is well-known from foreach, and somehow reads better:

 lmap i {1 2 3 4 5} {expr $i*$i}

vs.

 map [lambda i {expr $i*$i}] {1 2 3 4 5}

Category Functional Programming