Chapter 7. Using Sequences

Sequence Iteration With for

The JavaFX™ Script programming language supports sequence comprehensions with the for operator. A sequence comprehension consists of one or more input sequences, an optional filter, and an expression. Each input sequence is associated with a variable. The result of the sequence comprehension is a new sequence which is the result of applying the expression to the combination of the source sequences' elements that satisfy the filter.

The following program demonstrates this syntax, using the for operator to identify the title tracks in a list of albums:


class Album {
     attribute title: String;
     attribute artist: String;
     attribute tracks: String[];
}

var albums =
     [Album {
          title: "A Hard Day's Night"
          artist: "The Beatles"
          tracks:
               ["A Hard Day's Night",
                "I Should Have Known Better",
                "If I Fell",
                "I'm Happy Just To Dance With You",
                "And I Love Her",
                "Tell Me Why",
                "Can't Buy Me Love",
                "Any Time At All",
                "I'll Cry Instead",
                "Things We Said Today",
                "When I Get Home",
                "You Can't Do That"]
     },
     Album {
          title: "Circle Of Love"
          artist: "Steve Miller Band"
          tracks:
               ["Heart Like A Wheel",
                "Get On Home",
                "Baby Wanna Dance",
                "Circle Of Love",
                "Macho City"]
     }];


for (album in albums, track in album.tracks)  {
    if (album.title == track)
        java.lang.System.out.println("TITLE TRACK = {track}")
    else
        java.lang.System.out.println("Track = {track}")
  } 
    

Output:

TITLE TRACK = A Hard Day's Night
Track = I Should Have Known Better
Track = If I Fell
Track = I'm Happy Just To Dance With You
Track = And I Love Her
Track = Tell Me Why
Track = Can't Buy Me Love
Track = Any Time At All
Track = I'll Cry Instead
Track = Things We Said Today
Track = When I Get Home
Track = You Can't Do That
Track = Heart Like A Wheel
Track = Get On Home
Track = Baby Wanna Dance
TITLE TRACK = Circle Of Love
Track = Macho City

Here is another example that uses a filter. This example defines a function that takes a number and returns a list of all its factors:

function factors(n:Number) {
     return for (i in [1 .. n/2] where n % i == 0) i;
}

Within a for-comprehension, the indexof operator can be used; its syntax is indexof name where name is the name of an iteration variable. The value is the "index" of the iteration within in the base sequence.

The indexof operator can also be used in sequence selection, as shown in the following example:

var nums = [1..5];
var numsExceptTheFirstTwo = nums[n|indexof n > 1]; // returns 3,4,5

Modifying Sequence Variables

insert inserts a new element into a sequence:

insert x into seq
insert x before seq[idx]
insert x after seq[idx]

delete removes an element from a sequence:

delete seq
delete x from seq
delete seq[idx]
delete seq[a..b] // and all other slice forms  

The following code provides examples of both inserting and deleting elements:

// INSERT EXAMPLES

var nums = [1..5];
var x = 6;
insert x into nums; // result is [1,2,3,4,5,6]
x++;
insert x before nums[0]; // result is [7,1,2,3,4,5,6]
x++;
insert x after nums[3]; // result is [7,1,2,3,8,4,5,6]

// DELETE EXAMPLES

nums = [1..5];
delete 2 from nums; // result is [1,3,4,5]
delete nums[0];// result is [3,4,5]
nums = [1..10]; // result is [1,2,3,4,5,6,7,8,9,10]
delete nums[3..7]; // result is [1,2,3,9,10]
delete nums; // result is []
nums = [1..10];
delete nums[5..]; // result is [1,2,3,4,5]
delete nums[0..>]; // result is [5]


Elements of a sequence also can be reversed:

var nums = [1..5];
reverse nums; // returns 5,4,3,2,1