Racket-y way on multidimensional vectors operation?

前端 未结 1 845
终归单人心
终归单人心 2021-01-21 11:40

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 woul

相关标签:
1条回答
  • 2021-01-21 12:23

    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.

    0 讨论(0)
提交回复
热议问题