This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-133@nospamsrfi.schemers.org
. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.
vector-unfold!
)vector-cumulate
from (vector-cumulate f vec knil)
to
(vector-cumulate f knil vec)
to match the
argument order of vector-fold
.)vector-partition
.)elt=?
argument be consistent with eq?
.)vector-map!
.
Clarified the second return value
of vector-partition
in the case that all elements satisfy pred?.)Post-finalization note: The finalized version of this
SRFI required the equality operator used by the
vector=
procedure to be consistent with
eq?
. However, =
is not consistent
with eq?
in a Scheme which provides NaN values and
IEEE semantics for them, as (eq? +nan.0 +nan.0)
may
return either #t
or #f
but (=
+nan.0 +nan.0)
must return #f.
This means
that the expected semantics of =
when used with
vector=
are violated. Therefore, the requirement
for consistency has been removed from this version, and the
sample implementation adjusted accordingly.
This SRFI proposes a comprehensive library of vector operations accompanied by a freely available and complete reference implementation. The reference implementation is unencumbered by copyright, and useable with no modifications on any Scheme system that is R5RS-compliant. It also provides several hooks for implementation-specific optimization as well.
R5RS provides very few list-processing procedures, for which reason SRFI 1 exists. However, R5RS provides even fewer vector operations — while it provides mapping, appending, et cetera operations for lists, it specifies only nine vector manipulation operations:
vector?
make-vector
vector
vector-length
vector-ref
vector-set!
vector->list
list->vector
vector-fill!
R7RS-small added support for
start and end arguments to vector->list
,
list->vector
and vector-fill!
. It also
provided seven additional vector procedures, bringing vectors
and lists to approximate parity:
SRFI 43 standardized more vector procedures,
all of which are included in this SRFI. Unfortunately, R7RS-small
and SRFI 43 placed irreconcileable requirements on the procedures
invoked by vector-map
and vector-for-each
.
This SRFI resolves that issue by changing these SRFI 43
procedures as well as vector-map!
, vector-fold
,
vector-fold-right
, and vector-count
to leave
out the index argument that is passed under SRFI 43's
definition.
In addition, the version of vector-copy
in this SRFI
does not require support for a fill argument, which
makes it equivalent to the R7RS-small definition.
This SRFI also provides the following new procedures (some from Python, some from other sources):
It should be noted that no vector sorting procedures are provided by this SRFI, because there already are several SRFIs for that purpose.
Here is an index of the procedures provided by this package. Those marked by italics are also provided in R7RS-small.
make-vector
vector
vector-unfold
vector-unfold-right
vector-copy
vector-reverse-copy
vector-append
vector-concatenate
vector-append-subvectors
vector?
vector-empty?
vector=
vector-ref
vector-length
vector-fold
vector-fold-right
vector-map
vector-map!
vector-for-each
vector-count
vector-cumulate
vector-index
vector-index-right
vector-skip
vector-skip-right
vector-binary-search
vector-any
vector-every
vector-partition
vector-set!
vector-swap!
vector-fill!
vector-reverse!
vector-copy!
vector-reverse-copy!
vector-unfold!
vector-unfold-right!
vector->list
reverse-vector->list
list->vector
reverse-list->vector
vector->string
string->vector
In this section containing specifications of procedures, the following notation is used to specify parameters and return values:
f
takes the parameters
arg1 arg2
...
and returns a value of the
type something
. If something
is unspecified
, then f
returns a single
implementation-dependent value; this SRFI does not specify what it
returns, and in order to write portable code, the return value
should be ignored.
vector?
.
exact?
, integer?
and either
zero?
or positive?
. The third case of it
indicates the index at which traversal begins; the fourth case
of it indicates the size of a vector.
exact?
, integer?
and
either zero?
or positive?
. This indicates the index directly before
which traversal will stop — processing will occur until
the the index of the vector is end
. It is the
closed right side of a range.
something
is an optional
argument; it needn't necessarily be applied.
Something
needn't necessarily be one thing; for
example, this usage of it is perfectly valid:
[start [end]]
something
s are
allowed to be arguments.
something
must be
arguments.
n
will be used later in the
procedure description.
It should be noted that all of the procedures that iterate across
multiple vectors in parallel stop iterating and produce the final
result when the end of the shortest vector is reached. The sole
exception is vector=
, which
automatically returns #f
if the vectors' lengths vary.
size
.
If fill is specified, all the elements of the vector
are initialized to fill. Otherwise, their contents
are indeterminate.
(make-vector 5 3)
#(3 3 3 3 3)
x ...
.
(vector 0 1 2 3 4)
#(0 1 2 3 4)
length
and iterates across each index
k
between 0
and
length
, applying f
at each
iteration to the current index and current seeds, in that
order, to receive n + 1
values: first, the
element to put in the k
th slot of the new
vector and n
new seeds for the next iteration.
It is an error for the number of seeds to vary between
iterations. Note that the termination condition is different from
the unfold
procedure of SRFI 1.
(vector-unfold (λ (i x) (values x (- x 1)))
10 0)
#(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
n
).
(vector-unfold values n
)
#(0 1 2 ... n-2 n-1)
vector
.
(vector-unfold (λ (i) (vector-ref vector i))
(vector-length vector))
vector-unfold
, but
it uses f
to generate elements from
right-to-left, rather than left-to-right.
The first index used is length - 1.
Note that the termination condition is different from
the unfold-right
procedure of SRFI 1.
(vector-unfold-right (λ (i x) (values (cons i x) (+ x 1))) 5 0)
#((0 . 4) (1 . 3) (2 . 2) (3 . 1) (4 . 0))
vector
.
(vector-unfold-right (λ (i x)
(values (vector-ref vector x)
(+ x 1)))
(vector-length vector)
0)
end -
start
and fills it with elements from
vec
, taking elements from vec
starting at index start
and stopping at index
end
. Start
defaults to
0
and end
defaults to the value of
(vector-length
vec)
.
SRFI 43 provides an optional fill argument to supply
values if end is greater than the length of vec.
Neither R7RS-small nor this SRFI requires support for this argument.
(vector-copy '#(a b c d e f g h i))
#(a b c d e f g h i)
(vector-copy '#(a b c d e f g h i) 6)
#(g h i)
(vector-copy '#(a b c d e f g h i) 3 6)
#(d e f)
vector-copy
, but it
copies the elements in the reverse order from
vec
.
(vector-reverse-copy '#(5 4 3 2 1 0) 1 5)
#(1 2 3 4)
vec
...
.
(vector-append '#(x) '#(y))
#(x y)
(vector-append '#(a) '#(b c d))
#(a b c d)
(vector-append '#(a #(b)) '#(#(c)))
#(a #(b) #(c))
list-of-vectors
. This
is equivalent to:
(apply vector-append
list-of-vectors)
(vector-concatenate '(#(a b) #(c d)))
#(a b c d)
vector-append
.
(vector-append-subvectors '#(a b c d e) 0 2 '#(f g h i j) 2 4)
#(a b h i)
#t
if x
is a
vector, and #f
if otherwise.
(vector? '#(a b c))
#t
(vector? '(a b c))
#f
(vector? #t)
#f
(vector? '#())
#t
(vector? '())
#f
#t
if vec
is empty, i.e. its
length is 0
, and #f
if not.
(vector-empty? '#(a))
#f
(vector-empty? '#(()))
#f
(vector-empty? '#(#()))
#f
(vector-empty? '#())
#t
a
and
b
are considered equal by vector=
iff
their lengths are the same, and for each respective element
Ea
and
Eb
, (elt=? Ea
Eb)
returns a true value.
Elt=?
is always applied to two arguments.
#t
is
automatically returned. The dynamic order in which comparisons
of elements and of vectors are performed is left completely
unspecified; do not rely on a particular order.
(vector= eq? '#(a b c d) '#(a b c d))
#t
(vector= eq? '#(a b c d) '#(a b d c))
#f
(vector= = '#(1 2 3 4 5) '#(1 2 3 4))
#f
(vector= = '#(1 2 3 4) '#(1 2 3 4))
#t
(vector= eq?)
#t
(vector= eq? '#(a))
#t
eq?
.
(vector= eq? (vector (vector 'a)) (vector (vector 'a)))
#f
(vector= equal? (vector (vector 'a)) (vector (vector 'a)))
#t
vec
at
i
is mapped to in the store. Indexing is based
on zero. I
must be within the range [0,
(vector-length
vec)
).
(vector-ref '#(a b c d) 2)
c
vec
, the number of
locations reachable from vec
. (The careful
word 'reachable' is used to allow for 'vector slices,' whereby
vec
refers to a larger vector that contains
more locations that are unreachable from vec
.
This SRFI does not define vector slices, but later SRFIs may.)
(vector-length '#(a b c))
3
Kons
is
iterated over each value in all of the vectors, stopping at the
end of the shortest; kons
is applied as
(kons state
(vector-ref
vec1 i)
(vector-ref
vec2 i)
...)
where state
is the current state value —
the current state value begins with knil
, and
becomes whatever kons
returned on the
previous iteration —, and i
is the
current index.
vector-of-strings
.
(vector-fold (λ (len str)
(max (string-length str) len))
0 vector-of-strings)
vec
.
(vector-fold (λ (tail elt) (cons elt tail))
'() vec)
vec
.
(vector-fold (λ (counter n)
(if (even? n) (+ counter 1) counter))
0 vec)
vector-fold
, but
it iterates right to left instead of left to right.
(vector-fold-right (λ (tail elt)
(cons elt tail))
'() '#(a b c d))
(a b c d)
i
of the new
vector is mapped from the old vectors by
(f (vector-ref
vec1
i)
(vector-ref
vec2
i)
...)
.
The dynamic order of application of f
is
unspecified.
(vector-map (λ (x) (* x x))
(vector-unfold
(λ (i x) (values x (+ x 1)))
4 1))
#(1 4 9 16)
(vector-map (λ (x y) (* x y))
(vector-unfold
(λ (x) (values x (+ x 1)))
5 1)
(vector-unfold
(λ (x) (values x (- x 1)))
5 5))
#(5 8 9 8 5)
(let ((count 0))
(vector-map (λ (ignored-elt)
(set! count (+ count 1))
count)
'#(a b)))
#(1 2) OR #(2 1)
vector-map
, but
rather than mapping the new elements into a new vector, the new
mapped elements are destructively inserted into
vec1
. Again, the dynamic order of
application of f
unspecified, so it is
dangerous for f
to apply either
vector-ref
or
vector-set!
to
vec1
in f
. It is
an error if vec1
contains more
elements than any other vec
.
f
to
the corresponding list of parallel elements
from vec1 vec2
...
in the range [0, length
), where
length
is the length of the smallest vector
argument passed,
In contrast with
vector-map
, f
is reliably applied to each subsequent element, starting at
index 0, in the vectors.
(vector-for-each (λ (x) (display x) (newline))
'#("foo" "bar" "baz" "quux" "zot"))
foo bar baz quux zot
pred?
, which is applied, for each index
i
in the range [0, length
)
where length
is the length of the
smallest vector argument, to each
parallel element in the vectors, in order.
(vector-count even?
'#(3 1 4 1 5 9 2 5 6))
3
(vector-count <
'#(1 3 6 9) '#(2 4 6 8 10 12))
2
new
with the same length as vec
.
Each element i of new is set to the result of invoking
f
on new
i-1
and vec
i,
except that for the first call on f, the first argument is
knil. The new vector is returned.
Note that the order of arguments to vector-cumulate
was changed by errata-3
on 2016-09-02.
(vector-cumulate + 0 '#(3 1 4 1 5 9 2 5 6))
#(3 4 8 9 14 23 25 30 36)
vec1 vec2
...
that satisfy
pred?
. If no matching element is found by the
end of the shortest vector, #f
is returned.
(vector-index even? '#(3 1 4 1 5 9))
2
(vector-index < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
1
(vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))
#f
vector-index
, but it
searches right-to-left, rather than left-to-right, and all of
the vectors must have the same length.
vec1 vec2
...
that do not satisfy
pred?
. If all the values in the vectors
satisfy pred?
until the end of the shortest
vector, this returns #f
. This is equivalent to:
(vector-index
(λ (x1 x2
...)
(not (pred? x1
x1
...)))
vec1 vec2
...)
(vector-skip number? '#(1 2 a b 3 4 c d))
2
vector-skip
, but it
searches for a non-matching element right-to-left, rather than
left-to-right, and it is an error if all of the vectors do not
have the same length. This is equivalent to:
(vector-index-right
(λ (x1 x2
...)
(not (pred? x1
x1
...)))
vec1 vec2
...)
Similar to vector-index
and
vector-index-right
,
but instead of searching left to right or right to left, this
performs a binary search.
If there is more than one element of vec that matches
value in the sense of cmp,
vector-binary-search
may return the index of any of them.
cmp
should be a
procedure of two arguments and return a negative integer, which
indicates that its first argument is less than its second,
zero, which indicates that they are equal, or a positive
integer, which indicates that the first argument is greater
than the second argument. An example cmp
might
be:
(λ (char1 char2)
(cond ((char<? char1
char2)
-1)
((char=? char1
char2)
0)
(else 1)))
vec1 vec2
...
for which
pred?
returns a true value. If such a parallel
set of elements exists, vector-any
returns the value
that pred?
returned for that set of elements.
The iteration is strictly left-to-right.
i
between 0 and the length
of the shortest vector argument, the set of elements
(vector-ref vec1
i)
(vector-ref vec2
i)
...
satisfies pred?
, vector-every
returns
the value that pred?
returned for the last
set of elements, at the last index of the shortest vector. The
iteration is strictly left-to-right.
vec
is newly allocated
and filled with all the elements of vec
that
satisfy pred?
in their original order followed
by all the elements that do not satisfy pred?
,
also in their original order.
pred?
.
i
in
vec
to value
.
vec
at i
&
j
.
vec
between start
, which defaults to 0
and
end
, which defaults to the length of
vec
, to fill
.
vec
between start
and end
. Start
defaults to
0
and end
defaults to the length of
vec
. Note that this does not deeply reverse.
from
between start
and end
to vector to
, starting at at
.
The order in which elements
are copied is unspecified, except that if the source and destination
overlap, copying takes place as if the source is first
copied into a temporary vector and then into the destination.
This can be achieved without allocating storage by
making sure to copy in the correct direction in such circumstances.
vector-copy!
, but
the elements appear in to
in reverse order.
vector-unfold
, but
the elements are copied into the vector vec starting
at element start rather than into a newly allocated vector.
Terminates when end-start elements have been generated.
vector-unfold!
, but the
elements are copied in reverse order into the vector vec
starting at the index preceding end.
vec
between start
, which defaults to 0
,
and end
, which defaults to the length of
vec
.
vector->list
,
but the resulting list contains the elements in reverse of
vec
.
proper-list
.
list->vector
,
but the resulting vector contains the elements in reverse of
proper-list
.
string
between start
, which defaults to 0
,
and end
, which defaults to the length of
string
.
vec
between start
, which defaults to 0
,
and end
, which defaults to the length of
vec
. It is an error if the elements are not characters.
The sample implementation is in the repository of this SRFI.
It has only one non-R5RS dependency:
SRFI 23's error
procedure,
which is also provided by R7RS-small.
It is in the public domain, or alternatively under the same copyright
as this SRFI. The following files are provided:
vectors-impl.scm
-
a modified version of the implementation of SRFI 43vectors.scm
-
a Chicken library showing what to export for an R5RS implementationvectors.sld
-
an R7RS library that excludes what R7RS-small already providesvectors-test.scm
-
tests using the Chicken test egg (also available on Chibi)These acknowledgements are copied from SRFI 43.
Thanks to Olin Shivers for his wonderfully complete
list and string
packages; to all the members of the
#scheme
IRC
channel on Freenode
who nitpicked a great deal, but also helped quite a lot in
general, and helped test the reference implementation in various
Scheme systems; to Michael Burschik for his numerous comments; to
Sergei Egorov for helping to narrow down the procedures; to Mike
Sperber for putting up with an extremely overdue draft; to
Felix Winkelmann for continually bugging me about finishing up the
SRFI so that it would be only overdue and not withdrawn; and to
everyone else who gave questions, comments, thoughts, or merely
attention to the SRFI.
error
) for
reporting that an error occurred, written by Stephan Houben.
Copyright (C) Taylor Campbell (2003). All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.