Vector

The :std/misc/vector library provides common vector functions that complement those provided by RnRS, Gambit, :std/srfi/43 and :std/srfi/133.

To use the bindings from this module:

(import :std/misc/vector)

vector-map/index

This is the variant of vector-map from :std/srfi/43, that passes an index as well as a value to the function argument, unlike the variant in R7RS or in :std/srfi/133.

vector-for-each/index

This is the variant of vector-for-each from :std/srfi/43, that passes an index as well as a value to the function argument, unlike the variant in R7RS or in :std/srfi/133.

vector-map!/index

This is the variant of vector-map! from :std/srfi/43, that passes an index as well as a value to the function argument, unlike the variant in R7RS or in :std/srfi/133.

vector-fold/index

This is the variant of vector-fold from :std/srfi/43, that passes an index as well as a value to the function argument, unlike the variant in R7RS or in :std/srfi/133.

vector-fold-right/index

This is the variant of vector-fold-right from :std/srfi/43, that passes an index as well as a value to the function argument, unlike the variant in R7RS or in :std/srfi/133.

vector-count/index

This is the variant of vector-count from :std/srfi/43, that passes an index as well as a value to the function argument, unlike the variant in R7RS or in :std/srfi/133.

vector-ref-set!

(def vector-ref-set! vector-set!)
(set! (vector-ref v i) x)

This binding enables you to use set! with vector-ref.

Examples:

> (def foo (vector 1 2 3))
> (set! (vector-ref foo 1) 4)
> foo
#(1 4 3)

vector-least-index

(vector-least-index pred vector [start: 0] [end: #f])

Given a predicate pred on the elements of given vector, that is “increasing”, i.e. if true for a given element, true on all subsequent elements, and optionally a start (inclusive, defaults to 0) and an end (exclusive, defaults to #f which designates the vector length), return the least index of a vector element in the interval [start, env) that satisfies the predicate, or the end if none does.

Examples:

> (vector-least-index (cut < <> 10) #(35 21 16 11 10 9 7 4 1))
5
> (vector-least-index true #(35 21 16 11 10 9 7 4 1))
0
> (vector-least-index false #(35 21 16 11 10 9 7 4 1))
9

maybe-subvector

(maybe-subvector vector [start 0] [end #f]) => vector

Copy a vector if necessary: return the same if no change in start and end requested. (This is unlike subvector that always generates a fresh vector.)

Examples:

> (maybe-subvector #(1 3 5 7) 2)
#(5 7)
> (def foo #(a b c))
> (eq? foo (maybe-subvector foo 0 3))
#t

subvector-for-each

(subvector-for-each function vector start: (start 0) end: (end #f))

Examples:

> (with-list-builder (c)
   (subvector-for-each
    c #(a b c d e f g h) start: 2 end: 5))
(c d e)

subvector-for-each/index

(subvector-for-each/index function vector start: (start 0) end: (end #f))

Examples:

> (with-list-builder (c)
   (subvector-for-each/index
    (lambda (x y) (c [x y])) #(a b c d e f g h) start: 5))
((5 f) (6 g) (7 h))

subvector-reverse-for-each

(subvector-reverse-for-each function vector start: (start 0) end: (end #f))

Examples:

> (with-list-builder (c)
   (subvector-reverse-for-each c #(a b c d e f g h) start: 2 end: 5))
(e d c)

subvector-reverse-for-each/index

(subvector-reverse-for-each/index function vector start: (start 0) end: (end #f))

Examples:

> (with-list-builder (c)
   (subvector-reverse-for-each/index
    (lambda (x y) (c [x y])) #(a b c d e f g h) start: 5))
((7 h) (6 g) (5 f))

subvector->list

(subvector->list vector start: (start 0) end: (end #f))

Examples:

> (subvector->list #(a b c d e f g h) start: 5)
(f g h)

cons->vector

(cons->vector pair)

Examples:

> (cons->vector '(a . b))
#(a b)
> (cons->vector 'foo)
#f

vector-filter

(vector-filter pred? v start: (start 0) end: (end #f))

Filter entries of a vector v to those that satisfy the predicate pred? and having indexes between the optional start (inclusive, defaults to 0) and an end (exclusive, defaults to #f which designates the vector length). Return a fresh vector with the filtered entries.

Examples:

> (vector-filter odd? #(1 2 3 4 5 6 7 8 9) start: 1 end: 7)
#(3 5 7)