Sequences come with several other important features worth mentioning here. Although the literal representations of sequences looks like an array, that is where the similarity ends. Sequences support several data management operations such as insert, union, query, and delete. As you will see below, sequence expressions can also be used as generators in JavaFX loops. The code samples are from script file ch01/source-code/src/javafx/Sequence.fx.
JavaFX sequences support several operators:
sizeof operators return the size when applied to a sequence.
Comparison JavaFX sequences can be tested for deep equality. This means that two sequences are the same if they are of the same size and contain the same items. The statement below will print true
Reverse this operator automatically reverses the order in which items are referenced in a sequence.
JavaFX sequences also support operations to manipulate sequences and sequence items directly.
Insert Operation as the name implies, this operation inserts item(s) into a given sequence. The following example shows all of the supported form of insert.
Besides the into directive, note that the insert operation support a before and after clause which specifies the location (index) where the item is to be inserted.
Union sequences can be nested using literal declaration to create new lists:
Delete Operation the delete operation removes items from a given sequence. The following example shows the supported forms of delete.
Note
It is critical to understand that sequences are immutable, meaning that the values in a sequence do not change. Rather, any modification to a sequence (insert, delete, and so on) generates a new sequence object to reflect the modification desired.
When deleting by value (that is, delete "May" from months), all items of same value will be removed from the sequence.
Sequence slice notations are used to generate subsets of larger sequences. Given this sequence
Here are some slice operations supported by JavaFX:
months[1..4] returns a sequence ["Feb", "Mar", "Apr", "May"]
months[0..<3] returns sequence [ "Jan", "Feb", "Mar"]
months[3..] returns sequence ["Apr", "May", "Jun"]
months[0..<] returns sequence ["Jan", "Feb", "Mar", "Apr", "May"]
Sequence Projection you can use constraint expressions to project sub-sequences on a given sequence using format sequence[x | {Boolean expression}]. This notation reads as "select all element x where the Boolean expression is true".
The above code returns sequence ["Mar", "May"] from var months declared previously. This expression creates slices based on given arbitrary Boolean condition. It reads as "for all item m in months where m starts with M."
The loop structure is used to query elements in sequences to create subsets based on conditional expressions. The general format is:
The loop expression can use a where clause along with a Boolean expression to filter down to specific elements to build the subset. A simple example is:
In the previous code, the loop generates a new sequence with only even members from the original sequence, [ 2, 4, 6, 8, 10, 12, 14 ], using the where clause to specify a selection expression.
Also notice you can add more than one query in the loop expression where the result is a Cartesian product of all subsets expressed in each query. For instance, the following will produce 14 elements
This code loops over two sequences; the first sequence contains all even members of the original points variable declared previously; the other is a two-member sequence containing 2 and 4, the loop generates new sequence [ 4, 8, 8, 16, 12, 24, 16, 32, 20, 40, 24, 48, 28, 56 ].