Next: , Previous: , Up: Data Structures   [Contents][Index]

7.1.2 Subarrays

(require 'subarray)

Function: subarray array select …

selects a subset of an array. For 0 <= j < n, selectj is either an integer, a list of two integers within the range for the jth index, or #f.

When selectj is a list of two integers, then the jth index is restricted to that subrange in the returned array.

When selectj is #f, then the full range of the jth index is accessible in the returned array. An elided argument is equivalent to #f.

When selectj is an integer, then the rank of the returned array is less than array, and only elements whose jth index equals selectj are shared.

> (define ra '#2A((a b c) (d e f)))
> (subarray ra 0 #f)
#1A(a b c)
> (subarray ra 1 #f)
#1A(d e f)
> (subarray ra #f 1)
#1A(b e)
> (subarray ra '(0 1) #f)
#2A((a b c) (d e f))
> (subarray ra #f '(0 1))
#2A((a b) (d e))
> (subarray ra #f '(1 2))
#2A((b c) (e f))
> (subarray ra #f '(2 1))
#2A((c b) (f e))

Arrays can be reflected (reversed) using subarray:

> (subarray '#1A(a b c d e) '(4 0))
#1A(e d c b a)
Function: array-trim array trim …

Returns a subarray sharing contents with array except for slices removed from either side of each dimension. Each of the trims is an exact integer indicating how much to trim. A positive s trims the data from the lower end and reduces the upper bound of the result; a negative s trims from the upper end and increases the lower bound.

For example:

(array-trim '#(0 1 2 3 4) 1)  ⇒ #1A(1 2 3 4)
(array-trim '#(0 1 2 3 4) -1) ⇒ #1A(0 1 2 3)

(require 'array-for-each)
(define (centered-difference ra)
  (array-map ra - (array-trim ra 1) (array-trim ra -1)))

(centered-difference '#(0 1 3 5 9 22))
  ⇒ #(1 2 2 4 13)

Next: , Previous: , Up: Data Structures   [Contents][Index]