**list ensemble** [bll] 2018-7-24: An exercise in learning about [namespace ensemble]. 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 by using the namespace ensemble -unknown handler. It seems to work, but has not been tested extensively. I had a lot of trouble creating this. namespace ensemble seems to be very sensitive. Many things I tried did not work as expected from reading the wiki page on namespace ensemble. The documentation could use some updating to be more precise and could use some quality examples. ====== #!/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 index insert length map range repeat replace reverse search set sort} \ -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. # 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 } { set _cflag false return [::_list $_csave {*}$args] } return [::_list {*}$args] } } package provide listensemble 0.4 # 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] list set 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] 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] 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. <> <>Enter Category Here