arrays

append, compose, copy, decompose, equal, fill, inject, insert, iterate, qsort, remove, rsort, sort


append

Syntax

@array = append(@array, [<elements>, ...])

Description

Appends <elements> to @array. If [@array].n does not exist it will be initialized to 0 and this routine will in practice work like compose(). Unlike compose() however, all element argument must be present. The returned value is the input @array reference.

Notice that calling this function on a "double-ended queue" also works.

Examples

append(@myArray, 5, 6, 7, 8)
equal(append(compose(@temp1, 'a', 'b', 'c'), 'd', 'e', 'f'), compose(@temp2, 'a', 'b', 'c', 'd', 'e', 'f')) == true

See Also

compose, insert


compose

Syntax

@array = compose(@array, [<elements>, ...])

Description

Creates an array of indexed elements under @array initialized with the values of the remaining arguments (<element>). The first element will have an index of zero (e.g. "array[0]"). The special element 'n' (e.g. "array.n") will contain the number of indexed elements in the array. If an element argument is omitted the corresponding element will not be initialized, possibly making the array "non-contiguous". The returned value is the input @array reference.

Notice that this function does not erase any existing elements under @array. You may want to consider calling prune() on @array before composing a new array.

Examples

compose(@myArray, 1, 2, 3, 4)
compose(@holy, 'nextIsEmpty', , 'previousWasEmpty')
[compose(@temp, 'zero', 'one', 'two', 'three')][2] === 'two'

See Also

append, decompose, map, prune


copy

Syntax

@target = copy(@source, +offset, +count, @target, +index)

Description

Copies +count elements from the @source array beginning at +offset into @target at +index, replacing any already existing elements at the target indices. The element count of the @target array (i.e. [@target].n) may be incremented to fit the new elements.

The @source array must be contiguous. If the output +index is greater than [@target].n (or (+index) + (+count) < 0), the resulting array will become non-contiguous.

Only direct elements under the arrays will be affected. Any sub-elements that they in turn may have are ignored. @source and @target may reference the same array. The returned value is the input @target reference.

Examples

copy(@myArray, 10, 5, @myArray, 13)
equal(copy(compose(@temp1, 'a', 'b', 'c', 'd'), 1, 2, compose(@temp2, 'e', 'f', 'g', 'h'), 3), compose(@temp3, 'e', 'f', 'g', 'b', 'c')) == true

See Also

clone, inject, remove


decompose

Syntax

decompose(@array, [@variables, ...])

Description

Decomposes an array by storing the indexed elements under @array one by one into the given references. If an argument is left empty, the corresponding element index will be skipped.

Examples

decompose(@breakMe, @first, @second, @third, , @noFourthButFifth)

See Also

compose


equal

Syntax

?same = equal(@arrayA, @arrayB)

Description

Returns true if the arrays @arrayA and @arrayB are the same size and all their elements are identical. Both arrays must be contiguous (i.e. all their elements must be defined). Only direct elements under the arrays will be tested. Any sub-elements that they in turn may have are silently ignored.

Examples

equal(@firstArray, @secondArray)
equal(compose(@temp1, 1, 10, 100, 'one thousand'), compose(@temp2, 1, 10, 100, 'one thousand')) == true

fill

Syntax

@array = fill(@array, +offset, +count, <value>)

Description

Fills a range of +count elements in @array with <value> starting at +offset, replacing any existing elements. If the target @array does not exist (i.e. [@array].n is not defined) it is created. The element count (i.e. [@array].n) may be incremented to fit the new elements.

Only direct elements under the arrays will be affected. The returned value is the input @array reference.

Examples

equal(fill(@a, 0, 5, 'x'), compose(@b, 'x', 'x', 'x', 'x', 'x'))

See Also

copy, inject, insert, remove


inject

Syntax

@target = inject(@source, +offset, +count, @target, +index)

Description

Inserts +count elements from the @source array beginning at +offset into @target at +index, relocating any elements at and after +index to make room for the inserted elements. Both arrays must be contiguous and the target +index must be between 0 and [@target].n. Only direct elements under the arrays will be affected. Any sub-elements that they in turn may have are ignored. @source and @target should not reference the same array. The returned value is the input @target reference.

Examples

inject(@myArray, 10, 5, @myArray, 13)
equal(inject(compose(@temp1, 'a', 'b', 'c', 'd'), 1, 2, compose(@temp2, 'e', 'f', 'g', 'h'), 3), compose(@temp3, 'e', 'f', 'g', 'b', 'c', 'h')) == true

