Getting the spacing of a vector

I want to take the spacing of a vector in Outline. I know there is a procedure named vector->values

, but it looks like it returns each item separately, while I want the result as a vector. How can I achieve this?

> (vector->values (vector 1 2 3 4 5) 0 3)
1
2
3

      

while I need:

#(1 2 3)

      

+2


source to share


5 answers


If you are using PLT, you have a few simple ways to get this:

(define (subvector v start end)
  (list->vector (for/list ([i (in-vector v start end)]) i)))

(define (subvector v start end)
  (build-vector (- end start) (lambda (i) (vector-ref v (+ i start)))))

(define (subvector v start end)
  (define new (make-vector (- end start)))
  (vector-copy! new 0 v start end)
  new)

      



The latter will probably be the fastest. The reason there is no such operation that is built in is because people usually don't. When you deal with vectors in Scheme, you usually do it because you want to optimize something, so returning a vector and a range instead of allocating a new one is more common.

(And if you think it's helpful, suggest it on the PLT mailing list.)

+3


source


Scheme R6RS has a vector-vector, vector-referent, vector set! and the length of the vector. With this you can write your own function subvector, which doesn't seem to be part of R6RS (!). Some schema implementations already have something like subvector.



You can also switch to Common Lisp, which provides the SUBSEQ function in the standard.

+2


source


Here is a portable version of R6RS using SRFI 43 :

#!r6rs

(import (rnrs base)
        (prefix (srfi :43) srfi/43:))

(srfi/43:vector-copy (vector 1 2 3 4 5) 0 3)

      

+2


source


#lang scheme
(define (my-vector-value v l h c)
  (if (and (>= c l) (< c h))
      (cons (first v) (my-vector-value (rest v) l h (add1 c)))
      empty))

(list->vector (my-vector-value (vector->list (vector 1 2 3 4 5)) 0 3 0))

      

Ghetto? Yes very. But it only took two minutes to write and do my job.

(I find it is generally easier to play with lists in Outline)

+1


source


you want subvector

:

(subvector (vector 1 2 3 4 5) 0 3)

      

0


source







All Articles