Package

org.taffy.core.container

Object Hierarchy
What is it?

An list is a variable-sized container that provides fast insertion and deletion from its ends. As with all Taffy containers, its objects are of arbitrary type.

Quick Example
list = new List
list push: 1
list push: 2



Table of Contents:

Constructing an List

A list is constructed via the new keyword, or via the List#fromArray method. The following example shows two ways to create a list with two objects — 1 and 2.

list = new List
list push: 1
list push: 2

list = [List fromArray: [1, 2]]

Adding and Removing

List supports two add methods: push: and unshift:. The push: method adds the given object to the end of the list, while the unshift: method adds the given object to the beginning of the list.
The pop and shift methods remove an object from the end, and remove an object from the beginning, respectively:

list = [List fromArray: [1, 2, 3, 4]]
==> #List[1, 2, 3, 4]

// pop removes from the end, 4 is removed
list pop
==> 4

// shift removes from the beginning, 1 is removed
list shift
==> 1

list
==> #List[2, 3]

// now we add 1 back
list unshift: 1]
==> #List[1, 2, 3]

Iterating a list

A list is iterated via the each: method:

list = [List fromArray: [1, 2, 3, 4]]

list each: { <value>
    io putLine: value
}

==> 1
==> 2
==> 3
==> 4

Arithmetic

The List class supports all Taffy mathematical operators, and contains the methods from LineContainer. When an mathematical operator is applied to a list, it is treated as if the mathematical operator was applied to each element in the list. The List class does not support assignment operators, or operators with = in them. Here are some examples:

list = [List fromArray: [1, 2, 3, 4]]

list + 2
==> #List[3, 4, 5, 6]

// see that list is unchanged
list
==> [1, 2, 3, 4]

list * 2
==> #List[2, 4, 6, 8]

// now average the list, 1 thru 4
list mean
==> 2.5
List Operators
+ (add)
- (subtract)
/ (divide)
* (multiply)
^ (raise)
% (modulus)
! (factorial)

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

Vectorization

Vectorization applies a given function to each element in a list:

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

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

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