Builtins and Libraries
3.1 Global Utilities
3.2 Numbers
3.3 Strings
3.4 Booleans
3.5 Raw  Array
3.6 Tables
3.7 lists
3.8 sets
3.9 arrays
3.10 string-dict
3.11 option
3.12 pick
3.13 either
3.14 srcloc
3.15 pprint
3.16 s-exp
3.17 s-exp-structs
3.18 image-structs
3.19 image
3.20 world
3.21 gdrive-sheets
3.22 data-source
3.23 reactors
3.24 chart
3.25 plot
3.26 statistics
3.27 math
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-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-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