Notes from Practical Common Lisp
Vector are Common Lisp's basic index-based collection and they come in two flavors. Fixes-size and adjustable. To make an arbitrarily resizable vector, you need to pass MAKE-ARRAY another keyword argument: :adjustable. To add item to an adjustable vector, you need use VECTOR-PUSH-EXTEND,which works like the vector-push except it will adjust the size of vector automatically. All the vectors you’ve dealt with so far have been general vectors that can hold any type of object.It’s also possible to create specialized vectors that are restricted to holding certain types of elements.Name | Required Arguments | Returns |
Count | Item and Sequence | Number of times item appears in sequence |
Find | Item and Sequence | Item and Sequence |
Position | Item and Sequence | Index into sequence or NIL |
remove | Item and Sequence | Sequence with instances of item removed |
substitute | New item, item, and sequence | Sequence with instances of item replaced with new item |
You can modify the behavior of these five functions in a variety of ways using keyword arguments. For instance, these functions, by default, look for elements in the sequence that are the same object as the item argument. You can change this in two ways: First, you can use the :test keyword to pass a function that accepts two arguments and returns a boolean. If provided, it will be used to compare item to each element instead of the default object equality test, EQL.5 Second, with the :key keyword you can pass a one-argument function to be called on each element of the sequence to extract a key value, which will then be compared to the item in the place of the element itself.
Four other handy functions are EVERY, SOME, NOTANY, and NOTEVERY, which iterate over sequences testing a boolean predicate. The first argument to all these functions is the predicate, and the remaining arguments are sequences.
Sequence Mapping Function
MAP, like the sequence predicate functions, takes a n-argument function and n sequences. But instead of a boolean value, MAP returns a new sequence containing the result of applying the function to subsequent elements of the sequences. Like CONCATENATE and MERGE, MAP needs to be told what kind of sequence to create.
MAP-INTO is like MAP except instead of producing a new sequence of a given type, it places the results into a sequence passed as the first argument. This sequence can be the same as one of the sequences providing values for the function.REDUCE, which does another kind of mapping: it maps over a single sequence, applying a two-argument function first to the first two elements of the sequence and then to the value returned by the function and subsequent elements of the sequence.
Hash Table IterationCommon Lisp provides a couple ways to iterate over the entries in a hash table. The simplest of these is via the function MAPHASH. Analogous to the MAP function, MAPHASH takes a two-argument function and a hash table and invokes the function once for each key/value pair in the hash table.
No comments:
Post a Comment