Current version compared with version Tue Nov 16 09:17:05 GMT 2004...
Alists can be implemented very well using {\[dict\]}, but since
{\[dict\]} is still only available in Tcl >= 8.5 this implementation
uses only commands available in Tcl 8.4 (notably {\[lset\]}).
- Btw note that alists implemented with [[dict]] lost some interesting
+ Btw note that alists implemented with {\[dict\]} lost some interesting
priprietis (because the lookup is no longer from "left" to "right",
and also can't hold multiple identical keys).
...
- '''BASIC USAGE'''
+ ===BASIC USAGE===
To define a new alist, the {\[alist\]} command is used:
...
===DEFAULT VALUES AND INITIALIZATION===
When alists are declared, we can specify the default value for
- every key. This way, the [[make-<alistname>]] command will create
+ every key. This way, the {\[make-<alistname>\]} command will create
alists with every value initialized to the default value specified
in the declaration.
...
alist flower {petals 0} {color none} {type unknow}
- Now every new flower alist created using [[make-flower]] will be
+ Now every new flower alist created using {\[make-flower\]} will be
initialized with the default values we provided. As you can see
to specify a default value, instead to use the key name, we use
a two-element list with the key name and the default value.
...
Will output: "This flower is a unknown with 0 none petals".
Another way to initialize fields at creation time is to pass
- arguments to the [[make-flower]] function. For example to
+ arguments to the {\[make-flower\]} function. For example to
create a flower with the default values but with 3 white petals
we can write:
...
set someflower [make-flower petals 3 color white]
It should be noted that if no default value is specified in the
- alist declaration, nor arguments are passed to the [[make-...]] command,
+ alist declaration, nor arguments are passed to the {\[make-...\]} command,
all the fields are initialized to a null string.
- '''NESTING'''
+ ===NESTING===
- alists (like [[dict]]s) are able to nest. Let's see why and how
+ alists (like {\[dict\]}s) are able to nest. Let's see why and how
to use this feature.
Suppose you are creating a program for a florist that ships
...
alist box color [list content [make-flower]] [list shipaddr [make-address]]
- This way all the box created using [[make-box]] will contain nested
+ This way all the box created using {\[make-box\]} will contain nested
flower and address alists.
Now to create the previous box we can write:
...
values (so, just strings), so you can pass and return they with
normal procedures.
- '''LISTS OF ALISTS'''
+ ===LISTS OF ALISTS===
Our florist wants to expand it's product line, so it plans to
ship boxes with more than one flower. The florist's software
...
alset box content $content
In order to make it more simple, the alist library provides
- the [[allappend]] command that works like [[lappend]] but against
+ the {\[allappend\]} command that works like {\[lappend\]} but against
alist fields. So we can rewrite the above three lines of code into:
allappend box content [make-flower color blue type tulipan]
...
'''ALISTS TYPE SYSTEM'''
- Every time an alist is created by the [[make-<name>]] command,
+ Every time an alist is created by the {\[make-<name>\]} command,
the generated alist contains all the keys the user specified
in the alist definition of that name, plus an additional
key __alisttype__ that has as default value the name of the
...
alget $p __alisttype__
Instead to directly get the __alisttype__ key, there is the
- command [[altype]] that does just this. So the above code is
+ command {\[altype\]} that does just this. So the above code is
equivalent to:
altype $p
...
The output will be 1020, 10 20 and 30
- '''OTHER UTILITIES'''
+ ===OTHER UTILITIES===
- [[alincr]] increments the integer at the specified alist's key.
+ {\[alincr\]} increments the integer at the specified alist's key.
Example:
alincr alistVarName x.y -3
...
The last argument (the increment) can be omitted and defaults to 1.
- [[alsappend]] is like [[allappend]] but append strings to the specified
+ {\[alsappend\]} is like {\[allappend\]} but append strings to the specified
string instead to elements to the specified list.
- it's called 'sappend' because append strings. Since [[allappend]] is
- similar to [[lappend]], and [[alsappend]] is similar to [[append]] you may
- wonder why the name of this command is not just [[alappend]]. That's because
+ it's called 'sappend' because append strings. Since {\[allappend\]} is
+ similar to {\[lappend\]}, and {\[alsappend\]} is similar to {\[append\]} you may
+ wonder why the name of this command is not just {\[alappend\]}. That's because
it's too simple to write "alappend" instead of "allappend".
Happy nesting.
...
- }
# Alists - Lisp-like alist data structures.
# Copyright (C) 2004 Salvatore Sanfilippo <antirez@invece.org>.
...
The following is the old page content
|