Purpose: Proposal for an official extension of additional list operations.
Proposal for an official extension of additional list operations. The proposal is based on the Chart of existing list functionality and the wishlist in Additional list functions.
- Andreas Kupries.
Please note: This proposal discusses only the functionality to include in the extension, but neither command names nor implementation issues (like C vs. Tcl, etc.).
Handling lists as sets
- Determine membership of an element in a list.
- Compute the union of 2 sets represented by 2 lists.
- Compute the intersection of 2 sets represented by 2 lists.
- Compute the symmetric difference of 2 sets represented by 2 lists.
- Compute the onesided difference of 2 sets represented by 2 lists.
- Check for an empty list, eh, set.
Sometimes intersection and both onesided differences are
required. A procedure computing them in parallel should be more
efficient than doing it separately.
- A procedure to compute intersection and both onesided differences.
Lists as queues and stacks
- A function to remove one or more items from the end of a list.
- A function to remove one or more items from the front of a list.
- A function to append one or more items at the end of a list. --- This function already exists in the kernel, lappend.
- A function to prepend one or more items before the front of a list.
All functions should operate in place, i.e. have list variables
as their arguments, not lists.
Other generic accessor and manipulator functionality
- A function to remove an item by position, in place (lreplace in the kernel does that, but not in place!)
- A function to replace an item by another item, in place. (lreplace, in the kernel, can do that too, but not in place)
TclX combines both of the above in one function, as does the kernel. To discuss.
- A function to remove items from a list, by name!
- A function to append new items to a list, as long as they are not already in the list.
- A function to append new items to a list, but only the non-empty ones.
- A function to append complete lists to a list, but not as single items. Their items become items of the modified list too (See 'lvarcat').
The last three functions could be combined to form a sort of
generalized lappend, with options controlling the behaviour.
- A function to add an item at an arbitrary place in the list, in place. Sort of generalized stack/queue insertion functionality.
- Lispish access to head item and tail of a list (car, cdr), and the complements, last item and front of a list. Four functions.
- Fast assignment of values in a list to different variables.
- Removal of duplicate items from a list. Sort of list2set conversion.
- Removal of empty items from a list.
- A function to reverse the order of the items in a list.
- A generalized lindex allowing the selection of more than one element, more than once, exclusion of elements, etc.
- Computation of the indices of items in a list, by name. Or pattern ?
Uses: Scatter/gather operations, permutations.
- A generic function for application of a command to all elements in a list.
- A generic function for application of a command to all elements in a list, with reduction to a single result.
Both of the above can be used as the base of several other nice functionalities,
see below. And they might be usable for the implementation of some of the
functionality listed above too.
- A function applying a regsub to all elements of a list and returning the results, again as a list.
- A function treating a list as list of lists (a matrix) and returning a specified column of that matrix (items of the outer list are rows). This is a projection operation.
- A function computing the length of the longest item in a list, the items are treated as strings.
- List universal/existential quantifiers: An expression has to hold for all items or item sequences in a list (universal), or for at least one item/item sequence (existential).
Searching in lists.
- A function extending the known kernel command lsearch to return all matching elements from a list. Allow negation, i.e. returning all non-matching elements ?
- Extend the above to allow multiple patterns.
Special functions, not fitting anywhere else
- A function to merge two or more lists into one, by interleaving the contents. Now to have lazy lists too :-).
- A function to split a single list into two or more. The reverse of the last function.
- A function to set several places in a list to an item, controlled by a list of indices. (Basically the second function in 3., extended to work for multiple items).