File
tip/367.tip
— part of check-in
[f8a1afa2aa]
at
2010-05-18 10:57:26
on branch trunk
— Created for Donal K. Fellows <[email protected]>
(user:
dkf
size: 2534)
0000: 54 49 50 3a 09 09 33 36 37 0a 54 69 74 6c 65 3a TIP:..367.Title:
0010: 09 09 41 20 43 6f 6d 6d 61 6e 64 20 74 6f 20 52 ..A Command to R
0020: 65 6d 6f 76 65 20 45 6c 65 6d 65 6e 74 73 20 66 emove Elements f
0030: 72 6f 6d 20 61 20 4c 69 73 74 0a 53 74 61 74 65 rom a List.State
0040: 3a 09 09 44 72 61 66 74 0a 54 79 70 65 3a 09 09 :..Draft.Type:..
0050: 50 72 6f 6a 65 63 74 0a 54 63 6c 2d 56 65 72 73 Project.Tcl-Vers
0060: 69 6f 6e 3a 09 38 2e 37 0a 56 6f 74 65 3a 09 09 ion:.8.7.Vote:..
0070: 50 65 6e 64 69 6e 67 0a 50 6f 73 74 2d 48 69 73 Pending.Post-His
0080: 74 6f 72 79 3a 09 0a 56 65 72 73 69 6f 6e 3a 09 tory:..Version:.
0090: 24 52 65 76 69 73 69 6f 6e 3a 20 31 2e 31 20 24 $Revision: 1.1 $
00a0: 0a 41 75 74 68 6f 72 3a 09 09 44 6f 6e 61 6c 20 .Author:..Donal
00b0: 4b 2e 20 46 65 6c 6c 6f 77 73 20 3c 64 6b 66 40 K. Fellows <dkf@
00c0: 75 73 65 72 73 2e 73 66 2e 6e 65 74 3e 0a 43 72 users.sf.net>.Cr
00d0: 65 61 74 65 64 3a 09 31 38 2d 4d 61 79 2d 32 30 eated:.18-May-20
00e0: 31 30 0a 4b 65 79 77 6f 72 64 73 3a 09 54 63 6c 10.Keywords:.Tcl
00f0: 2c 20 64 65 6c 65 74 65 2c 20 69 74 65 6d 0a 0a , delete, item..
0100: 7e 20 41 62 73 74 72 61 63 74 0a 0a 54 68 69 73 ~ Abstract..This
0110: 20 54 49 50 20 70 72 6f 70 6f 73 65 73 20 61 20 TIP proposes a
0120: 63 6f 6d 6d 61 6e 64 2c 20 27 27 27 6c 72 65 6d command, '''lrem
0130: 6f 76 65 27 27 27 2c 20 74 68 61 74 20 74 61 6b ove''', that tak
0140: 65 73 20 61 20 6c 69 73 74 20 76 61 6c 75 65 20 es a list value
0150: 61 6e 64 20 61 0a 63 6f 6c 6c 65 63 74 69 6f 6e and a.collection
0160: 20 6f 66 20 69 6e 64 69 63 65 73 2c 20 61 6e 64 of indices, and
0170: 20 72 65 74 75 72 6e 73 20 61 20 6c 69 73 74 20 returns a list
0180: 74 68 61 74 20 69 73 20 74 68 65 20 69 6e 70 75 that is the inpu
0190: 74 20 6c 69 73 74 20 77 69 74 68 20 74 68 65 0a t list with the.
01a0: 65 6c 65 6d 65 6e 74 73 20 61 74 20 74 68 6f 73 elements at thos
01b0: 65 20 69 6e 64 69 63 65 73 20 72 65 6d 6f 76 65 e indices remove
01c0: 64 2e 0a 0a 7e 20 52 61 74 69 6f 6e 61 6c 65 0a d...~ Rationale.
01d0: 0a 54 63 6c 20 68 61 73 20 6d 61 6e 79 20 6f 70 .Tcl has many op
01e0: 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 77 6f 72 erations for wor
01f0: 6b 69 6e 67 20 77 69 74 68 20 6c 69 73 74 73 2c king with lists,
0200: 20 73 75 63 68 20 61 73 20 27 27 27 6c 69 73 74 such as '''list
0210: 27 27 27 20 66 6f 72 0a 62 75 69 6c 64 69 6e 67 ''' for.building
0220: 20 74 68 65 6d 2c 20 27 27 27 6c 61 70 70 65 6e them, '''lappen
0230: 64 27 27 27 20 66 6f 72 20 61 64 64 69 6e 67 20 d''' for adding
0240: 74 6f 20 74 68 65 6d 2c 20 27 27 27 6c 69 6e 73 to them, '''lins
0250: 65 72 74 27 27 27 20 66 6f 72 20 69 6e 73 65 72 ert''' for inser
0260: 74 69 6f 6e 2c 0a 27 27 27 6c 72 65 70 6c 61 63 tion,.'''lreplac
0270: 65 27 27 27 20 66 6f 72 20 72 65 70 6c 61 63 65 e''' for replace
0280: 6d 65 6e 74 20 6f 66 20 72 61 6e 67 65 73 2c 20 ment of ranges,
0290: 61 6e 64 20 27 27 27 6c 73 65 74 27 27 27 20 66 and '''lset''' f
02a0: 6f 72 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 6f or replacement o
02b0: 66 0a 69 6e 64 69 76 69 64 75 61 6c 20 65 6c 65 f.individual ele
02c0: 6d 65 6e 74 73 2c 20 62 75 74 20 69 74 20 68 61 ments, but it ha
02d0: 73 20 6e 6f 6e 65 20 74 68 61 74 20 69 73 20 64 s none that is d
02e0: 65 73 69 67 6e 65 64 20 74 6f 20 72 65 6d 6f 76 esigned to remov
02f0: 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 0a e elements of a.
0300: 6c 69 73 74 2e 20 57 68 69 6c 65 20 74 68 65 20 list. While the
0310: 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 63 61 functionality ca
0320: 6e 20 62 65 20 73 69 6d 75 6c 61 74 65 64 20 69 n be simulated i
0330: 6e 20 74 68 65 20 73 69 6d 70 6c 65 20 63 61 73 n the simple cas
0340: 65 20 77 69 74 68 0a 27 27 27 6c 72 65 70 6c 61 e with.'''lrepla
0350: 63 65 27 27 27 2c 20 74 68 69 73 20 69 73 20 72 ce''', this is r
0360: 61 74 68 65 72 20 6d 6f 72 65 20 64 69 66 66 69 ather more diffi
0370: 63 75 6c 74 20 77 68 65 6e 20 6d 75 6c 74 69 70 cult when multip
0380: 6c 65 20 69 6e 64 69 63 65 73 20 61 72 65 0a 70 le indices are.p
0390: 72 65 73 65 6e 74 2e 20 49 74 20 69 73 20 70 61 resent. It is pa
03a0: 72 74 69 63 75 6c 61 72 6c 79 20 63 68 61 6c 6c rticularly chall
03b0: 65 6e 67 69 6e 67 20 77 68 65 6e 20 75 73 69 6e enging when usin
03c0: 67 20 61 20 6d 69 78 74 75 72 65 20 6f 66 20 69 g a mixture of i
03d0: 6e 64 69 63 65 73 20 74 68 61 74 0a 61 72 65 20 ndices that.are
03e0: 64 65 66 69 6e 65 64 20 72 65 6c 61 74 69 76 65 defined relative
03f0: 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 61 6e to the start an
0400: 64 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 d the end of the
0410: 20 6c 69 73 74 2e 20 53 69 6e 63 65 20 74 68 65 list. Since the
0420: 20 74 6f 6f 6c 73 20 66 6f 72 0a 64 6f 69 6e 67 tools for.doing
0430: 20 74 68 65 20 6d 61 70 70 69 6e 67 20 6f 66 20 the mapping of
0440: 69 6e 64 69 63 65 73 20 74 6f 20 6c 69 73 74 20 indices to list
0450: 70 6f 73 69 74 69 6f 6e 73 20 61 72 65 20 65 61 positions are ea
0460: 73 69 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 61 sily available a
0470: 74 20 74 68 65 20 43 0a 6c 65 76 65 6c 2c 20 49 t the C.level, I
0480: 20 70 72 6f 70 6f 73 65 20 74 6f 20 61 64 64 20 propose to add
0490: 61 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 54 63 6c a command to Tcl
04a0: 20 74 6f 20 64 6f 20 74 68 65 20 72 65 6d 6f 76 to do the remov
04b0: 61 6c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 al operation tha
04c0: 74 0a 74 61 6b 65 73 20 61 64 76 61 6e 74 61 67 t.takes advantag
04d0: 65 20 6f 66 20 74 68 65 20 63 61 70 61 62 69 6c e of the capabil
04e0: 69 74 69 65 73 20 74 6f 20 64 6f 20 74 68 69 73 ities to do this
04f0: 20 61 6c 6c 20 63 6f 72 72 65 63 74 6c 79 2e 0a all correctly..
0500: 0a 7e 20 50 72 6f 70 6f 73 65 64 20 43 68 61 6e .~ Proposed Chan
0510: 67 65 0a 0a 54 68 69 73 20 54 49 50 20 70 72 6f ge..This TIP pro
0520: 70 6f 73 65 73 20 61 64 64 69 6e 67 20 61 20 63 poses adding a c
0530: 6f 6d 6d 61 6e 64 2c 20 27 27 27 6c 72 65 6d 6f ommand, '''lremo
0540: 76 65 27 27 27 2c 20 77 69 74 68 20 74 68 65 20 ve''', with the
0550: 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6e 74 61 78 following syntax
0560: 3a 0a 0a 20 3e 20 27 27 27 6c 72 65 6d 6f 76 65 :.. > '''lremove
0570: 27 27 27 20 27 27 6c 69 73 74 27 27 20 3f 27 27 ''' ''list'' ?''
0580: 69 6e 64 65 78 27 27 3f 20 3f 27 27 69 6e 64 65 index''? ?''inde
0590: 78 2e 2e 2e 27 27 3f 0a 0a 54 68 61 74 20 69 73 x...''?..That is
05a0: 2c 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 74 61 , the command ta
05b0: 6b 65 73 20 6f 6e 65 20 6d 61 6e 64 61 74 6f 72 kes one mandator
05c0: 79 20 61 72 67 75 6d 65 6e 74 2c 20 27 27 6c 69 y argument, ''li
05d0: 73 74 27 27 2c 20 61 6e 64 20 61 6e 20 61 72 62 st'', and an arb
05e0: 69 74 72 61 72 79 0a 6e 75 6d 62 65 72 20 6f 66 itrary.number of
05f0: 20 27 27 69 6e 64 65 78 27 27 20 61 72 67 75 6d ''index'' argum
0600: 65 6e 74 73 20 28 69 6e 63 6c 75 64 69 6e 67 20 ents (including
0610: 7a 65 72 6f 29 2e 20 54 68 65 20 27 27 6c 69 73 zero). The ''lis
0620: 74 27 27 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 t'' argument mus
0630: 74 20 62 65 0a 61 20 76 61 6c 69 64 20 54 63 6c t be.a valid Tcl
0640: 20 6c 69 73 74 2c 20 61 6e 64 20 65 61 63 68 20 list, and each
0650: 6f 66 20 74 68 65 20 27 27 69 6e 64 65 78 27 27 of the ''index''
0660: 20 61 72 67 75 6d 65 6e 74 73 20 6d 75 73 74 20 arguments must
0670: 62 65 20 61 20 76 61 6c 69 64 20 54 63 6c 0a 69 be a valid Tcl.i
0680: 6e 64 65 78 20 28 73 65 65 20 5b 31 37 36 5d 20 ndex (see [176]
0690: 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f for a descriptio
06a0: 6e 29 20 77 68 65 72 65 20 27 27 27 65 6e 64 27 n) where '''end'
06b0: 27 27 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f '' will refer to
06c0: 20 74 68 65 20 6c 61 73 74 0a 65 6c 65 6d 65 6e the last.elemen
06d0: 74 20 6f 66 20 27 27 6c 69 73 74 27 27 2e 20 41 t of ''list''. A
06e0: 73 73 75 6d 69 6e 67 20 73 79 6e 74 61 63 74 69 ssuming syntacti
06f0: 63 20 76 61 6c 69 64 69 74 79 2c 20 74 68 65 20 c validity, the
0700: 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 result will be a
0710: 20 6c 69 73 74 0a 74 68 61 74 20 69 73 20 74 68 list.that is th
0720: 65 20 73 61 6d 65 20 61 73 20 27 27 6c 69 73 74 e same as ''list
0730: 27 27 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 '' except for th
0740: 65 20 72 65 6d 6f 76 61 6c 20 6f 66 20 74 68 65 e removal of the
0750: 20 65 6c 65 6d 65 6e 74 73 20 61 74 20 65 61 63 elements at eac
0760: 68 0a 67 69 76 65 6e 20 27 27 69 6e 64 65 78 27 h.given ''index'
0770: 27 2e 20 54 68 65 20 72 65 73 75 6c 74 20 73 68 '. The result sh
0780: 61 6c 6c 20 62 65 20 61 73 20 69 66 20 61 6c 6c all be as if all
0790: 20 72 65 6d 6f 76 61 6c 73 20 68 61 70 70 65 6e removals happen
07a0: 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a simultaneously.
07b0: 61 6e 64 20 74 68 65 20 6f 72 64 65 72 20 6f 66 and the order of
07c0: 20 74 68 65 20 27 27 69 6e 64 65 78 27 27 20 61 the ''index'' a
07d0: 72 67 75 6d 65 6e 74 73 20 73 68 61 6c 6c 20 62 rguments shall b
07e0: 65 20 75 6e 69 6d 70 6f 72 74 61 6e 74 3b 20 69 e unimportant; i
07f0: 66 20 61 6e 20 65 6c 65 6d 65 6e 74 0a 69 73 20 f an element.is
0800: 69 6e 64 69 63 61 74 65 64 20 74 77 69 63 65 20 indicated twice
0810: 28 77 68 65 74 68 65 72 20 74 68 72 6f 75 67 68 (whether through
0820: 20 73 79 6e 74 61 63 74 69 63 61 6c 6c 79 20 69 syntactically i
0830: 64 65 6e 74 69 63 61 6c 20 69 6e 64 69 63 65 73 dentical indices
0840: 20 6f 72 20 6e 6f 74 29 0a 74 68 65 6e 20 69 74 or not).then it
0850: 20 77 69 6c 6c 20 62 65 20 61 73 20 69 66 20 69 will be as if i
0860: 74 20 77 61 73 20 6f 6e 6c 79 20 69 6e 64 69 63 t was only indic
0870: 61 74 65 64 20 6f 6e 63 65 2e 0a 0a 7e 20 45 78 ated once...~ Ex
0880: 61 6d 70 6c 65 73 0a 0a 7c 25 20 6c 72 65 6d 6f amples..|% lremo
0890: 76 65 20 7b 61 20 62 20 63 20 64 20 65 7d 20 31 ve {a b c d e} 1
08a0: 0a 7c 61 20 63 20 64 20 65 0a 7c 25 20 6c 72 65 .|a c d e.|% lre
08b0: 6d 6f 76 65 20 7b 61 20 62 20 63 20 64 20 65 7d move {a b c d e}
08c0: 20 65 6e 64 2d 31 0a 7c 61 20 62 20 63 20 65 0a end-1.|a b c e.
08d0: 7c 25 20 6c 72 65 6d 6f 76 65 20 7b 61 20 62 20 |% lremove {a b
08e0: 63 20 64 20 65 7d 20 31 20 33 0a 7c 61 20 63 20 c d e} 1 3.|a c
08f0: 65 0a 7c 25 20 6c 72 65 6d 6f 76 65 20 7b 61 20 e.|% lremove {a
0900: 62 20 63 20 64 20 65 7d 20 33 20 31 0a 7c 61 20 b c d e} 3 1.|a
0910: 63 20 65 0a 7c 25 20 6c 72 65 6d 6f 76 65 20 7b c e.|% lremove {
0920: 61 20 62 20 63 20 64 20 65 7d 20 32 20 32 0a 7c a b c d e} 2 2.|
0930: 61 20 62 20 64 20 65 0a 7c 25 20 6c 72 65 6d 6f a b d e.|% lremo
0940: 76 65 20 7b 61 20 62 20 63 20 64 20 65 7d 20 33 ve {a b c d e} 3
0950: 20 65 6e 64 2d 31 0a 7c 61 20 62 20 63 20 65 0a end-1.|a b c e.
0960: 7c 25 20 6c 72 65 6d 6f 76 65 20 7b 61 20 62 20 |% lremove {a b
0970: 63 20 64 20 65 7d 20 31 20 33 20 31 20 34 20 30 c d e} 1 3 1 4 0
0980: 0a 7c 63 0a 0a 7e 20 49 6d 70 6c 65 6d 65 6e 74 .|c..~ Implement
0990: 61 74 69 6f 6e 0a 0a 27 27 50 65 6e 64 69 6e 67 ation..''Pending
09a0: 2e 27 27 0a 0a 7e 20 43 6f 70 79 72 69 67 68 74 .''..~ Copyright
09b0: 0a 0a 54 68 69 73 20 64 6f 63 75 6d 65 6e 74 20 ..This document
09c0: 68 61 73 20 62 65 65 6e 20 70 6c 61 63 65 64 20 has been placed
09d0: 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20 64 6f in the public do
09e0: 6d 61 69 6e 2e 0a main..