Provides the pw::listutils command ensemble to perform setwise operations on lists. See the Set (mathematics) wiki for details.
To use this library you must include listutils.tcl
in your application script.
source "/some/path/to/your/copy/of/listutils.tcl"
All of the procs in this collection reside in the pw::listutils ensemble.
To call a proc in this collection, you must prefix the proc name with a pw::listutils ensemble name.
For example:
source "/some/path/to/your/copy/of/listutils.tcl"
pw::listutils lunion {1 2 3} {a b c}
pw::listutils lmutate {a b c}
To avoid the long ensemble name prefix, you can also import the public pw::listutils procs into your script.
For example, to import all procs.
source "/some/path/to/your/copy/of/listutils.tcl"
# import all public procs
namespace import ::pw::listutils::*
lunion {1 2 3} {a b c}
lmutate {a b c}
Or, to only import specific procs.
source "/some/path/to/your/copy/of/listutils.tcl"
# import all public procs
namespace import ::pw::listutils::lunion
namespace import ::pw::listutils::lmutate
lunion {1 2 3} {a b c}
lmutate {a b c}
Commands in this ensemble are accessed as:
pw::listutils <cmd> ?<options>?
Where,
cmd
- Is one of the setwise operation names.
options
- Optional, cmd dependent options.
pw::listutils lproduct <subcmd> ?<options>?
Computes the product for a collection of lists.
For example, the product of {1 2 3}
and {a b}
is
{{1 a} {1 b} {2 a} {2 b} {3 a} {3 b}}
subCmd
- One of get or foreach.
pw::listutils lproduct get <list> ?<list> ...?
Returns the product as a list of sub-product lists.
list ?lists ...?
- One or more lists used used to compute the product.
pw::listutils lproduct foreach <varname> <list> ?<list> ...? <body>
Each sub-product is passed to the script defined by body using the specified varname.
varname
- Name of the sub-product script variable.
list ?lists ...?
- One or more lists used used to compute the product.
body
- The script to execute for each sub-product.
pw::listutils lmutate <subcmd> ?<options>?
Computes the permutations of a list.
For example, the permutations of {a b c}
are {{a b c} {a c b} {b a c} {b c a} {c b a} {c a b}}
.
subCmd
- One of get or foreach.
pw::listutils lmutate get <list>
Returns the permutations as a list of lists.
list
- The list to mutate.
pw::listutils lmutate foreach <varname> <list> <body>
Each permutation is passed to the script defined by body using the specified varname.
varname
- Name of the permutation script variable.
list
- The list to mutate.
body
- The script to execute for each permutation.
pw::listutils lunion ?<list> ...?
Returns the union of a collection of lists.
For example, the union of {1 2 3}
and {a b}
is {1 2 3 a b}
.
list ...
- The lists used used to compute the union. If no lists are provided, an empty list is returned.
pw::listutils lintersect <list> <list> ?<list> ...?
Returns the intersection of a collection of lists.
For example, the intersection of {1 2 3 a}
and {a 2 z}
is {a 2}
.
list
- Two or more lists used used to compute the intersection.
pw::listutils lsubtract <list> <list> ?<list> ...?
Returns the left-to-rigth subtraction of a collection of lists.
For example, the subtraction of {1 2 3 a}
and {a 2 z}
is {1 3}
.
list
- Two or more lists used used to compute the subtraction.
pw::listutils lsymmetricdiff <list> <list> ?<list> ...?
Returns the symmetric difference of a collection of lists. A symmetric difference of A and B is equivalent to ((A subtract B) union (B subtract A)).
For example, the symmetric difference of {1 2 3 a}
and {a 2 z}
is {1 3 z}
.
list
- Two or more lists used used to compute the symmetricdifference.
pw::listutils lissubset <superlist> <sublist> ?<sublist> ...?
Returns true if all sublist lists are a subset of superlist.
For example, {1 a}
is a sublist of {1 2 3 a}
.
superlist
- The list to compare all sublists against.
sublist
- One or more subset lists.
pw::listutils lunique <list>
Returns a copy of list with all duplicates removed.
For example, lunique {1 a b c 2 3 1 b 9}
returns {1 a b c 2 3 9}
.
list
- The list to process.
pw::listutils lremove <listvarname> <value> ?<options>?
Removes the requested value from the list. Returns nothing.
For example, set lst {a b c d e} ; lremove lst c -sorted
sets $lst
equal
to {a b d e}
.
listvarname
- The list to process.
value
- The value to remove from the list.
options
- Any options supported by `lsearch $lst $value`.
pw::listutils lstitch <list1> ?<list2>? ?<repeat>?
Returns a single list comprised of alternating values from the list1
and
list2
. The returned list will be the same length as list1
and can be used
as a dict
.
For example, lstitch {1 2 3 4} {a b c d}
returns {1 a 2 b 3 c 4 d}
.
list1
- The list of dict keys.
list2
- The list of dict values.
repeat
- If 1, `list2` will be repeated as needed to provide values for `list1`. If 0, any unmatched keys will have a value of {}. The default is 0.
pw::listutils lshift <listvarname>
Removes the first item from list and returns it. The list is modifed by this proc. If the list is empty, {} is returned.
For example, set lst {1 2 3 4} ; lshift lst
returns 1
and sets lst
equal
to {2 3 4}
.
listvarname
- The list to process.
xxxx
Scripts are freely provided. They are not supported products of Pointwise, Inc. Some scripts have been written and contributed by third parties outside of Pointwise's control.
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, POINTWISE DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH REGARD TO THESE SCRIPTS. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL POINTWISE BE LIABLE TO ANY PARTY FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THESE SCRIPTS EVEN IF POINTWISE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES AND REGARDLESS OF THE FAULT OR NEGLIGENCE OF POINTWISE.