TIP 295: Enhance Arguments to lrange

Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2017 Conference, Houston/TX, US, Oct 16-20
Send your abstracts to tclconference@googlegroups.com
by Aug 21.
Author:         Andreas Leitgeb <avl@logic.at>
State:          Draft
Type:           Project
Vote:           Pending
Created:        06-Nov-2006
Keywords:       Tcl,lrange
Tcl-Version:    8.7


This TIP proposes an enhancement to lrange and string range that lets them take more than one start-end index pair.


Sometimes you need to extract a non-continuous selection of elements from some list and build a new list from them. This requires the use of multiple calls to lrange and a surrounding concat to build the final list. However, since the following fails with a syntax error:

    lrange {a b c d e f}  1 3  5 5

It would seem a reasonable extension for such a usage of lrange to return "b c d f". This would also make following pattern of usage feasible:

 lassign [lrange $someList 0 2  10 12]  var0 var1 var2 var10 var11 var12

The indices should not be required to be in ascending order. This is meant such that the ranges may be overlapping at will. E.g.:

 [lrange $list 0 end 0 end] == [lrepeat 2 {*}$list]

This TIP does NOT change the way each range is interpreted. Reversing lists is not in the scope of this TIP.

Proposed Change

Change the implementation of lrange to accept any odd number of arguments, with semantics (upon supply of a correct number of arguments) equivalent to:

 proc lrange-new {list args} {
    set result [list]
    foreach {start end} $args {
       lappend result {*}[lrange $list $start $end]
    return $result

Draft Implementation

Just the above mentioned procedure, at the moment.

Further Thoughts


This document has been placed in the public domain.