Package

org.taffy.core.container

Object Hierarchy
What is it?

An array is a fixed-size container that provides fast lookup and insertion. Its objects are always of arbitrary type.

Quick Example
array = [1, 2, 3, 4]



Table of Contents:

Constructing an Array

An array is defined using immediate or delayed construction. An example of immediate construction:

myArray = [1, 2, 3]

and delayed construction:

myArray = [Array createWithCapacity: 3]
myArray[0] = 1
myArray[1] = 1
myArray[2] = 3

both examples produce the same result.

Setting and Getting

To set a value use the []= operator or the insertObject:atIndex: method. Example:

myArray = [1, 2, 3]

myArray[0] = "one"
===> ["one", 2, 3]

[myArray insertObject: "two" atIndex: 1]
===> ["one", "two", 3]

To get a value use the [] operator or the objectAtIndex: method. Example:

myArray = [1, 2, 3]

myArray[0]
===> 1

myArray objectAtIndex: 0
===> 0

Iterating an Array

The elements of an array can be iterated by using one of three methods: a loop, the each: method or the eachIndex: method.

You can use a standard for or while loop to iterate thru an array:

array = [1, 2, 3, 4]

// use a for loop
for (a = 0; a < [array size]; a++)
{
    io putLine: "Array has: " + array[a]
}

// use a while loop
a = 0

while (a < [array size])
{
    io putLine: "Array has: " + array[a]
    a++
}

The each: and eachIndex: methods allow the same iteration as a for or while loop, but with the added benefit that they verify that contents aren’t changed during iteration.

array = [1, 2, 3, 4]

// use each:
array each: { <value>
    io putLine: "Array has: " + value
}

// use useIndex:
array eachIndex: { <index>
    io putLine: "Array has: " + array[index]
}

Arithmetic

The Array class supports all Taffy mathematical operators, and contains the methods from LineContainer. When an mathematical operator is applied to an Array, it is treated as if the mathematical operator was applied to each element in the array. Operators without = in them do not modify the left argument, while modifiers with = do modify the left argument. Here are some examples:

array = [1, 2, 3, 4]

array + 2
==> [3, 4, 5, 6]

// see that array is unchanged
io putLine: array
==> [1, 2, 3, 4]

// modify array itself
array += 2
array
==> [3, 4, 5, 6]

array * 2
==> [2, 4, 6, 8]

[1, 2, 3] mean
==> 2
Array Operators
+ (add)
++ (add-one-to-self)
+= (add-equals)
- (subtract)
-- (subtract-self-by-one)
-= (subtract-equals)
/ (divide)
/= (divide-equals)
* (multiply)
*= (multiply-equals)
^ (raise)
^= (raise-equals)
% (modulus)
! (factorial)

// operators requiring whole left and right values
<< (left-shift)
<<= (left-shift-self)
>> (right-shift)
>>= (right-shift-self)
| (bit-or)
|= (bit-or-self)
& (bit-and)
&= (bit-and-self)
^^ (bit-xor)
^^= (bit-xor-self)

Vectorization

Vectorization applies a given function to each element in an Array:

input = [20, 30, 15]
f(x) = sin(x)

// vectorize: creates a new Array
input vectorize: f
==> [0.9129452507276281, -0.9880316240928519, 0.6502878401571161]

// see that input is unchanged
input
==> [20, 30, 15]

// vectorize!: modifies the existing Array
input vectorize!: f

input
==> [0.9129452507276281, -0.9880316240928519, 0.6502878401571161]