On this page:
Raw  Array
3.5.1 Raw  Array Functions
raw-array
raw-array-of
raw-array-get
raw-array-set
raw-array-length
raw-array-to-list
raw-array-from-list
raw-array-build
raw-array-fold

3.5 RawArray

A RawArray is a mutable, fixed-length collection indexed by non-negative intgers. They are a very thin wrapper around native JavaScript arrays, and are manipulated entirely via functions. They are the implementation mechanism for Pyret arrays, which provide a nicer API for interacting with them.

RawArrays are widely used internally in Pyret language development.

3.5.1 RawArray Functions
[raw-array: elt :: a, ...] -> RawArray<a>

Constructs a RawArray array of length count, whose elements are the values specified in the construction expression.

Note that RawArrays are mutable, so comparisons using == (the operator for equal-always) will only return true on RawArrays when they are also identical, regardless of their contents. To compare the elements, use equal-now/=~, and test with is=~.

Examples:

check: [raw-array: 1, 2, 3] is-not== [raw-array: 1, 2, 3] [raw-array: 1, 2, 3] is-not [raw-array: 1, 2, 3] [raw-array: 1, 2, 3] is=~ [raw-array: 1, 2, 3] a = [raw-array: 1, 2, 3] a is a a is== a end

raw-array-of :: (value :: a, count :: Number) -> RawArray<a>

Constructs an RawArray of length count, where every element is the value given as elt.

Note that value is not copied, so, the elements of RawArrays created with raw-array-of will always be identical (with the usual caveats if the value was a function or method).

Examples:

check: arr = raw-array-of(true, 2) arr is=~ [raw-array: true, true] arr is-not [raw-array: true, true] raw-array-get(arr, 0) is<=> raw-array-get(arr, 1) raw-array-set(arr, 1, false) arr is=~ [raw-array: true, false] arr-of-arrs = raw-array-of(arr, 3) arr-of-arrs is=~ [raw-array: [raw-array: true, false], [raw-array: true, false], [raw-array: true, false]] raw-array-set(arr, 0, false) arr-of-arrs is=~ [raw-array: [raw-array: false, false], [raw-array: false, false], [raw-array: false, false]] end

raw-array-get :: (array :: RawArray<a>, index :: Number) -> a

Returns the value at the given index. If the index is too large, is negative, or isn’t a whole number, an error is raised.

Examples:

check: a = [raw-array: "a", "b", "c"] raw-array-get(a, 0) is "a" raw-array-get(a, 1) is "b" raw-array-get(a, 2) is "c" end

raw-array-set :: (
array :: RawArray<a>,
index :: Number,
new-value :: a
)
-> RawArray<a>

Updates the value at the given index, returning the new value. The update is stateful, so all references to the RawArray see the update.

Examples:

check: a = [raw-array: "a", "b", "c"] raw-array-get(a, 0) is "a" b = a raw-array-set(a, 0, "d") a is=~ [raw-array: "d", "b", "c"] b is=~ [raw-array: "d", "b", "c"] c = raw-array-set(a, 0, "z") c is=~ [raw-array: "z", "b", "c"] end

raw-array-length :: (array :: RawArray<a>) -> Number

Examples:

check: a = [raw-array: "a", "b"] raw-array-length(a) is 2 b = [raw-array:] raw-array-length(b) is 0 end

raw-array-to-list :: (array :: RawArray<a>) -> List<a>

Converts a RawArray to a List containing the same elements in the same order.

Note that it does not recursively convert RawArrays; only the top-level is converted.

Examples:

check: a = [raw-array: 1, 2, 3] raw-array-to-list(a) is [list: 1, 2, 3] a2 = raw-array-of([raw-array:], 3) raw-array-to-list(a2) is=~ [list: [raw-array:], [raw-array:], [raw-array:]] raw-array-to-list(a2) is-not=~ [list: [list:], [list:], [list:]] end

raw-array-from-list :: (lst :: List<a>) -> RawArray<a>

Converts a List to a RawArray containing the same elements in the same order.

raw-array-build :: (f :: (Number -> a), size :: Number) -> RawArray<a>

Constructs an array of length size, and fills it with the result of calling the function f with each index from 0 to size - 1.

Examples:

check: fun sq(x): x * x end raw-array-build(sq, 4) is=~ [raw-array: sq(0), sq(1), sq(2), sq(3)] end

raw-array-fold :: (
f :: (b, a, Number -> b),
init :: b,
array :: RawArray<a>,
start-index :: Number
)
-> b

Combines the elements in the array with a function that accumulates each element with an intermediate result. Similar to fold_n. Has an argument order that works with for. The numeric argument to the accumulator is the index of the current element.

Examples:

check: a = [raw-array: "a", "b", "c"] str = for raw-array-fold(str from "", elt from a, i from 0): if i < (raw-array-length(a) - 1): str + elt + ": " + tostring(i) + ", " else: str + elt + ": " + tostring(i) end end str is "a: 0, b: 1, c: 2" end