Version 23 of lrepeat

Updated 2004-12-12 12:50:27

lrepeat was defined in TIP 136 [L1 ] , which has been approved for incorporation into Tcl 8.5.0 .

Eventually, the doc will be here: http://www.tcl.tk/man/tcl8.5/TclCmd/lrepeat.htm

 lrepeat number element1 ?element2 element3 ...?

This command builds up a list by repeating elements. It will be a part of Tcl 8.5.0 . Note that Tcl 8.5 is available in an early developer form from ftp://ftp.tcl.tk/pub/tcl/nightly-cvs/tcl-*gz . Early access to Tk 8.5 is there as well.

An example of how it will work is

 lrepeat 3 a => a a a
 lrepeat 3 [lrepeat 3 0] => {0 0 0} {0 0 0} {0 0 0}

RS: Note the possible confusion with string repeat's syntax:

 % string repeat a 5
 aaaaa

FW: A forward-compatible version for versions before 8.5 is available in Additional list functions


LES How is [lrepeat 3 a] different from [string repeat "a " 3]?

And how is lrepeat 3 [lrepeat 3 0] different from string repeat [list [string repeat "0 " 3]]] 3?

RS The first creates a list, while the second creates a string that can be parsed into a list. For short lists, this doesn't matter much, but I noticed extra time costs of about half a second when converting a 2000x2500 list-like string to a nested list.

LES Half a second? In the entire operation or each nested list? Mentioning once again that I am completely ignorant of C, I wonder if it wouldn't have been possible to rewrite string repeat and optimize it, using whatever method lrepeat uses to do its trick. This lrepeat command still strikes me as something a bit superfluous.

LES Does anyone realize that one requires number - string while the other requires string - number, and that is an excellent method to prevent people from memorizing correct syntax? - RS pointed this out just 11 lines above :^) LES I thought you were just pointing out that one adds spaces in-between (list) and the other concatenates it all together (string).


Category Command