12 The Built-In Functions
Functions such as map
, map-as
that return a new collection cannot rely on the type they instantiate having a valid default for fill:
. Therefore when the size of the result is non-zero these functions should compute the first element of the result before making the collection, and specify that element as the fill:
value. Otherwise a spurious type error could occur when making the collection.
empty?
[Open Generic Function]
empty? object
Þ boolean
<object>
.
<boolean>
.
<collection>
return true if the collection has zero elements.size
[Open Generic Function]
size object
Þ #rest objects
<object>
.
<object>
.
size
returns the numbers of keys in the collection. This default method simply counts while iterating through the collection. size
may return #f
for collections of unbounded size.
reduce(\*, 1, dimensions (array))
<table>
provides an implementation of size
for use by its subclasses. The method returns an instance of <integer>
.size-setter
[Open Generic Function]
size-setter new-size object Þ new-size
<object>
.
<object>
.
<object>
.
<stretchy-collection>
and of <sequence>
.
size-setter
sets the size of stretchy-sequence to be integer. stretchy-sequence is modified by this operation. If integer is less than or equal to the original size of stretchy-sequence, then the first integer elements of stretchy-sequence are retained at the same positions. If integer is greater than the original size of stretchy-sequence, then the previous elements of the stretchy-sequence are retained at the same positions, and enough new elements are added to reach the new size. The value of each new element is the same as would have been used if stretchy-sequence had been created with make, specifying size:
integer but not fill:
.
It is not specified how size-setter
adds new elements to a stretchy-sequence. In particular, it is not required to call add!
or any other predefined function.
rank
[Open Generic Function]
rank array
Þ rank
<array>
.
<integer>
.
row-major-index
[Open Generic Function]
row-major-index array
#rest subscripts Þ index
<array>
.
<integer>
.
<integer>
.
An error is signaled if the number of subscripts is not equal to the rank of the array. An error is signaled if any of the subscripts are out of bounds for array.
dimensions
[Open Generic Function]
dimensions array
Þ sequence
An instance of <array>
.
An instance of <sequence>
. The elements of this sequences will be instances of <integer>
.
dimension
[Open Generic Function]
dimension array axis
Þ dimension
<array>
.
<integer>
.
An instance of <integer>
.
axis must be a non-negative integer less than the rank of array. An error is signaled if axis is out of bounds for array.
key-test
[Open Generic Function]
key-test collection
Þ test-function
An instance of <collection>
.
An instance of <function>.
The function used by the collection to compare keys.
All collection classes must provide or inherit a method that returns a result consistent with their iteration protocol and element methods. A given method for key-test must return the same value (compared with ==) each time it is called.
<table>
returns the first value of table-protocol(table)
.key-sequence
[Open Generic Function]
key-sequence collection
Þ keys
An instance of <collection>
.
An instance of <sequence>
containing the keys of collection.
Although elements may be duplicated in a collection, keys, by their nature, must be unique; two different elements in a collection may not share a common key, even though distinct keys may yield identical elements.
The order in which the keys from collection appear in the key sequence is unspecified if collection is unstable under iteration. In particular, different calls to key-sequence with the same argument may yield differently ordered key sequences. If collection is stable under iteration, however, the resulting sequence of keys will be in the natural order for collection.
element
[Open Generic Function]
element collection key #key default
Þ element
An instance of <collection>
.
<object>
.
<object>
.
An instance of <object>
.
All collections are required to implement element.
element
for all general instances of <simple-vector>
.element-setter
[Open Generic Function]
element-setter new-value mutable-collection key
Þ new-value
An instance of <object>
.
<mutable-collection>
.
<object>
.
Zero or more instances of <object>
.
element-setter
may also change its size (for example, by adding new keys with values).An error is signaled if a program calls element-setter with a key that is not already a key to collection, unless the collection is stretchy.
Stretchy collections allow element-setter to be called with a key that is not present in the collection, expanding the collection as necessary to add a new element in that case. Each concrete subclass of <stretchy-collection> must provide or inherit a method for element-setter that behaves as follows when there is not already an element present for the indicated key:
element-setter
for all general instances of <simple-vector>
.<table>
provides an implementation of element-setter
for use by its subclasses. If no element with the given key exists, element-setter will add the key and new-value to the table.aref
[Open Generic Function]
aref array
#rest
indices Þ element
An instance of <array>
.
<integer>
.
An instance of <object>
.
An error is signaled if the number of indices is not equal to the rank of the array. An error is signaled if any of the indices are out of bounds for the array.
aref-setter
[Open Generic Function]
aref-setter new-value array #rest indices
Þ new-value
An instance of <object>
.
<array>
.
<integer>
.
An instance of <object>
.
An error is signaled if the number of indices is not equal to the rank of the array. An error is signaled if any of the indices are out of bounds for array. An error is signaled if the array is limited to hold objects of a particular type and the new value is not an instance of that type.
first
[Function]
first sequence #key default
Þ value
An instance of <sequence>
.
<object>
.
An instance of <object>
.
Note that because element is zero-based, first(seq) is equivalent to element(seq, 0) and seq[0].
second
[Function]
second sequence #key default
Þ value
An instance of <sequence>
.
<object>
.
An instance of <object>
.
third
[Function]
third sequence #key default
Þ value
An instance of <sequence>
.
<object>
.
An instance of <object>
.
first-setter
[Function]
first-setter new-value mutable-sequence
Þ new-value
<object>
.
<mutable-sequence>
.
<object>
.
Note that because element-setter
is zero-based, first-setter(val, seq) is equivalent to element-setter(val, seq, 0) and seq[0] := val.
second-setter
[Function]
second-setter new-value mutable-sequence
Þ new-value
<object>
.
<mutable-sequence>
.
<object>
.
third-setter
[Function]
third-setter new-value mutable-sequence
Þ new-value
<object>
.
<mutable-sequence>
.
<object>
.
last
[Open Generic Function]
last sequence #key default
Þ value
<sequence>
.
<object>
.
<object>
.
If the sequence is empty, then the behavior of last depends on whether it was called with a default argument. If the default argument was supplied, its value is returned; otherwise, an error is signaled.
last (#("emperor", "of", "china"))
Þ "china"
last-setter
[Open Generic Function]
last-setter new-value mutable-sequence
Þ new-value
<object>
.
<mutable-sequence>
.
<object>
.
new-value must obey any type restrictions for elements of mutable-sequence . An error is signaled if mutable-sequence is empty or unbounded.
define variable my-list = list (1, 2, 3) my-listÞ #(1, 2, 3) last (my-list) := 4
Þ 4 my-list
Þ #(1, 2, 4) define variable my-empty-vector = vector() my-empty-vector
Þ #[] last (my-empty-vector) := 4 {error}
head
[Function]
head list
Þ object
<list>
.
<object>
.
If list is a pair, head
returns the value of the head slot. If list is the empty list, head returns the empty list.
head (#(4, 5, 6))Þ 4 head (#())
Þ #()
tail
[Function]
tail list
Þ object
<list>
.
<object>
.
If list is a pair, tail
returns the value of the tail slot. If list is the empty list, tail returns the empty list.
tail (#(4, 5, 6))Þ #(5, 6) tail (#())
Þ #()
head-setter
[Function]
head-setter object pair
Þ object
<object>
.
<pair>
.
<object>
.
Example
define variable x = list (4, 5, 6) head (x) := 9Þ 9 x
Þ #(9, 5, 6)
tail-setter
[Function]
tail-setter object pair
Þ object
<object>
.
<pair>
.
<object>
.
define variable x = list (4, 5, 6) tail (x) := #(9, 8, 7)Þ #(9, 8, 7) x
Þ #(4, 9, 8, 7) tail (x) := "dot"
Þ "dot" x
Þ #(4, 9, 8 . "dot")
add
[Open Generic Function]
add source-sequence new-element
Þ result-sequence
<sequence>
.
<object>
.
<sequence>
.
The result-sequence's size is one greater than the size of source-sequence. The generic function add doesn't specify where the new element will be added, although individual methods may do so.
define variable *numbers* = #(3, 4, 5) add (*numbers*, 1) Þ #(1, 3, 4, 5) *numbers* Þ #(3, 4, 5)
add!
[Open Generic Function]
add! source-sequence new-element
Þ result-sequence
<sequence>
.
<object>
.
<sequence>
.
==
.source-sequence may be modified by this operation.
result-sequence's size is one greater than the size of source-sequence. The generic function add! doesn't specify where the new element will be added, although individual methods may do so.
define variable *numbers* = list (3, 4, 5) add! (*numbers*, 1) Þ #(1, 3, 4, 5) *numbers* Þ {undefined}
add!
on a stretchy vector is == to the stretchy-vector argument, and the argument is modified by this operation. add! adds new-element at the end of the stretchy-vector.add!
on a list is equivalent to (pair element list)
. The result will share structure with the list argument, but it will not be ==
to the argument, and the argument will not be modified.add-new
[Open Generic Function]
add-new source-sequence new-element #key test
Þ result-sequence
<sequence>
.
<object>
.
<function>
. The default is ==
.
<sequence>
.
If an element is added, add-new operates just as add would.
The test function may be non-commutative: it is always called with an element from source-sequence as its first argument and new-element as its second argument.
add-new (#(3, 4, 5), 1) Þ #(1, 3, 4, 5) add-new (#(3, 4, 5), 4) Þ #(3, 4, 5)
add-new!
[Open Generic Function]
add-new! source-sequence new-element #key test
Þ result-sequence
<sequence>
.
<object>
.
<function>
. The default is ==
.
<sequence>
.
If an element is added, add-new! operates just as add! would.
The test function may be non-commutative: it is always called with an element from sequence as its first argument and new-element as its second argument.
add-new! (list (3, 4, 5), 1) Þ #(1, 3, 4, 5) add-new! (list (3, 4, 5), 4) Þ #(3, 4, 5)
remove
[Open Generic Function]
remove source-sequence value #key test count
Þ result-sequence
<sequence>
.
<object>
.
<function>
. The default is ==
.
<integer>
or #f
. The default is #f
.
<sequence>
.
remove
.test is a function which determines whether an element is equal to value. The test function may be non-commutative: it is always called with an element from source-sequence as its first argument and value as its second argument.
If count is #f
, then all copies of value are removed. Otherwise, no more than count copies of value are removed (so additional elements equal to value might remain in result-sequence).
define variable *old-list* = list(1, 2, 3) define variable *new-list* = remove(*old-list* 1) *new-list* Þ #(2, 3) *new-list* == tail(*old-list*) Þ {undefined}
remove!
[Open Generic Function]
remove! source-sequence value #key test count
Þ result-sequence
<sequence>
.
<object>
.
<function>
. The default is ==
.
<integer>
or #f
. The default is #f
.
<sequence>
.
==
to the source-sequence, and may or may not share structure with the source-sequence. The source-sequence may be modified by remove!
.test is a function which determines whether an element is equal to value. The test function may be non-commutative: it is always called with an element from source-sequence as its first argument and value as its second argument.
If count is #f
, then all copies of value are removed. Otherwise, no more than count copies of value are removed (so additional elements equal to value might remain in result-sequence).
push
[Open Generic Function]
push deque new-value
Þ new-value
<deque>
.
<object>
.
<object>
. The same object that was passed in as an argument.
pop
[Open Generic Function]
pop deque
Þ first-element
<deque>
.
<object>
.
push-last
[Open Generic Function]
push-last deque new-value
Þ new-value
<deque>
.
<object>
.
<object>
. The same object that was passed in as an argument.
pop-last
[Open Generic Function]
pop-last deque
Þ last-element
<deque>
.
<object>
.
reverse
[Open Generic Function]
reverse source-sequence
Þ result-sequence
<sequence>
.
<sequence>
.
The result-sequence may or may not be freshly allocated. The source-sequence is not modified by this operation.
The consequences are undefined if the source-sequence is unbounded (circular or infinite).
define variable *x* = list("bim", "bam", "boom") *x* Þ #("bim", "bam", "boom") reverse(*x*) Þ #("boom", "bam", "bim") *x* Þ #("bim", "bam", "boom")
reverse!
[Open Generic Function]
reverse! source-sequence
Þ result-sequence
<sequence>
.
<sequence>
.
The source-sequence may be modified by this operation. The result-sequence may or may not be freshly allocated. The source-sequence and the result-sequence may or may not be ==
. Programs should never rely on this operation performing a side-effect on an existing sequence, but should instead use the value returned by the function.
The consequences are undefined if the source-sequence is unbounded (circular or infinite).
define variable *x* = list("bim", "bam", "boom")
*x*
Þ #("bim", "bam", "boom")
reverse!(*x*)
Þ #("boom", "bam", "bim")
*x*
Þ {undefined}
sort
[Open Generic Function]
sort source-sequence #key test stable
Þ result-sequence
<sequence>
.
<function>
. The default is <
.
<object>
, treated as a boolean.
<sequence>
.
sort
determines the relationship between two elements by giving elements to the test. The first argument to the test function is one element of source-sequence; the second argument is another element of source-sequence. test should return true if and only if the first argument is strictly less than the second (in some appropriate sense). If the first argument is greater than or equal to the second (in the appropriate sense), then the test should return #f
.
If stable is supplied and not #f
, a possibly slower algorithm will be used that will leave in their original order any two elements, x and y, such that test(x, y) and test(y, x) are both false.
define variable *numbers* = vector(3, 1, 4, 1, 5, 9) *numbers* Þ #[3, 1, 4, 1, 5, 9] sort (*numbers*) Þ #[1, 1, 3, 4, 5, 9] *numbers* Þ #[3, 1, 4, 1, 5, 9]
sort!
[Open Generic Function]
sort! source-sequence #key test stable
Þ result-sequence
<sequence>
.
<function>
. The default is <
.
<object>
, treated as a boolean.
<sequence>
.
==
to source-sequence. After this operation, the contents of source-sequence are undefined.Programs should never rely on this operation performing a side-effect on an existing sequence, but should instead use the value returned by the function.
sort!
determines the relationship between two elements by giving elements to the test. The first argument to the test function is one element of source-sequence; the second argument is another element of source-sequence. test should return true if and only if the first argument is strictly less than the second (in some appropriate sense). If the first argument is greater than or equal to the second (in the appropriate sense), then the test should return #f
.
If stable is supplied and not #f
, a possibly slower algorithm will be used that will leave in their original order any two elements, x and y, such that test(x, y) and test(y, x) are both false.
define variable *numbers* = vector(3, 1, 4, 1, 5, 9)
*numbers*
Þ #[3, 1, 4, 1, 5, 9]
sort! (*numbers*)
Þ #[1, 1, 3, 4, 5, 9]
*numbers*
Þ {undefined}
intersection
[Open Generic Function]
intersection sequence1 sequence2
#key test Þ new-sequence
<sequence>
.
<sequence>
.
<function>
. The default is ==
.
<sequence>
.
test is used to determine whether an element appears in sequence2. It is always called with an element of sequence1 as its first argument and an element from sequence2 as its second argument. The order of elements in the result sequence is not specified.
new-sequence may or may not share structure with the sequence1 and sequence2.
? intersection (#("john", "paul", "george", "ringo"), #("richard", "george", "edward", "charles"), test: \=) #("george")
union
[Open Generic Function]
union sequence1 sequence2
#key test Þ new-sequence
<sequence>
.
<sequence>
.
<function>
. The default is ==
.
<sequence>
.
If the same element appears in both argument sequences, this will not cause it to appear twice in the result sequence. However, if the same element appears more than once in a single argument sequence, it may appear more than once in the result sequence.
test is used for all comparisons. It is always called with an element from sequence1 as its first argument and an element from sequence2 as its second argument. The order of elements in the new-sequence is not specified.
new-sequence may or may not share structure with sequence1 or sequence2.
union (#("butter", "flour", "sugar", "salt", "eggs"),
#("eggs", "butter", "mushrooms", "onions", "salt"),
test: \=)
Þ #("salt", "butter", "flour", "sugar", "eggs",
"mushrooms", "onions")
remove-duplicates
[Open Generic Function]
remove-duplicates source-sequence
#key test Þ result-sequence
<sequence>
.
<function>
. The default is ==
.
<sequence>
.
test is the function used to determine whether one element is a duplicate of another. The test argument may be non-commutative; it will always be called with its arguments in the same order as they appear in source-sequence.
The result-sequence may or may not be freshly allocated. However, the source-sequence will not be modified by this operation.
remove-duplicates (#("spam", "eggs", "spam", "sausage", "spam", "spam"), test: \=)Þ #("spam", "eggs", "sausage") or
Þ #("eggs", "spam", "sausage") or
Þ #("eggs", "sausage", "spam")
remove-duplicates!
[Open Generic Function]
remove-duplicates! source-sequence
#key test Þ result-sequence
<sequence>
.
<function>
. The default is ==
.
<sequence>
.
test is the function used to determine whether one element is a duplicate of another. The test argument may be non-commutative; it will always be called with its arguments in the same order as they appear in source-sequence.
The result-sequence may or may not be freshly allocated, may or may not share structure with the source-sequence, and may or may not be ==
to the source-sequence. The source-sequence may or may not be modified by the operation.
define variable *menu* = #("spam", "eggs", "spam", "sausage", "spam", "spam") remove-duplicates! (*menu*, test: \=) Þ #("spam", "eggs", "sausage") orÞ #("eggs", "spam", "sausage") or
Þ #("eggs", "sausage", "spam") *menu* Þ {undefined}
copy-sequence
[Open Generic Function]
copy-sequence source
#key start end Þ new-sequence
<sequence>
.
<integer>
. The default is 0
.
<integer>
. The default is the size of source.
<sequence>
.
define constant hamlet = #("to", "be", "or", "not", "to", "be") hamlet == copy-sequence (hamlet)Þ #f copy-sequence (hamlet, start: 2, end: 4)
Þ #("or", "not")
concatenate
[Function]type-for-copy
of its first argument.
concatenate first-sequence
#rest more-sequences Þ result-sequence
<sequence>
.
<sequence>
.
<sequence>
.
The result-sequence will be an instance of the type-for-copy
value for first-sequence. It may or may not be freshly allocated. The result-sequence may be created by calling make
on the indicated type, with a size: initialization argument whose value is the sum of the sizes of the argument sequences. (For this reason, the type-for-copy
value of first-sequence must support the size:
init-keyword.)
new-sequence may share structure with any of the argument sequences, but it is not guaranteed to do so. The argument sequences will not be modified by this operation.
concatenate ("low-", "calorie")
Þ "low-calorie"
concatenate-as
[Function]
concatenate-as type first-sequence
#rest more-sequences Þ result-sequence
<type>
, which must be a subtype of <mutable-sequence>
<sequence>
.
<sequence>
.
<sequence>
.
The result-sequence will be an instance of type. It may or may not be freshly allocated.
type must be a subtype of <mutable-sequence> and acceptable as the first argument to make. size: with a non-negative integer value must be an acceptable initarg for make
of type. The result-sequence may be created by calling make on type, with a size: initialization argument whose value is the sum of the sizes of the arguments.
Example
concatenate-as (<string>, #('n', 'o', 'n'), #('f', 'a', 't'))
Þ "nonfat"
replace-subsequence!
[Open Generic Function]
replace-subsequence! source-sequence insert-sequence #key start end
Þ result-sequence
<sequence>
.
<sequence>
.
<integer>
. The default is 0
.
<integer>
. The default is the size of sequence.
<sequence>
.
replace-subsequence!
returns a sequence with the same elements as source-sequence, except that elements of the indicated subsequence are replaced by all the elements of insert-sequence. The subsequence to be overridden begins at index start and ends at index end.result-sequence may or may not share structure with source-sequence or insert-sequence, and it may or may not be == to source-sequence or insert-sequence. source-sequence may or may not be modified by the operation. insert-sequence will not modified by this operation.
Example
define variable x = list ("a", "b", "c", "d", "e") abcde := replace-subsequence! (x, #("x", "y", "z"), end: 1))Þ #("x", "y", "z", "b", "c", "d", "e") abcde := replace-subsequence! (x, #("x", "y", "z"), start: 4))
Þ #("x", "y", "z", "b", "x", "y", "z") abcde := replace-subsequence! (x, #("a", "b", "c"), start: 2, end: 4))
Þ #("x", "y", "a", "b", "c", "x", "y", "z")
subsequence-position
[Open Generic Function]
subsequence-position big pattern
#key test count Þ index
<sequence>
.
<sequence>
.
<function>
. The default is ==
.
<integer>
. The default is 1
.
#f
or an instance of <integer>
.
test is applied to elements of successive subsequences of big and corresponding elements of the pattern to determine whether a match has occurred. If a subsequence is found, subsequence-position
returns the index at which the subsequence starts; otherwise, it returns #f. If there is more than one match, count determines which subsequence is selected. A count of 1 (the default) indicates that the first match should be returned.
subsequence-position ("Ralph Waldo Emerson", "Waldo")
Þ 6
do
, map
, map-as
, map-into
, any?
, every?
) iterate over a number of source collections. Each time through the iteration, a function is applied to one element from each of the source collections. The number of arguments to the function is equal to the number of source collections.The functions vary in how they handle the results of each function application.
do
[Function]
do function collection #rest more-collections
Þ false
<function>
.
<collection>
.
<collection>
.
#f
.
do (method (a b) print (a + b) end,
#(100, 100, 200, 200),
#(1, 2, 3, 4))
101
102
203
204
Þ #f
map
[Function]
map function collection #rest more-collections
Þ new-collection
<function>
.
<collection>
.
<collection>
.
<collection>
.
map returns a collection whose value is an instance of the type-for-copy value of collection. The new collection is created by calling make
on that type, with a size:
initialization argument whose value is the number of corresponding elements in the collections.
map (\+,
#(100, 100, 200, 200),
#(1, 2, 3, 4))
Þ #(101, 102, 203, 204)
map-as
[Function]
map-as type function collection #rest more-collections
Þ new-collection
<type>
. It must be an instantiable subtype of <mutable-collection>.
<function>
.
<collection>
.
<collection>
.
<mutable-collection>
.
map-as (<vector>, \+,
#(100, 100, 200, 200),
#(1, 2, 3, 4))
Þ #(101, 102, 203, 204)
map-into
[Function]
map-into mutable-collection function collection #rest more-collections
<mutable-collection>
.
<function>
.
<collection>
.
<collection>
.
<mutable-collection>
.
If mutable-collection and all the other collections are sequences, processing is done in the natural order.
When mutable-collection is an instance of <stretchy-collection>, the usual alignment requirement (described in "Collection Alignment" on page 118) is relaxed. In this case, the key sequence of mutable-collection is not considered during alignment. Rather, only the key sequences for the source collections are aligned, with function called on the corresponding elements. The result of each call to function is then stored into mutable-collection with the corresponding key (possibly stretching mutable-collection in the process), using element-setter. Other keys in mutable-collection remain undisturbed.
mutable-collection may be the same object as collection or any of the more-collections.
An error is signalled if mutable-collection does not have the same key-test
function as the rest of the collections. This is true even if it is a <stretchy-collection> and therefore does not get aligned.
define variable x = list (10, 9, 8, 7) map-into (x, \+, #(1, 2, 3, 4), #(100, 100, 200, 200))Þ #(101, 102, 203, 204) x
Þ #(101, 102, 203, 204)
any?
[Function]
any? function collection
#rest
more-collections Þ value
<function>
.
<collection>
.
<collection>
.
<object>
.
any?
returns that true value. Otherwise function returns #f when applied to every such group, and any?
returns #f.
If all the collections are sequences, any?
operates in natural order. In all cases, any?
stops on the first true value returned by function.
any? (\>, #(1, 2, 3 ,4), #(5, 4, 3, 2))Þ #t any? (even?, #(1, 3, 5, 7))
Þ #f
every?
[Function]
every? function collection #rest more-collections
Þ value
<function>
.
<collection>
.
<collection>
.
<boolean>
.
every?
returns #f
. Otherwise function returns a true value when applied to every such group, and every?
returns #t.
If all the collections are sequences, every?
operates in natural order. In all cases, every?
stops on the first false value returned by function.
every? (\>, #(1, 2, 3, 4), #(5, 4, 3, 2))Þ #f every? (odd?, #(1, 3, 5, 7))
Þ #t
reduce
[Open Generic Function]
reduce function initial-value collection
Þ value
<function>
.
<object>
.
<collection>
.
<object>
.
If collection is empty, reduce returns initial-value; otherwise, function is applied to initial-value and the first element of collection to produce a new value. If more elements remain in the collection, then function is called again, this time with the value from the previous application and the next element from collection. This process continues until all elements of collection have been processed.
function is a binary function used to combine all the elements of collection into a single value. Processing is always done in the natural order for collection.
Example
define variable high-score = 10 reduce (max, high-score, #(3, 1, 4, 1, 5, 9))Þ 10 reduce (max, high-score, #(3, 12, 9, 8, 8, 6))
Þ 12
reduce1
[Open Generic Function]
reduce1 function collection
Þ value
<function>
.
<collection>
.
<object>
.
reduce1 is similar to reduce, except that the first element of collection is taken as the initial value, and all the remaining elements of collection are processed as if by reduce. (In other words, the first value isn't used twice.)
For unstable collections, "first" element effectively means "an element chosen at random." Processing is done in the natural order for collection.
reduce1 (\+, #(1, 2, 3, 4, 5))
Þ 15
choose
[Open Generic Function]
choose predicate source-sequence
Þ result-sequence
<function>
.
<sequence>
.
<sequence>
.
choose (even?, #(3, 1, 4, 1, 5, 8, 9)) Þ #(4, 8)
choose-by
[Open Generic Function]
choose-by predicate test-sequence value-sequence
Þ result-sequence
<function>
.
<sequence>
.
<sequence>
.
<sequence>
.
choose-by (even?, range (from: 1), #("a", "b", "c", "d", "e", "f", "g", "h", "i")) Þ #("b", "d", "f", "h")
member?
[Open Generic Function]
member? value collection
#key test Þ boolean
<object>
.
<collection>
.
<function>
. The default is ==
.
<boolean>
.
The test function may be non-commutative: it is always called with value as its first argument and an element from collection as its second argument.
define constant flavors = #(#"vanilla", #"pistachio", #"ginger") member? (#"vanilla", flavors)Þ
#t
member? (#"banana", flavors)Þ #f
==
.find-key
[Open Generic Function]
find-key collection function
#key skip failure Þ key
<collection>
.
<function>
.
<integer>
. The default is 0
.
<object>
. The default is #f
.
<object>
.
find-key
returns failure.The skip argument indicates that the first skip matching elements should be ignored. If skip or fewer elements of collection satisfy predicate, then failure is returned. If collection is not stable under iteration, then skip is only useful for finding out whether collection contains at least skip elements which satisfy predicate; it is not useful for finding a particular element.
flavorsÞ #(#"vanilla", #"pistachio", #"ginger") find-key (flavors, has-nuts?)
Þ 1 flavors[1]
Þ #"pistachio"
remove-key!
[Open Generic Function]
remove-key! collection key
Þ boolean
<mutable-explicit-key-collection>
.
<object>
.
<boolean>
.
The boolean return value will be #t
if the key was present and removed, or #f
if the key was not present and hence not removed.
<table>
.replace-elements!
[Open Generic Function]
replace-elements! mutable-collection predicate new-value-fn
#key count
<mutable-collection>
.
<function>
.
<function>
.
<integer>
or #f
. The default is #f
.
<mutable-collection>
.
#f
, all of the matching elements are replaced. Otherwise, no more than count elements are replaced.
define variable numbers = list (10, 13, 16, 19)
replace-elements! (numbers, odd?, double)
Þ #(10, 26, 16, 38)
fill!
[Open Generic Function]
fill! mutable-collection value
#key start end Þ mutable-collection
<collection>
.
<object>
.
<integer>
.
<integer>
.
<collection>
.
If mutable-collection is a sequence, then start and end keywords may be specified to indicate that only a part of the sequence should be filled. start is considered an inclusive bound and defaults to 0
; end is an exclusive bound and defaults to the length of the sequence.
define variable numbers = list (10, 13, 16, 19)
fill! (numbers, 3, start: 2)
Þ #(10, 13, 3, 3)
forward-iteration-protocol
[Open Generic Function]
forward-iteration-protocol
collection
<collection>
.
<object>
. The initial iteration state object.
<object>
that is used by the finished-state? function to determine whether the iteration has been completed.
<function>
. Its signature is
<function>
. Its signature is
<function>
. Its signature is
<function>
. Its signature is
<function>
. Its signature is
<function>
. Its signature is
Only the collection argument this function was called with may be used as the collection argument to functions returned by this function. Only the initial-state object and state objects returned by the next-state and copy-state functions may be used as the state argument to functions returned by this function. Only the limit object may be used as the limit argument to the finished-state? function.
An example of the use of the iteration protocol is the following definition of a single-argument version of the do function:
define method do1 (f :: <function>, c :: <collection>)
let (init, limit, next, end?, key, elt) =
forward-iteration-protocol(c);
for (state = init then next(c, state),
until end?(c, state, limit))
f(elt(c, state));
end for;
end method do1;
<table>
implements the iteration protocol in terms of the function table-protocol
.backward-iteration-protocol
[Open Generic Function]
backward-iteration-protocol
collection
<collection>
.
<object>
.
<object>
.
<function>
.
<function>
.
<function>
.
<function>
.
<function>
.
<function>
.
Some collection classes that are stable under iteration support the ability to iterate in the reverse of the natural order, by providing a method on the generic function backward-iteration-protocol. The eight values returned by this function are analogous to the corresponding values returned by forward-iteration-protocol.
table-protocol
[Open Generic Function]
table-protocol table
Þ test-function hash-function
<table>
.
<function>
. Its signature is
<function>
. Its signature is
<table>
. These functions are in turn used to implement the other collection operations on <table>
.
<object-table>
returns ==
as the test-function and object-hash as the hash-function.
The method for <object-table>
could be written as
define method table-protocol (table :: <object-table>) => test-function :: <function>, hash-function :: <function>; values(\==, object-hash); end method table-protocol;
merge-hash-codes
[Function]
merge-hash-codes
id1 state1 id2 state2 #key ordered
Þ merged-id merged-state
<integer>
.
<object>
.
<integer>
.
<object>
.
<boolean>
.
<integer>
.
<object>
.
id1, id2, and merged-id are all integers. state1, state2, and merged-state are all hash states. ordered is a boolean and determines whether the algorithm used to perform the merge is permitted to be order dependent. If false, which is the default, then the merged result must be independent of the order in which the argument pairs are provided. If true, then the order of the argument pairs matters because the algorithm used need not be either commutative or associative. Providing a true value for ordered is recommended when doing so will not cause the hash function to violate the second constraint on hash functions, because it may result in a better distribution of hash ids.
state1 and state2 should be the value of $permanant-hash-state
or hash-states returned from previous calls to merge-hash-codes
or object-hash
.
object-hash
[Function]
object-hash object
Þ hash-id hash-state
<object>
.
<integer>
.
<object>
.
==
. It is made available as a tool for writing hash functions in which the object identity of some component of a key is to be used in computing the hash code. It returns a hash id (an integer) and associated hash state for the object, computed in some implementation dependent manner. The values returned by object-hash when called repeatedly on the same object might not be the same for each call. If the hash-id value changes then the hash-state value will also change.
Generated with Harlequin WebMaker