6.1 Quick Start
> (array #[0 1 2 3 4])
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Byte))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[0 1 2 3 4])
> (array #[#['first 'row 'data] #['second 'row 'data]])
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes (U 'data 'first 'row 'second)))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#['first 'row 'data] #['second 'row 'data]])
> (array "This array has zero axes and one element")
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes String))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array "This array has zero axes and one element")
> (define arr (build-array #(4 5) (λ: ([js : Indexes]) (match-define (vector j0 j1) js) (+ j0 j1)))) > arr
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Nonnegative-Fixnum))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[0 1 2 3 4] #[1 2 3 4 5] #[2 3 4 5 6] #[3 4 5 6 7]])
> (array-ref arr #(2 3)) - : Integer [more precisely: Nonnegative-Fixnum]
5
> (define brr (array->mutable-array arr)) > (array-set! brr #(2 3) -1000) > brr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array #[#[0 1 2 3 4] #[1 2 3 4 5] #[2 3 4 -1000 6] #[3 4 5 6 7]])
> (array-map (λ: ([n : Natural]) (* 2 n)) arr)
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Nonnegative-Integer))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[0 2 4 6 8] #[2 4 6 8 10] #[4 6 8 10 12] #[6 8 10 12 14]])
> (array+ arr arr)
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Nonnegative-Integer))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[0 2 4 6 8] #[2 4 6 8 10] #[4 6 8 10 12] #[6 8 10 12 14]])
> (array* arr (array 2))
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Nonnegative-Integer))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[0 2 4 6 8] #[2 4 6 8 10] #[4 6 8 10 12] #[6 8 10 12 14]])
> (array* arr (array #[0 2 0 2 0]))
- : #(struct:Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Nonnegative-Integer))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[0 2 0 6 0] #[0 4 0 8 0] #[0 6 0 10 0] #[0 8 0 12 0]])
> (array-slice-ref arr (list (::) (:: 0 5 2)))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Nonnegative-Fixnum))
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.15.0.8/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[0 2 4] #[1 3 5] #[2 4 6] #[3 5 7]])
Functional code that uses whole-array operations often creates many short-lived, intermediate arrays whose elements are referred to only once. The overhead of allocating and filling storage for these arrays can be removed entirely by using nonstrict arrays, sometimes at the cost of making the code’s performance more difficult to reason about. Another bonus is that computations with nonstrict arrays have fewer synchronization points, meaning that they will be easier to parallelize as Racket’s support for parallel computation improves. See Nonstrict Arrays for details.