TIP #303: Enhance 'llength' Command to Support Nested Lists


TIP:303
Title:Enhance 'llength' Command to Support Nested Lists
Version:$Revision: 1.3 $
Author:Wolf-Dieter Busch <wolf-dieter dot busch at gmx dot de>
State:Draft
Type:Project
Tcl-Version:8.7
Vote:Pending
Created:Monday, 29 January 2007
Keywords:Tcl, lindex

Abstract

The command llength currently returns the length of a list. Sometimes it is desirable to know the length of a nested list. This TIP proposes an improvement to llength to do that.

Description

Currently, finding the length of a nested list requires the combination of llength and lindex. This works, but is not very clean for a comparatively common operation when you compare with straight lindex usage. This TIP proposes to enhance llength so that it also does the indexing, making it's usage in such situations cleaner and less subject to programming errors.

Proposed Change

The llength command shall be updated to have syntax like this:

llength list ?indexList ...?

When no indexList is supplied, the current behavior is used. When one or more indexList arguments are supplied, they are used to restrict which part of list is taken the length of, just as if lindex had been used to index into list.

Thus, [llength {a {b c} d}] shall return 3 as nowadays, [llength {a {b c} d} 1]> shall return the length of the nested list on index 1, here {b c} => 2, and [llength {a {b c} d} 1 0] shall return the length of the nested list on index 0 of nested list on index 1, i.e., 1 in this case.

Compatibility

As this only changes a way of use of the llength that currently returns an error, there are no compatibility problems.

Example Implementation

The procedure idxllength (below) does what is described above, but inefficiently:

proc idxllength args {
    llength [lindex {*}$args]
}

Copyright

This document has been placed in the public domain.


Powered by Tcl[Index] [History] [Edit] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows