3.5 RawArray
The type of raw array values. Raw arrays are a primitive datastructure that allows for lookup and (mutable) update by non-negative integers.
Raw arrays are used in the interface to constructor functions like [constr: e1, e2, ...], as the way of bundling up the values from e1, e2, etc. to pass them to constr.make.
3.5.1 RawArray Functions
Creates a RawArray with the given elements. 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=~.
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
Creates a RawArray with length equal to count, and with each index holding value. Note that value is not copied, so, the elements of arrays created with raw-array-of will always be identical (with the usual caveats if the value was a function or method).
check: a1 = raw-array-of("init", 3) raw-array-length(a1) is 3 a1 is=~ [raw-array: "init", "init", "init"] a2 = raw-array-of({}, 3) raw-array-length(a2) is 3 raw-array-get(a2, 0) is<=> raw-array-get(a2, 1) raw-array-get(a2, 1) is<=> raw-array-get(a2, 2) end
- raw-array-set :: (
- array :: RawArray<a>,
- index :: Number,
- new-value :: a
- )
- -> RawArray<a>
a1 = raw-array-of(0, 3) raw-array-length(a1) is 3 raw-array-set(a1, 0, 1) raw-array-set(a1, 1, 2) raw-array-set(a1, 2, 3) raw-array-get(a1, 0) is 1 raw-array-get(a1, 1) is 2 raw-array-get(a1, 2) is 3 raw-array-get(a1, 3) raises "too large"
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.
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.
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