On this page:
array-ref
array-set!
array-indexes-ref
array-indexes-set!
array-slice-ref
array-slice-set!
Slice-Spec
Slice
:  :
slice?
slice-start
slice-end
slice-step
slice->range-values
Slice-Dots
:  :  ...
slice-dots?
Slice-New-Axis
:  :  new
slice-new-axis?
slice-new-axis-length

6.11 Indexing and Slicing

procedure

(array-ref arr js)  A

  arr : (Array A)
  js : In-Indexes
Returns the element of arr at position js. If any index in js is negative or not less than its corresponding axis length, array-ref raises an error.

procedure

(array-set! arr js value)  Void

  arr : (Settable-Array A)
  js : In-Indexes
  value : A
Sets the element of arr at position js to value. If any index in js is negative or not less than its corresponding axis length, array-set! raises an error.

procedure

(array-indexes-ref arr idxs)  (Array A)

  arr : (Array A)
  idxs : (Array In-Indexes)
High-level explanation: Returns multiple elements from arr in a new array.

Lower-level explanation: Returns an array with same shape as idxs, whose elements are array-ref’d from arr using the indexes in idxs.

Examples:

> (define arr (array #[#[1 2] #[10 20]]))
> (define idxs (array #['#(0 0) '#(1 1)]))
> (array-indexes-ref arr idxs)

eval:212:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: arr38

  in: idxs

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

eval:212:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: idxs39

  in: idxs

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

Type Checker: Summary: 2 errors encountered

Implementation-level explanation: (array-indexes-ref arr idxs) is equivalent to
> (build-array (array-shape idxs)
               (λ: ([js : Indexes])
                 (array-ref arr (array-ref idxs js))))

eval:213:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: idxs39

  in: js

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

eval:213:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: arr38

  in: js

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

eval:213:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: idxs39

  in: js

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:133:21: for-loop

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

Type Checker: Summary: 3 errors encountered

but faster.

procedure

(array-indexes-set! arr idxs vals)  Void

  arr : (Settable-Array A)
  idxs : (Array In-Indexes)
  vals : (Array A)
Indexes arr in the same way that array-indexes-ref does, but mutates elements. If idxs and vals do not have the same shape, they are broadcast first.

Examples:

> (define arr (mutable-array #[#[1 2] #[10 20]]))
> (define idxs (array #['#(0 0) '#(1 1)]))
> (array-indexes-set! arr idxs (array -1))

eval:216:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: arr40

  in: -1

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

eval:216:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: idxs41

  in: -1

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

Type Checker: Summary: 2 errors encountered

> arr

eval:217:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr40

  in: arr

When two indexes in idxs are the same, the element at that index is mutated more than once in some unspecified order.

procedure

(array-slice-ref arr specs)  (Array A)

  arr : (Array A)
  specs : (Listof Slice-Spec)
Returns a transformation of arr according to the list of slice specifications specs. See Slicing for a discussion and examples.

procedure

(array-slice-set! arr specs vals)  Void

  arr : (Settable-Array A)
  specs : (Listof Slice-Spec)
  vals : (Array A)
Like array-indexes-set!, but for slice specifications. Equivalent to
(let ([idxs  (array-slice-ref (indexes-array (array-shape arr)) specs)])
  (array-indexes-set! arr idxs vals))

Examples:

> (define arr (array->mutable-array (axis-index-array #(5 5) 1)))
> (array-slice-set! arr (list (:: 1 #f 2) (::)) (array 1))

eval:219:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr42

  in: 1

> arr

eval:220:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr42

  in: arr

> (array-slice-set!
   arr (list (::) (:: 1 #f 2))
   (array-scale (array-slice-ref arr (list (::) (:: 1 #f 2))) -1))

eval:221:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: arr42

  in: -1

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

eval:221:0: Type Checker: missing type for top-level identifier;

 either undefined or missing a type annotation

  identifier: arr42

  in: -1

  context...:

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:515:0: tc-toplevel-form

   fail-to-succeed

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:39:0: tc-setup

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:24:4

   /Users/mflatt/build/macro/racket/collects/racket/private/more-scheme.rkt:148:2: call-with-break-parameterization

   /Users/mflatt/build/macro/build/user/6.2.900.4/pkgs/sandbox-lib/racket/sandbox.rkt:837:5: loop

Type Checker: Summary: 2 errors encountered

> arr

eval:222:0: Type Checker: missing type for top-level

identifier;

 either undefined or missing a type annotation

  identifier: arr42

  in: arr

When a slice specification refers to an element in arr more than once, the element is mutated more than once in some unspecified order.

The type of a slice specification. Currently defined as

A (Sequenceof Integer) slice specification causes array-slice-ref to pick rows from an axis. An Integer slice specification causes array-slice-ref to remove an axis by replacing it with one of its rows.

See Slicing for an extended example.

syntax

Slice

procedure

(:: [end])  Slice

  end : (U #f Integer) = #f
(:: start end [step])  Slice
  start : (U #f Integer)
  end : (U #f Integer)
  step : Integer = 1

procedure

(slice? v)  Boolean

  v : Any

procedure

(slice-start s)  (U #f Fixnum)

  s : Slice

procedure

(slice-end s)  (U #f Fixnum)

  s : Slice

procedure

(slice-step s)  Fixnum

  s : Slice
The type of in-range-like slice specifications, its constructor, predicate, and accessors.

array-slice-ref interprets a Slice like an in-range sequence object. When start or end is #f, it is interpreted as an axis-length-dependent endpoint.

procedure

(slice->range-values s dk)  (Values Fixnum Fixnum Fixnum)

  s : Slice
  dk : Index
Given a slice s and an axis length dk, returns the arguments to in-range that would produce an equivalent slice specification.

This is used internally by array-slice-ref to interpret a Slice object as a sequence of indexes.

syntax

Slice-Dots

value

::... : Slice-Dots

procedure

(slice-dots? v)  Boolean

  v : Any
The type of greedy, multiple-axis-preserving slice specifications, its singleton value, and predicate.

syntax

Slice-New-Axis

procedure

(::new [dk])  Slice-New-Axis

  dk : Integer = 1

procedure

(slice-new-axis? v)  Boolean

  v : Any

procedure

(slice-new-axis-length s)  Index

  s : Slice-New-Axis
The type of slice specifications that indicate inserting a new axis, its constructor, predicate, and accessor. The axis length dk must be nonnegative.