Code: Select all
proc K {x y} {set x}
set theList [lreplace [K $theList [set theList {}]] 7 42]
when you use the above method with so-called K combinator (an important notion in functional programming), list manipulation is done in-place, meaning duplication is eliminated (the K combinator in this case is functioning as get-and-unset proc)
on my machine, it's almost 50 times faster! operating on a big list of course:
Code: Select all
[demond@whitepine demond]$ tclsh8.4
% proc K {a b} {set a}
% for {set i 0} {$i<100000} {incr i} {
lappend a $i; lappend b $i
}
% time {set a [lreplace $a 40 80]}
24879 microseconds per iteration
% time {set b [lreplace [K $b [set b {}]] 40 80]}
537 microseconds per iteration