Re: Inline return values



On Tue, 1 Jul 2008, slebetman@xxxxxxxxx wrote:

On Jul 1, 7:44 pm, Kaitzschu
<kaitzs...@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
On Tue, 1 Jul 2008, dave.joub...@xxxxxxxxxxxxxx wrote:
On Jun 30, 12:35 pm, Kaitzschu
set tl [lrange [split [read $in] \n end-[incr n -1] end]

As an aside, I found myself shocked at the thought that one could use incr that way!

<snip>

There has been some tangential discussion using "inline return values" on tclcore mailing list regarding [chan pipe], but I guess this doesn't really apply since using a "mutator" command this way is most likely going to result in an error, as you point out with your scenario.

But I like it! Even if it is going to put a nail to my foot sometimes :)

Ah yes, for "casual" programming tasks I also like (ab)using return values of commands. I especially like that [set] always returns a value. My favourite (ab)use is the "slurp" one-liner:

set data [read [set f [open $filename]]][close $f]

That is still very readable and only thing that can confuse reader is if (s)he doesn't know that [close] returns an empty string (which is phased out). But once we have a -unique -sorted $list that needs to be added an $item while ${exclude}ing another and still kept -unique -sorted in case $item already was there, then we have one ugly one-liner:

[code]
set list [lreplace [set list [lsort -unique [lappend list $item]]] [set ind [lsearch -sorted $list $exclude]] $ind]
[/code]

...which mutates list once, twice, three times (plus $ind is calculated in the mist). And does different things to each cycle (which, obviously, is needed, since otherwise there is no need for mutating the value, return value could be used directly; true, list is [lappend]ed for nothing, but [linsert] is uglier).

[code]
lappend list $item
set list [lsort -unique $list]
set ind [lsearch -sorted $list $exclude]
set list [lreplace $list $ind $ind]
# lreplace will bail out when $ind isn't in range (-1 = not found)
# woohoo, 4-fold increase in LOC! plus vague documentation!
[/code]

Of course, in production code it's not a good idea to do this.

So _that_ must be the reason I'm not being considered a productive member of society :P

--
-Kaitzschu
set s TCL\ ;wh 1 {pu [set s [st ra $s 1 3][st in $s 0]]\033\[1A;af 99}

"Good thing programmers don't build bridges
[the same way as Kaitzschu writes code]."
--John Kelly in comp.lang.tcl
.



Relevant Pages

  • Inline return values
    ... incr that way! ... There has been some tangential discussion using "inline return values" on ... really apply since using a "mutator" command this way is most likely going ...
    (comp.lang.tcl)
  • Re: VBA Command Button
    ... Probably what's happening is that the command button is in line with text ... collection instead of the Shapes collection, ... change the button from Inline to one of the other settings such as Square. ... I'll suggest that you'd be much better served by using a toolbar ...
    (microsoft.public.word.vba.general)
  • RE: Using a Command Button to attach a file??
    ... and plain text messages or inline in RTF messages", ... Use a command button that acts exactly like the "Insert File" button (i.e., ... Sue Mosher, Outlook MVP ...
    (microsoft.public.outlook.program_forms)
  • Re: style standards for computer text
    ... Windows Services such as IMAP, or Unix command names, and so forth. ... monospace, fitting within a line, unhyphenated ... variable names (inline): body italic, not monospace, when used in the ... publishers insist on italics ...
    (comp.text.tex)
  • How do I turn off this warning?
    ... Inline: Boolean; ... Command: Unsigned_5; ... those warnings and I would like to say to the compiler ... I tried to look at the option list you get by calling "gnatmake" by ...
    (comp.lang.ada)