Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Pass result of last command in for, foreach, and while

Closed / Out of Date219376ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 20 days ago
Descriptionplain2000-10-26 05:11:50
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 "")
dkfplain2000-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_kupriesplain2000-11-23 15:30:15
With respect to "map", this is provided by "listx" (And some more :).
andreas_kupries Ticket [219376ffff]: 1 change [History diff]2000-11-23 18:38:31
dkf Ticket [219376ffff]: 2 changes [History diff]2001-02-07 23:06:26
dkf Ticket [219376ffff]: 1 change [History diff]2001-02-15 23:28:17
dkfplain2001-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...
dgp Ticket [219376ffff]: 1 change [History diff]2001-04-04 04:57:25
dkf Ticket [219376ffff]: 1 change [History diff]2001-12-18 21:58:01
serhiy.storchakawiki2026-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).

Log in (anonymous is fine) to add a comment.