TIP #295 Version 1.4: Enhance Arguments to lrange

This is not necessarily the current version of this TIP.


TIP:295
Title:Enhance Arguments to lrange
Version:$Revision: 1.4 $
Author:Andreas Leitgeb <avl at logic dot at>
State:Draft
Type:Project
Tcl-Version:8.5
Vote:Pending
Created:Monday, 06 November 2006
Keywords:Tcl, lrange

Abstract

This TIP proposes an enhancement to lrange that lets it take more than one start-end index pair.

Rationale

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 index-pairs should not be required to be in ascending order (this might be difficult to determine when one index is end-relative), or even disjoint. Even:

 lrange {a b c d}  3 3  0 3  0 0

should be made legal and return "d a b c d a".

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

Uwe Klein asks: "Should this support reverse ranges?"

This TIP is just about implicit concat'ing multiple lranges of the same list. Reversing lists doesn't fit here. (except if they are only constant small sized, then

 lrange $list size size size-1 size-1 ... 1 1 0 0

would have that effect.

Second Edit: I only now realize that the formulation may be a bit misleading:

 The index-pairs should ''not'' be required to be in ascending order

means, that using multiple ranges is not limited to cut holes into the original list, but that each range is completely independent of all previous and following ranges. e.g.:

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

Copyright

This document has been placed in the public domain.


Powered by TclThis is not necessarily the current version of this TIP.

TIP AutoGenerator - written by Donal K. Fellows