Perhaps I'm just having a bout of Perl envy, but I keep wanting to implement an ''lmap'' and ''lgrep'' set of procs that do map and grep on lists. Sample usage: % set l [list a b c d] a b c d % lmap l { format %s%s $_ $_ } aa bb cc dd % lgrep l { string match *d* $_ } d These are easy enough to implement in Tcl: proc lmap {listName expr} { upvar $listName list set res [list] foreach _ $list { lappend res [eval $expr] } return $res } proc lgrep {listName expr} { upvar $listName list set res [list] foreach _ $list { if [eval $expr] { lappend res $_ } } return $res } Does anyone else wish these were part of standard Tcl? Would someone want to write up the TIP to try and get this in? -- [Dossy] 13aug2003 ---- [RS] What you want (and have implemented) are the classic ''map'' ([function mapping]) and ''filter'' functions. The term ''lgrep'' seems not so good to me, as ''grep'' is etymologized as "general regular expression parser", and ''filter'' allows much more than that. However, use of an implicit variable _ is no good Tcl style. I'm afraid one would have to call these with named or anonymous functions ([Lambda in Tcl]): lmap2 [lambda x {format %s%s $x $x}] $list ;# pass values, not names! lfilter [lambda x {string match *d* $x}] $list And I think, as easily these are implemented, there's no need to TIP a core change... [List comprehension] has a fancy, and lambda-less, wrapper for filters: % all i in {1 0 3 -2 4 -4} where {$i>1} 3 4 ---- [Dossy] - The reason I called it lgrep is because it's counterpart in Perl is grep -- yes, perhaps a misnomer. Also, $_ is the magic var in Perl that indicates the current element -- not good Tcl style, but I was trying to keep the similarities in place to illustrate my idea for folks who already have Perl background. Also, after having done Vignette development for a long time, I've gotten really spoiled by the FOREACH proc. Perhaps we could have a similar counterpart, say, lforeach, in the Tcl core: proc lforeach {argList list expr} { set res [list] foreach $argList $list { lappend res [eval $expr] } return $res } Used something like so: % join [lforeach x [list 1 2 3 4] { expr $x * $x }] , 1,4,9,16 Hmm, looks a lot like lmap, except it takes multiple args ... ---- [RS] Yes, and different arg order. Also, [Tcllib] since 1.4 has a '''::struct::list map''' command, so standards are just being set. ---- [[ [Category Suggestions] ]]