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 there are differences as well.
The following code presents some examples:
var weekDays = ["Mon","Tue","Wed","Thur","Fri"]; var days = [week_days, ["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, then they are equal. Expressions that produce nested sequences (as in the initialization of days
above) 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 may be Object. Sequences may be indexed like Java programming language arrays:
var wednesday = days[2];
There is also a shorthand notation using ".." for sequences whose elements form an arithmetic series, as in:
var nums = [1..100];
This shorthand eliminates the need to manually type out each element.
The []
operator also expresses selection in the
form of predicates. Predicates take the form:
sequence[variableName| booleanExp]
Example:
var nums = [1,2,3,4]; var numsGreaterThanTwo = nums[n|n > 2];
Such an 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 indicies a and b inclusive seq[a..<b] // the sequence between the indicies 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]
There are many different ways to access and/or modify a sequence or sequence slice. The following chapter explores JavaFX Script programming language's list comprehension features.