Racket-y way on multidimensional vectors operation?
Asked Answered
V

1

2

I've read this question before, and followed Eli Barzilay's answer on srfi-25.

Besides reading the source code of srfi-25, I found writing some auxiliary function would be much more easier, for example

#lang racket

(define (set2v! vec x y value)
  (vector-set! (vector-ref vec x) y value))

(define (get2v vec x y)
  (vector-ref (vector-ref vec x) y))

(define v2 (vector (vector 1 2 3) (vector 4 5 6) (vector 7 8 9)))

(get2v v2 1 1)

(set2v! v2 1 1 99)

(get2v v2 1 1)

I was wondering if there maybe some Racket-y way on multidimensional vectors operation?

Venessavenetia answered 26/1, 2016 at 8:27 Comment(0)
T
3

An alternative to using nested vectors for multidimensional vectors is to use the math library's array data structure.

Here's an example use:

Welcome to Racket v6.4.0.4.
-> (require math/array)
-> (define arr (mutable-array #[#[1 2 3] #[4 5 6]]))
-> (array-ref arr #(0 0))
1
-> (array-ref arr #(1 2))
6
-> (array-set! arr #(1 2) 15)
-> (array-ref arr #(1 2))
15

There is a caveat that this will be slower when you use the library from untyped code (e.g., #lang racket). It will be fast when used in Typed Racket.

Tar answered 26/1, 2016 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.