6.6 Types, Predicates and Accessors
syntax
(Array A)
> (define arr (array #[1 2 3 4 5])) > arr - : (Array Positive-Byte)
(array #[1 2 3 4 5])
> (ann arr (Array Real)) - : (Array Real)
(array #[1 2 3 4 5])
> (ann arr (Array Any)) - : (Array Any)
(array #[1 2 3 4 5])
> (define arr (mutable-array #[1 2 3 4 5])) > arr - : (Mutable-Array Integer)
(mutable-array #[1 2 3 4 5])
> (ann arr (Array Real)) - : (Array Real)
(mutable-array #[1 2 3 4 5])
> (ann arr (Array Any)) - : (Array Any)
(mutable-array #[1 2 3 4 5])
syntax
(Settable-Array A)
> (define arr (mutable-array #[1 2 3 4 5])) > arr - : (Mutable-Array Integer)
(mutable-array #[1 2 3 4 5])
> (ann arr (Settable-Array Integer)) - : (Settable-Array Integer)
(mutable-array #[1 2 3 4 5])
> (ann arr (Settable-Array Real)) eval:93:0: Type Checker: type mismatch
expected: (Settable-Array Real)
given: (Mutable-Array Integer)
in: Real
syntax
(Mutable-Array A)
> (define arr (mutable-array #[#[1 2] #[3 4]])) > (vector-set! (mutable-array-data arr) 0 -10) > arr - : (Mutable-Array Integer)
(mutable-array #[#[-10 2] #[3 4]])
syntax
> (array-shape (array #[#[#[0]]])) - : Indexes
'#(1 1 1)
syntax
> (define ds #(3 2)) > ds
- : (U (Immutable-Vector Positive-Byte Positive-Byte)
(Mutable-Vector Positive-Byte Positive-Byte)) [more precisely: (Immutable-Vector Positive-Byte Positive-Byte)]
'#(3 2)
> (make-array ds (void)) - : (Array Void)
(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 - : Indexes [more precisely: (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)