6.6 Types, Predicates and Accessors
syntax
(Array A)
> (define arr (array #[1 2 3 4 5])) > arr
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Byte))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[1 2 3 4 5])
> (ann arr (Array Real))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Real))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[1 2 3 4 5])
> (ann arr (Array Any))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Any))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[1 2 3 4 5])
> (define arr (mutable-array #[1 2 3 4 5])) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array #[1 2 3 4 5])
> (ann arr (Array Real))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Real))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(mutable-array #[1 2 3 4 5])
> (ann arr (Array Any))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Any))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(mutable-array #[1 2 3 4 5])
syntax
(Settable-Array A)
> (define arr (mutable-array #[1 2 3 4 5])) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array #[1 2 3 4 5])
> (ann arr (Settable-Array Integer))
- : #(struct:Settable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)))
(mutable-array #[1 2 3 4 5])
> (ann arr (Settable-Array Real)) eval:93:0: Type Checker: type mismatch
expected: #(struct:Settable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Real)
(-> Indexes Real Void)))
given: #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/p
rivate/array/typed-mutable-array.rkt:14:13
prop:custom-write>)
in: Real
syntax
(Mutable-Array A)
> (define arr (mutable-array #[#[1 2] #[3 4]])) > (vector-set! (mutable-array-data arr) 0 -10) > arr
- : #(struct:Mutable-Array
(Indexes
Index
(Boxof Boolean)
(-> Void)
(-> Indexes Integer)
(-> Indexes Integer Void)
(Vectorof Integer))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-mutable-array.rkt:14:13 prop:custom-write>)
(mutable-array #[#[-10 2] #[3 4]])
syntax
> (array-shape (array #[#[#[0]]])) - : Indexes
'#(1 1 1)
syntax
> (define ds #(3 2)) > ds - : (Immutable-Vector Positive-Byte Positive-Byte)
'#(3 2)
> (make-array ds (void))
- : #(struct:Array
(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Void))
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>
#<syntax:build/user/8.13.0.7/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)
(array #[#[#<void> #<void>] #[#<void> #<void>] #[#<void> #<void>]])
This makes indexes-accepting functions easier to use, because it is easier to convince Typed Racket that a vector contains Integer elements than that a vector contains Index elements.
> (define js ((inst vector Index) 3 4 5)) > js - : (Mutable-Vectorof Index)
'#(3 4 5)
> (ann js (Vectorof Integer)) eval:103:0: Type Checker: type mismatch
expected: (Vectorof Integer)
given: (Mutable-Vectorof Index)
in: Integer
> (ann js In-Indexes) - : In-Indexes
'#(3 4 5)
procedure
v : Any
procedure
(settable-array? v) → Boolean
v : Any
procedure
(mutable-array? v) → Boolean
v : Any
> (: maybe-array-data (All (A) ((Array A) -> (U #f (Vectorof A)))))
> (define (maybe-array-data arr) (cond [(mutable-array? arr) (mutable-array-data arr)] [else #f])) eval:106:0: Type Checker: Polymorphic function
`mutable-array-data' could not be applied to arguments:
Argument 1:
Expected: (Mutable-Array A)
Given: (Struct Mutable-Array)
in: #f
> array? - : (-> Any Boolean : (Array Any))
#<procedure:Array?>
> settable-array? - : (-> Any Boolean : (Struct Settable-Array))
#<procedure:Settable-Array?>
> mutable-array? - : (-> Any Boolean : (Struct Mutable-Array))
#<procedure:Mutable-Array?>
procedure
(array-shape arr) → Indexes
arr : (Array A)
> (array-shape (array 0)) - : Indexes
'#()
> (array-shape (array #[0 1])) - : Indexes
'#(2)
> (array-shape (array #[#[0 1]])) - : Indexes
'#(1 2)
> (array-shape (array #[])) - : Indexes
'#(0)
procedure
(array-size arr) → Index
arr : (Array A)
> (array-size (array 0)) - : Integer [more precisely: Index]
1
> (array-size (array #[0 1])) - : Integer [more precisely: Index]
2
> (array-size (array #[#[0 1]])) - : Integer [more precisely: Index]
2
> (array-size (array #[])) - : Integer [more precisely: Index]
0
procedure
(array-dims arr) → Index
arr : (Array A)
procedure
(mutable-array-data arr) → (Vectorof A)
arr : (Mutable-Array A)