Tcl Source Code

View Ticket
Login
Ticket UUID: 219376
Title: Pass result of last command in for, foreach, and while
Type: RFE Created on: 2000-10-26 05:11:50
Submitter: nobody Assigned to: dkf
Subsystem: 16. Commands A-H Severity:
Priority: 2 Last modified: 2026-11-13 11:18:44
Status: Closed Closed by: serhiy.storchaka
Resolution: Out of Date Closed on: 2026-11-13 11:18:43
Version: obsolete: 8.4a3
Description:
OriginalBugID: 6006 RFE
Version: 8.4a1
SubmitDate: '2000-07-12'
LastModified: '2000-10-25'
Severity: LOW
Status: UnAssn
Submitter: techsupp
OS: All
FixedDate: '2000-10-25'
ClosedDate: '2000-10-25'


Name:
Philip Moore

Comments:
Then here's some TCL code to try to explain (if you don't already
    understand what I'm saying):

    It would be extremely handy if I could do this:

    if {[foreach a "a b c d" { do whatever stuff on $a and $arg here }] ==
    "c"} { }

    That would save me from having to do all this:

    if {"[mycommand $arg]" == "c"} { }

    proc mycommand {arg} {
      foreach a "a b c d" { do whatever stuff on $a and $arg here } 
      return $result 
    }

    Also, if for, foreach, and while returned the results of the last thing
    inside them you could have a proc like this:

    proc someproc {arg} {
       for {set x 0} {$x<$arg} {incr x} {
         lappend out "x is $x"
       }
    }

    Instead of having this:

    proc someproc {arg} {
       for {set x 0} {$x<$arg} {incr x} {
         lappend out "x is $x"
       }
       set out
    }

    I know my examples were kinda poor.. But they should have gotten my
    point across.. 
    And before somebody says this is a bad idea.. I have to say this..
    proc's themselves already return the results of the last command, so why
    can't for, foreach, and while?

    -Philip Moore 
    [email protected]

    p.s.
    Please CC any replies to [email protected] as I don't have access to
    the newsgroup.

DesiredBehavior:
The commands for, foreach, and while don't pass the results of the last command inside them.. It'd be nice, I think, if they did.. (Currently, they always return "")
User Comments:
dkf added on 2000-11-22 23:45:58:
Speaking as an old functional programmer, it would be very nice if the [if] command returned the result of the last command of whatever body it ends up executing.
I'm less sure about returning values from [for], [foreach] and [while] since the cost of constructing the natural results of those commands (which is the list of the results of the last command of their bodies from each execution) is likely to be very expensive to compute in terms of memory.  Returning the value from the final execution of a loop body doesn't seem very intuitive to me...
Not that any of this should be taken as being against defining a separate command with the semantics described, since that would be an "opt-in" as opposed to "forced-upon"; i.e. people would use it where it makes sense according to the algorithm being implemented, and not otherwise.  (For example, a collecting [foreach] command is more commonly known as [map] in the functional-programming community.)

andreas_kupries added on 2000-11-23 15:30:15:
With respect to "map", this is provided by "listx" (And some more :).

dkf added on 2001-02-28 17:52:38:
Logged In: YES 
user_id=79902

Hmm. [if] does return the result of the last command it
executes in its bodies.  I wonder how I missed that...

serhiy.storchaka added on 2026-11-13 11:18:44:

The loop commands still return an empty result, but the collecting loop dkf suggested was added in Tcl 8.6 as lmap and dict map (TIP 405).