Document the dictionary module.
This commit is contained in:
parent
bb118c0fbe
commit
b0dc7d478e
2 changed files with 91 additions and 2 deletions
87
README.md
87
README.md
|
@ -89,6 +89,93 @@ implemented anyway to not require any external dependencies.
|
|||
|
||||
### Dictionary
|
||||
|
||||
Simple key/value dictionary with most operations implemented in linear
|
||||
time. The dictionary is internally represented as ```assq``` list and
|
||||
is best suitable for symbols and numbers as keys.
|
||||
|
||||
(make-dict)
|
||||
|
||||
Creates an empty dictionary.
|
||||
|
||||
(dict-has-key? d k)
|
||||
|
||||
* ```d``` - the dictionary
|
||||
* ```k``` - key to check
|
||||
|
||||
Returns ```#t``` (true) if the dictionary ```d``` contains the key
|
||||
```k```.
|
||||
|
||||
(dict-ref d k [v])
|
||||
|
||||
* ```d``` - the dictionary
|
||||
* ```k``` - the key to retrieve
|
||||
* ```v``` - optional default value
|
||||
|
||||
Retrieves given key ```k``` from the dictionary ```d```.
|
||||
|
||||
If the key is not stored in the dictionary an error is raised unless
|
||||
an optional value ```v``` is provided which is then returned in that
|
||||
case.
|
||||
|
||||
(dict-remove d k)
|
||||
|
||||
* ```d``` - the dictionary
|
||||
* ```k``` - the key to remove
|
||||
|
||||
Removes the key ```k``` from the given dictionary ```d``` raising an
|
||||
exception if the key is not stored in the dictionary.
|
||||
|
||||
(dict-set d k v)
|
||||
|
||||
* ```d``` - the dictionary
|
||||
* ```k``` - key to (re)set
|
||||
* ```v``` - the value to set
|
||||
|
||||
If the dictionary ```d``` does not contain the key ```k```, adds the
|
||||
value ```v``` into it under the given key. If the key is present, its
|
||||
value is then replaced.
|
||||
|
||||
(dict-keys d)
|
||||
|
||||
* ```d``` - the dictionary
|
||||
|
||||
Returns the list of keys contained in the dictionary ```d```.
|
||||
|
||||
(dict-map proc d)
|
||||
|
||||
* ```proc``` - procedure for processing
|
||||
* ```d``` - the dictionary
|
||||
|
||||
Returns a dictionary created by processing all elements of the
|
||||
dictionary ```d``` using the procedure ```proc```.
|
||||
|
||||
If the procedure accepts just one argument, only values are passed to
|
||||
it. If the procedure accepts two arguments, both the key and value are
|
||||
passed to it. In both cases, the procedure must produce only the value.
|
||||
|
||||
(dict-filter pred? d)
|
||||
|
||||
* ```pred?``` - predicate procedure
|
||||
* ```d``` - the dictionary
|
||||
|
||||
Returns a new dictionary created by keeping only the key/value pairs
|
||||
from the dictionary ```d``` matching the predicate ```pred?```.
|
||||
|
||||
If the procedure accepts just one argument, only values are passed to
|
||||
it. If the procedure accepts two arguments, both the key and value are
|
||||
passed to it.
|
||||
|
||||
(dict-reduce init proc d)
|
||||
|
||||
* ```init``` - arbitrary initial value
|
||||
* ```proc``` - procedure for performing the reducing step
|
||||
* ```d``` - the dictionary to reduce
|
||||
|
||||
Iterates over the key/value pairs of given dictionary ```d```
|
||||
initializing the algorithm with the ```init``` value given. In each
|
||||
step the procedure ```proc``` is called with three arguments: the
|
||||
value accumulated so far, key and value.
|
||||
|
||||
### Listing
|
||||
|
||||
This module implements listing a text file with line numbers and
|
||||
|
|
|
@ -29,8 +29,10 @@
|
|||
dictionary
|
||||
(
|
||||
make-dict
|
||||
dict-has-key? dict-ref
|
||||
dict-remove dict-set
|
||||
dict-has-key?
|
||||
dict-ref
|
||||
dict-remove
|
||||
dict-set
|
||||
dict-keys
|
||||
dict-map
|
||||
dict-filter
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue