TIP 367: A Command to Remove Elements from a List

Login
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.
State:		Draft
Type:		Project
Tcl-Version:	8.7
Vote:		Pending
Post-History:	
Author:		Donal K. Fellows <dkf@users.sf.net>
Created:	18-May-2010
Keywords:	Tcl, delete, item

Abstract

This TIP proposes a command, lremove, that takes a list value and a collection of indices, and returns a list that is the input list with the elements at those indices removed.

Rationale

Tcl has many operations for working with lists, such as list for building them, lappend for adding to them, linsert for insertion, lreplace for replacement of ranges, and lset for replacement of individual elements, but it has none that is designed to remove elements of a list. While the functionality can be simulated in the simple case with lreplace, this is rather more difficult when multiple indices are present. It is particularly challenging when using a mixture of indices that are defined relative to the start and the end of the list. Since the tools for doing the mapping of indices to list positions are easily available at the C level, I propose to add a command to Tcl to do the removal operation that takes advantage of the capabilities to do this all correctly.

Proposed Change

This TIP proposes adding a command, lremove, with the following syntax:

lremove list ?index? ?index...?

That is, the command takes one mandatory argument, list, and an arbitrary number of index arguments (including zero). The list argument must be a valid Tcl list, and each of the index arguments must be a valid Tcl index (see [176] for a description) where end will refer to the last element of list. Assuming syntactic validity, the result will be a list that is the same as list except for the removal of the elements at each given index. The result shall be as if all removals happen simultaneously and the order of the index arguments shall be unimportant; if an element is indicated twice (whether through syntactically identical indices or not) then it will be as if it was only indicated once.

Examples

% lremove {a b c d e} 1
a c d e
% lremove {a b c d e} end-1
a b c e
% lremove {a b c d e} 1 3
a c e
% lremove {a b c d e} 3 1
a c e
% lremove {a b c d e} 2 2
a b d e
% lremove {a b c d e} 3 end-1
a b c e
% lremove {a b c d e} 1 3 1 4 0
c

Implementation

Pending.

Copyright

This document has been placed in the public domain.

History