See Also

copy, fill, insert, remove


insert

Syntax

@array = insert(@array, +offset, [<elements>, ...])

Description

Inserts one or more elements into @array before the index +offset. The array must be contiguous. Only direct elements under the array will be moved to make room for the new elements. Any sub-elements that they in turn may have remain unaffected. +offset must be between 0 and the element count of @array (or an exception will be thrown). [@array].n must be defined prior to calling this routine. The returned value is the input @array reference.

Examples

insert(@myArray, 10, 'insert', 'three', 'strings')
equal(insert(compose(@temp1, 'a', 'b', 'f'), 2, 'c', 'd', 'e'), compose(@temp2, 'a', 'b', 'c', 'd', 'e', 'f')) == true

See Also

inject, remove


iterate

Syntax

iterate(@array, >doThis)

Description

Iterates all elements in @array (as determined by [@array].n) and calls >doThis once for every encountered element in ascending index order. Three arguments will be passed to >doThis:

$0 will be the full reference to the found element (e.g. "::highscores.3")
$1 will be the element index (e.g. 3)
$2 will be the value of the element.

iterate() is the equivalent to foreach() for arrays. Any change to [@array].n while running iterate() will not be accounted for. The array must be contiguous. Only direct elements under the array will be iterated.

Examples

iterate(compose(@a, 0, 'one', 2, true), >print($1 # '=' # $2))

See Also

foreach


qsort

Syntax

qsort(+from, +to, >compare, >swap)

Description

This is an abstract implementation of the quicksort algorithm. qsort() handles the logic of the sorting algorithm (the bones) while you provide the functions >compare and >swap that carries out the concrete operations on the data being sorted (the meat).

+from and +to defines the sorting range (+to is non-inclusive).

>compare is called with two sorting indices and you should return a negative value if the data for the first index ($0) should be placed before the data for the second index ($1). Return a positive non-zero value for the opposite and return zero if the data is identical. (You can use the global ::compare function to easily implement this.)

>swap is also called with two indices in $0 and $1 ($0 is always less than $1). The function should swap the data for the two indices. (You can use the global ::swap function to easily implement this.)

The functions sort() and rsort() use this function to implement sorting of entire arrays (ascending and descending respectively).  

Examples

qsort(0, myArray.n, >myArray[$0] - myArray[$1], >swap(@myArray[$0], @myArray[$1]))
qsort(0, scrambleMe.n, >random(2) - 1, >swap(@scrambleMe[$0], @scrambleMe[$1]))

See Also

compare, rsort, sort


remove

Syntax

@array = remove(@array, +offset, [+count = 1])

Description

Removes +count number of elements from @array beginning at +offset, relocating any elements after the removed elements so that the array remains contiguous. (Only direct elements under the array are moved. Any sub-elements under these elements will be left untouched.)

If +offset and / or +count are negative, this function still yields predictable results (e.g. an +offset of -3 and +count of 6 will remove the three first elements). Likewise, it is allowed to remove elements beyond the end of the array (but naturally it will have no effect). The returned value is the input @array reference.

Examples

remove(@removeNumberThree, 3)
remove(@drop1and2, 1, 2)
equal(remove(compose(@temp1, 'a', 'b', 'c', 'd', 'e'), 1, 3), compose(@temp2, 'a', 'e')) == true

See Also

copy, fill, inject, insert, prune


rsort

Syntax

@array = rsort(@array)

Description

Sorts the elements of @array in descending order. The returned value is the input @array reference. To sort in ascending order, use sort(). If you need greater control over the sorting (e.g. how elements are compared), use the lower level function qsort() instead.

Examples

rsort(@myArray)
equal(rsort(compose(@temp1, 1.1, -5, 1.5, 17, 0x10, 'xyz', 'a', 'def', 'a')), compose(@temp2, 'xyz', 'def', 'a', 'a', 17, 0x10, 1.5, 1.1, -5)) == true

See Also

qsort, sort


sort

Syntax

@array = sort(@array)

Description

Sorts the elements of @array in ascending order. The returned value is the input @array reference. To sort in descending order, use rsort(). If you need greater control over the sorting (e.g. how elements are compared), use the lower level function qsort() instead.

Examples

sort(@myArray)
equal(sort(compose(@temp1, 1.1, -5, 1.5, 17, 0x10, 'xyz', 'a', 'def', 'a')), compose(@temp2, -5, 1.1, 1.5, 0x10, 17, 'a', 'a', 'def', 'xyz')) == true

See Also

qsort, rsort