Version 20 of lrepeat

Updated 2004-12-12 10:36:29

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 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?


Category Command