Chapter 6. Declaring Sequences

In addition to the five basic types previously described, the JavaFX™ Script programming language also provides data structures known as sequences. Sequences are similar to Java programming language arrays, but some differences are notable.

The following code presents some examples:

var weekDays = ["Mon","Tue","Wed","Thur","Fri"];
var days = [weekDays, ["Sat","Sun"]];

Sequences represent ordered lists of objects. Sequences are not themselves objects, however, and do not nest. Sequences are compared for equality by value. If their lengths are equal and their elements are equal, they are equal. Expressions that produce nested sequences (like the previous initialization of days) are automatically flattened.


days == ["Mon","Tue","Wed","Thur","Fri","Sat","Sun"]; // returns true

In addition, a single object is equal to a sequence of one object:

1 == [1]; // returns true

Sequence types are declared with the [] annotation:

var xs:Number[]; // sequence of Number
var strs:String[]; // sequence of String

The elements of a sequence must have a common type, which might be Object. Sequences can be indexed like Java programming language arrays:


var wednesday = days[2];

Also a shorthand notation uses ".." for sequences that contain elements forming an arithmetic series, for example:

var nums = [1..100];

This shorthand eliminates the need to type out each element manually.

The [] operator also expresses selection in the form of predicates. Predicates take the following form:

sequence[variableName| booleanExp]

Example:

var nums = [1,2,3,4];
var numsGreaterThanTwo = nums[n|n > 2];

This type of expression returns a new sequence consisting of those elements of the original sequence that satisfy the predicate.

Finally, sequence slices provide access to portions of a sequence:

seq[a..b]  // the sequence between the indices a and b inclusive
seq[a..<b] // the sequence between the indices a inclusive and b exclusive
seq[a..]  // same as seq[a..<sizeof seq]
seq[a..<] // for consistancy.  This is the same as seq[a..<sizeof seq-1] 

You can access and/or modify a sequence or sequence slice in many different ways, as described in the next chapter.