**list ensemble** [bll] 2018-7-24: This was an exercise in learning about [namespace ensemble]. However namespace ensemble cannot have an empty sub-command, so a plain [list] would never work. This is an implementation (one possible anyways) of the list ensemble command. All of the usual [list] commands are still present. List creation is backwards compatible. It seems to work, but has not been tested extensively. Fifth try... ====== #!/usr/bin/tclsh # # Originally written by Brad Lanam # In the public domain # rename ::list ::_list # create append assign index insert length map range # repeat replace reverse search set size sort proc list { args } { if { [::llength $args] == 0 } { set sub create set largs [::_list] } else { set sub [lindex $args 0] set largs [::lrange $args 1 end] } switch -exact -- $sub { create { set rv [::_list {*}$largs] } append { set rv [uplevel 1 ::lappend $largs] } assign { set rv [uplevel 1 ::lassign $largs] } index { set rv [uplevel 1 ::lindex $largs] } insert { set rv [uplevel 1 ::linsert $largs] } size - length { set rv [uplevel 1 ::llength $largs] } map { set rv [uplevel 1 ::lmap $largs] } range { set rv [uplevel 1 ::lrange $largs] } repeat { set rv [uplevel 1 ::lrepeat $largs] } replace { set rv [uplevel 1 ::lreplace $largs] } reverse { set rv [uplevel 1 ::lreverse $largs] } search { set rv [uplevel 1 ::lsearch $largs] } set { set rv [uplevel 1 ::lset $largs] } sort { set rv [uplevel 1 ::lsort $largs] } default { set rv [uplevel 1 ::_list [lrange $args 0 end]] } } return $rv } package provide listensemble 0.5 # test code if { 1 } { proc orig { } { set l [::_list a b c d e f] set li [lindex $l 2] set l [linsert $l 3 C] set lr [lrange $l 2 3] set lp [lrepeat 2 x] set l [lreplace $l 4 4 $lp] set l [lreverse $l] lset l 5 c2 set ls [lsearch -inline -all -nocase -glob $l {C*} ] set l [lsort -nocase $l] lassign $l a1 a2 rest # I have no idea how to use lmap set nl [lmap a $l {set a ${a}X}] set ll [llength $l] set n [::_list] puts "$l / $li / $lr / $ls / $ll / $a1 / $a2 / $rest / $nl" } proc test { } { set l [list create a b c d e f] set l [list a b c d e f] set li [list index $l 2] set l [list insert $l 3 C] set lr [list range $l 2 3] set lp [list repeat 2 x] set l [list replace $l 4 4 $lp] set l [list reverse $l] list set l 5 c2 set ls [list search -inline -all -nocase -glob $l {C*} ] set l [list sort -nocase $l] list assign $l a1 a2 rest # I have no idea how to use lmap set nl [list map a $l {set a ${a}X}] set ll [list length $l] set n [list] puts "$l / $li / $lr / $ls / $ll / $a1 / $a2 / $rest / $nl" } orig test } ====== <>Discussion (Can't nest discussion blocks, so this is a lot of code interspersed). First pass ====== #!/usr/bin/tclsh # # Originally written by Brad Lanam # In the public domain # rename ::list ::_list namespace eval ::list { set cmdlist [::_list append assign index insert length map range] ::lappend cmdlist repeat replace reverse search set sort ::lappend cmdlist create size namespace export {*}$cmdlist namespace ensemble create \ -unknown ::list::_unknown \ -subcommands $cmdlist # I also tried -map {size length}, but that did not work out. # I suppose there must be a way to get -map to work, but I could not find it. variable _cflag false variable _csave {} proc _unknown { args } { variable _cflag variable _csave # Unfortunately, the unknown handler is very specific to # a namespace ensemble and is not a general handler. # argument index 1 is stripped out and lost. # Have to jump through some hoops here. # There's a lot of weirdness here # " If the -unknown handler returns anything else, # it is interpreted as a command prefix" # does not seem to be true. 'return create' does not work, # and in fact, 'return create' fails to pass the appended argument list, # the create routine gets called, and errors out with a bad # subcommand error. # save the needed value for the create command set _csave [::lindex $args 1] set _cflag true return [::_list ::list create] } proc create { args } { variable _cflag variable _csave if { $_cflag } { # more weirdness, cannot seem to set and use local variables, or # even global variables. # I tried: # set val [::_list $_csave {*}$args] # set _csave {} # return $val # and it did not work. # Tried: # variable _tval # set _tval [::_list $_csave {*}$args] # set _csave {} # return $_tval # and it did not work. set _cflag false return [::_list $_csave {*}$args] } return [::_list {*}$args] } # for orthogonality... proc size { args } { return [::llength {*}$args] } proc append { args } { return [uplevel 1 ::lappend {*}$args] } proc assign { args } { return [uplevel 1 ::lassign {*}$args] } proc index { args } { return [::lindex {*}$args] } proc insert { args } { return [::linsert {*}$args] } proc length { args } { return [::llength {*}$args] } proc map { args } { return [uplevel 1 ::lmap {*}$args] } proc range { args } { return [::lrange {*}$args] } proc repeat { args } { return [::lrepeat {*}$args] } proc replace { args } { return [::lreplace {*}$args] } proc reverse { args } { return [::lreverse {*}$args] } proc search { args } { return [::lsearch {*}$args] } proc set { args } { return [uplevel 1 ::lset {*}$args] } proc sort { args } { return [::lsort {*}{$args}] } } package provide listensemble 0.1 ====== [PL] 2018-07-24: You can make use of `ensemble create -map` like this: ====== namespace eval list { namespace export {[a-z]*} namespace ensemble create -map { create ::_list size ::llength append ::lappend assign ::lassign index ::lindex insert ::linsert length ::llength map ::lmap range ::lrange repeat ::lrepeat replace ::lreplace reverse ::lreverse search ::lsearch set ::lset sort ::lsort each ::foreach } } ====== [bll] 2018-7-24: Thanks for that. That simplifies things. I think I was trying to -map on to routines internal to the namespace (in different ways), and it wasn't working. This simplifies things. It appears [namespace ensemble] runs the -map'd commands in the correct frame. I tried the 'each', but that doesn't work. Perhaps the syntax to use it eludes me. [join] is also debatable, but I'm just going to leave this as the explicit list commands. Not sure how to format this wiki page at this time, perhaps after all the discussion is done, the final form can be displayed up top, and roll the rest into a discussion block. Second pass ====== #!/usr/bin/tclsh # # Originally written by Brad Lanam, Peter Lewerin # In the public domain # rename ::list ::_list namespace eval ::list { namespace export {[a-z]*} # size is just added for orthogonality purposes namespace ensemble create \ -unknown ::list::_unknown \ -subcommands create \ -map { size ::llength append ::lappend assign ::lassign index ::lindex insert ::linsert length ::llength map ::lmap range ::lrange repeat ::lrepeat replace ::lreplace reverse ::lreverse search ::lsearch set ::lset sort ::lsort } variable _cflag false variable _csave {} proc _unknown { args } { variable _cflag variable _csave # Unfortunately, the unknown handler is very specific to # a namespace ensemble and is not a general handler. # argument index 1 is stripped out and lost (though it # is available initially. # Have to jump through some hoops here. # # There's a lot of weirdness here... # " If the -unknown handler returns anything else, # it is interpreted as a command prefix" # does not seem to be true. 'return create' does not work, # and in fact, 'return create' fails to pass the appended argument list, # the create routine gets called, and errors out with a bad # subcommand error. # # I suspect this is just a documentation error, where # 'command prefix' should just read 'command'. # save the needed value for the create command set _csave [::lindex $args 1] set _cflag true return [::_list ::list create] } proc create { args } { variable _cflag variable _csave if { $_cflag } { # more weirdness, cannot seem to set and use local variables, or # even global variables. # I tried: # set val [::_list $_csave {*}$args] # set _csave {} # return $val # and it did not work. # Tried: # variable _tval # set _tval [::_list $_csave {*}$args] # set _csave {} # return $_tval # and it did not work. set _cflag false return [::_list $_csave {*}$args] } return [::_list {*}$args] } } package provide listensemble 0.2 ====== [bll] 2018-7-24: I was wrong. Executed in the wrong frame. Now that I have tested properly... Third pass ====== #!/usr/bin/tclsh # # Originally written by Brad Lanam, Peter Lewerin # In the public domain # rename ::list ::_list namespace eval ::list { namespace export {[a-z]*} # size is just added for orthogonality purposes namespace ensemble create \ -unknown ::list::_unknown \ -subcommands {create append assign map set} \ -map { size ::llength index ::lindex insert ::linsert length ::llength range ::lrange repeat ::lrepeat replace ::lreplace reverse ::lreverse search ::lsearch sort ::lsort } variable _cflag false variable _csave {} proc _unknown { args } { variable _cflag variable _csave # Unfortunately, the unknown handler is very specific to # a namespace ensemble and is not a general handler. # argument index 1 is stripped out and lost (though it # is available initially. # Have to jump through some hoops here. # # There's a lot of weirdness here... # " If the -unknown handler returns anything else, # it is interpreted as a command prefix" # does not seem to be true. 'return create' does not work, # and in fact, 'return create' fails to pass the appended argument list, # the create routine gets called, and errors out with a bad # subcommand error. # # I suspect this is just a documentation error, where # 'command prefix' should just read 'command'. # save the needed value for the create command set _csave [::lindex $args 1] set _cflag true return [::_list ::list create] } proc create { args } { variable _cflag variable _csave if { $_cflag } { # more weirdness, cannot seem to set and use local variables, or # even global variables. # I tried: # set val [::_list $_csave {*}$args] # set _csave {} # return $val # and it did not work. # Tried: # variable _tval # set _tval [::_list $_csave {*}$args] # set _csave {} # return $_tval # and it did not work. set _cflag false return [::_list $_csave {*}$args] } return [::_list {*}$args] } proc append { args } { return [uplevel 1 ::lappend {*}$args] } proc assign { args } { return [uplevel 1 ::lassign {*}$args] } proc map { args } { return [uplevel 1 ::lmap {*}$args] } proc set { args } { return [uplevel 1 ::lset {*}$args] } } package provide listensemble 0.3 ====== [PL] You can have `append`, `assign` etc in the map, you don't need to put them in procedures. The `each` subcommand works just like `foreach` (the following is run off my initial suggestion): ====== list append x a b c a b c set x a b c list each i $x {list append y $i} set y a b c ====== (Please erase my parts when you're done with them, I don't want to spam up your page.) ---- [bll] 2018-7-24: My first test showed otherwise. Now I am confused -- no idea what went wrong the first time I tested. Could not get 'each' to work either. This is good though. assign was not working well at all. <> ---- '''[dkf] - 2018-07-25 07:19:07''' The main reason we haven't done something like this already is that it will break a lot of code. Probably won't even be possible in Tcl 9 because of the level of incompatibility; plain [list] is used a lot, often with completely uncontrolled first arguments. [bll] 2018-7-25 There is one problem left that I just found. A plain [list] command does not work. I don't know if it possible to work around that using [namespace ensemble]. It may be necessary to rewrite this without namespace ensemble to make it work. With that caveat (a rather large caveat), this is completely backwards compatible and should be completely usable. It should not break any existing code. [bll] 2018-7-25 Ok, new code. This should be completely backwards compatible. Would it be worth updating [namespace ensemble] to make it possible to call it without a sub-command? Even though I think having a list ensemble would be nice, I'm not entirely sure if I would actually use this in my code. The alternative doesn't bother me that much. This was originally an exercise in using namespace ensemble, but since that does not work, the new code. ---- '''[dkf] - 2018-07-26 18:01:47''' The big issue is that there is simply no way to make it safe for existing code, and [list] is pervasively used in situations where its handling of its first argument is critical. (I remember looking at this for 8.5 - or maybe 8.4 - when making ensembles for other commands.) If you want fancier handling of missing subcommands, try a TclOO object. Those delegate to their `unknown` method handler even when no method name is provided... so you can unexport everything (especially `destroy`) and get complete control... <>Enter Category Here