For Programmers: Free Programming Magazines  


Home > Archive > Tcl > April 2005 > 8.5 and lassign









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author 8.5 and lassign
razajac@gmail.com

2005-04-22, 3:58 am

Dear Core Folks,

I'll like to put in my two bits re lassign.

In a bit, I'll insert my hand-rolled lassign; the one I use for my
work. But now I'll list the key behaviors that I know and love:

o The return code is the number of actual assignments made.

o null assignments are made to excess varNames. I might fix
this to *not* do these assignments.

o Note the '-gatherlast' flag: I like this *a lot*.
It puts all excess list elements at the tail end of the
list into the last varName, as a list. It comes in very
handy.

Of course, if you don't implement these things, I'll just have to
decide when to use the core lassign and when to override it with my
own, considering realtime issues and suchlike. But thanks for tuning
in for a bit.

- Ron A. Zajac, Dongshih Taiwan

Now: The code:

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # #
#
# Distributively assign "List" element values to subsequent varNames
("args").
# Return the number of values actually assigned.
#
proc lassign {List args} {

# If "List" is actually the flag "-gatherlast", then
# our actual List is the zeroth element of "args".
#
if {[set Gather [string match {-gatherlast} $List]]} {

set List [lindex $args 0]
set args [lrange $args 1 end]
}

# We'll be returning the length of the shortest list
# ("List" values list length vs. "args" variable names list
length).
# This is the number of "real" assignments made.
#
set rc \
[expr \
{[set L [llength $List]] < [set V [llength $args]]
? $L
: $V}]

# If we WISH to Gather, and we HAVE anything to gather, then...
#
if {$Gather && ($L > $V)} {
#
# ...give our List a makeover so the following algorithm
# will effect this gathering.
#
set List \
[concat \
[lrange $List 0 [expr $V - 2]] \
[list [lrange $List [expr $V - 1] end]]]
}

# Loop over the varNames, assigning the List elements to each in
turn.
# Note we assign null values to tail-end varnames for which there
are
# no associated List elements. These are "non-real"
assignments!
#
foreach arg $args {

upvar $arg _lassign_ThisVar

set _lassign_ThisVar [lindex $List 0]
set List [lrange $List 1 end]
}

return $rc
}

SM Ryan

2005-04-22, 3:58 am

razajac@gmail.com wrote:
# Dear Core Folks,
#
# I'll like to put in my two bits re lassign.
#
# In a bit, I'll insert my hand-rolled lassign; the one I use for my
# work. But now I'll list the key behaviors that I know and love:
#
# o The return code is the number of actual assignments made.
#
# o null assignments are made to excess varNames. I might fix
# this to *not* do these assignments.
#
# o Note the '-gatherlast' flag: I like this *a lot*.
# It puts all excess list elements at the tail end of the
# list into the last varName, as a list. It comes in very
# handy.

My setp returns a list of remaining unused elements.

while {[llength argv]} {
set argv [setp {p v} $argv]
switch -- $p {
...
}
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who's leading this mob?
Andreas Leitgeb

2005-04-22, 8:58 am

razajac@gmail.com <razajac@gmail.com> wrote:
> Dear Core Folks,
> I'll like to put in my two bits re lassign.
> In a bit, I'll insert my hand-rolled lassign; the one I use for my
> work.

[snipped list of differences to 8.5-core-lassign]

> Of course, if you don't implement these things, I'll just have to
> decide when to use the core lassign and when to override it with my
> own, considering realtime issues and suchlike. But thanks for tuning
> in for a bit.


Actually, I prefer the current 8.5-lassigns behaviour, and I
hope it will not change.

In your place I'd just rename your version of lassign to
(e.g.) mylassign, rzlassign or something else, so then you
need not choose one for each script, but can use both at will.

PS: your version also has some subtle divantages:
> proc lassign {List args} {
> if {[set Gather [string match {-gatherlast} $List]]} {

The first argument will always be (internally) converted to
string, even if it is a (perhaps large) list. This can lead
to noticeable slowdown.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com