Skip to content

Instantly share code, notes, and snippets.

@sebres
Created May 11, 2020 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebres/8ef0bb82eb39d119cb2f22efe5ee3ed5 to your computer and use it in GitHub Desktop.
Save sebres/8ef0bb82eb39d119cb2f22efe5ee3ed5 to your computer and use it in GitHub Desktop.
lpop -- definition for lpop command (compatible to TIP 523 implementation)
if {[namespace which -command ::lpop] eq {}} {
proc ::lpop {listvar {index end} args} {
upvar $listvar l
if {![info exists l]} {
return -code error "can't read \"$listvar\": no such variable"
}
if {![llength $args]} {
if {$index eq "end"} {
if {![llength $l]} {
return -code error -errorcode {TCL VALUE INDEX OUTOFRANGE} "index \"$index\" out of range"
}
} elseif {[string is integer -strict $index]} {
if {$index >= [llength $l] || $index < 0} {
return -code error -errorcode {TCL VALUE INDEX OUTOFRANGE} "index \"$index\" out of range"
}
} else {
set i [expr [regsub {\mend\M} $index [expr {[llength $l]-1}]]]
if {$i >= [llength $l] || $i < 0} {
return -code error -errorcode {TCL VALUE INDEX OUTOFRANGE} "index \"$index\" out of range"
}
}
set v [lindex $l $index]
set l [lreplace $l [set l $index] $index]
} else {
set v [lindex $l $index {*}$args]
set i [lrange $args 0 end-1]
set l2 [lindex $l $index {*}$i]
lset l $index {*}$i [lreplace $l2 [lindex $args end] [lindex $args end]]
}
set v
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment