Chapter 7. Sequence Comprehensions

Sequence Iteration with for

The JavaFX Script programming language supports sequence comprehensions with a familiar syntax that should be easily understood by most developers, namely 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 subset of the cartesian product 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) {
     for(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; it 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;
}

Sequence Comprehension Specifiers

In addition to the for operator shown above, the following sequence comprehension specifiers provide a convenient and simple mechanism for accessing or modifying the elements of a sequence:

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: a..>b a..

sizeof returns the number of elements in a sequence:

sizeof seq

indexof returns the ordinal position of an element within a sequence:

indexof seq[idx]

reverse reverses the sequence:

reverse seq

The following code provides examples of each:

// 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]

// SIZEOF EXAMPLES 

nums = [1..5];
sizeof nums; // returns 5

// INDEXOF EXAMPLES

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

// REVERSE EXAMPLES 

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