Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Document binary operaions union, intersect etc.

...

Anchor
sequence-creation
sequence-creation

Creation

new sequence

Parameter type

Result type

{ => sequence<Type> }

sequence<Type>

Sequence can be created with initializer.

Code Block

sequence<Type> seq = new sequence<Type>({=> /* code with yield keywords */ })

closure invocation

Result type

sequence<Type>

A sequence may be returned from a closure (see Closures).

Code Block

sequence<Type> seq = {=> /* code with yield keywords producing a sequence */ }.invoke;

array as a sequence

Operand type

Parameter type

Result type

Type[]

none

sequence<Type>

An array can be used as a sequence.

Code Block

T[] arrayOfT;
sequence<T> seq = T.asSequence;

...

foreach statement

Loop statement

Code Block

foreach foo in bar {
    ...
}

is equivalent to

Code Block

for (Type foo: bar) {
    ...
}

...

The code passed as a parameter (as a closure literal or by reference) is executed once for each element.

Code Block

seq.forEach({=> /* arbitrary code*/});

...

Gives the index of a first occurrence in a sequence of an element that is passed to it as a parameter.

Code Block

int idx = seq.indexOf(elm)

...

Produces boolean value, indicating whether or not a sequence contains the specified element.

Code Block

if (seq.contains(elm)) {
...
}

...

Produces boolean value that indicates whether any (in case of any operation) or all (in case of all) of the elements in the input sequence match the condition specified by the closure.

Code Block

if (seq.any({~it=> /* condition */ })) {
...
}

...

Operand type

Parameter type

Result type

sequence<Type>

none

iterator<Type>

Produces an #iterator.

enumerator

Operand type

Parameter type

Result type

sequence<Type>

none

enumerator<Type>

Produces an #enumerator.

Selection and filtering
first

...

Produces a sequence that is sub-sequence of the original one, starting from first element and of size count.

Code Block

seq.take (count) 
skip

Operand type

Parameter type

Result type

sequence<Type>

int

sequence<Type>

Produces a sequence that is sub-sequence of the original one, containing all elements starting with the element at index count.

Code Block

seq.skip (count)
cut

Operand type

Parameter type

Result type

sequence<Type>

int

sequence<Type>

Produces a sequence that is a sub-sequence of the original one, containing all elements starting with first and up to (but not including) the element at index size minus count. In other words, this operation returns a sequence with all elements from the original one except the last count elements.

Code Block

seq.cut (count)
tail

Operand type

Parameter type

Result type

sequence<Type>

int

sequence<Type>

Produces a sequence that is a sub-sequence of the original one, containing all elements starting with the element at index size minus count. In other words, this operations returns a sequence with count elements from the end of the original sequence, in the original order.

Code Block

seq.tail (count)
page

Operand type

Parameter type

Result type

sequence<Type>

int
int

sequence<Type>

Results in a sequence that is a sub-sequence of the original one, containing all elements starting with the element at index start and up to (but not including) the element at index end. It is a requirement that start is no greater than end.

Code Block

seq.page (start, end)


This is equivalent to

Code Block

seq.skip (skip).take (count)

...

Produces a sequence that is a sub-sequence of the original one, with all elements for which the code passed as a parameter returns true.

Code Block

seq.where ({ => ... })
findFirst

...

Results in the first element that matches the parameter closure.

Code Block

seq.findFirst ({ => ... })

...

Results in the last element that matches the parameter closure.

Code Block

seq.findLast ({ => ... })

...

Results in a sequence consisting of elements, each of which is the result of applying the parameter function to each element of the original sequence in turn.

Code Block

seq.select ({ Type element => /* code returning single element of same or some other type */ })

...

Produces a sequence that is a concatenation of all sequences, which are all the results of applying the parameter closure to each element of the original sequence in turn. The statements skip and stop are available within the parameter closure.

Code Block

seq.selectMany ({ <Type> element => /* code returning a sequence. May utilize yield keyword.*/ })

...

Produces a sequence with all elements from the original one in the order, which corresponds to an order induced by an imaginary sequence produced by applying the selector function to each element in the original sequence in turn. The selector function can be thought of as returning a key, which is used to sort elements in a sequence. The ascending parameter controls the sort order.

Code Block

seq.sortBy ({ => /* code returning a sorting key */ }, ascending)

...

Equivalent to sortBy, unless used as a chain operation immediately following sortBy or another alsoSortBy. The result is a sequence sorted with a compound key, with the first component taken from previous sortBy or alsoSortBy (which is also a compound key), and the last component taken from this operation.

Code Block

seq.sortBy ({ => /* code returning a sorting key */ }, ascending).alsoSortBy ({ => /* code returning another key */ }, descending)

...

Produces a sequence containing all elements from the original one in the order produced by applying the comparator function (passed as a closure literal or by reference) to a list with elements from the original sequence. The ascending parameter controls the sort order (order is reversed if the value is false).

Code Block

seq.sort ({ <Type> a, <Type> b => /* code returning -1, 0 or 1 according to java.util.Comparator */}, ascending)
Binary operations

All operations in this section have the semantics of operations on multisets. This means that the elements of both the operand and the parameter sequences are taken with their respective cardinalities (that is, how many times an element appears in the sequence), and then the cardinality of matched elements in the output is the result of an arithmetic operation on these values. These operations keep the order of elements (first operand's, then the parameter's). 

intersect

Operand type

Parameter type

Result type

sequence<Type>

sequence<Type>

sequence<Type>

Produces a sequence containing elements contained both by the original sequence and the parameter sequence. More precisely, the cardinality in the output of an element appearing in both the operand and the parameter sequences, is the minimum of its cardinalities in the input. 

Code Block

seq.intersect (anotherSeq)

...

Produces a sequence containing all elements from the original sequence that are not also members of the parameter sequence. More precisely, the cardinality in the output of an element appearing in both the operand and the parameter sequences, is the maximum of 0 and the result of subtracting the parameter's cardinality from the operand's cardinality. 

Code Block

seq.except (anotherSeq)
union

...

Produces a sequence containing elements both either from the original sequence and or the one passed as a parameter. More precisely, the cardinality in the output of an element appearing in both the operand and the parameter sequences, is the maximum of its cardinalities in the input. 

Code Block

seq.union (anotherSeq)
disjunction

...

Produces exclusive disjunction (symmetric difference) of the original sequence and the one passed as a parameter. This operation equivalent to:

(operand.except(param)).concat(parameter.except(operand))
Code Block

seq.disjunction (anotherSeq)

...

Produces a sequence, which is a concatenation of the original one with the sequence passed as a parameter

Code Block

seq.concat (otherSeq)
Conversion

...

Operation reduceLeft/reduceRight applies the combinator function passed as a parameter to all elements of the sequence in turn. One of the function parameters is a sequence element, and another is the result of previous application of the function. Operation reduceLeft takes first two sequence elements and applies the function to them, then takes the result of the first application and the third sequence element, etc. Operation reduceRight does the same, but moving from the sequence's tail backwards.

Code Block

sequence<int> int = ...
int sum = in.reduceLeft({~a,~b => a + b; });
  • reduceLeft
Code Block

{Type,Type => Type} f = ... // represented as *         
sequence<Type> seq = new sequence<Type>{A,B,C,D,E,F};    A B C D E F     
seq.reduceLeft(f) == f(f(f(f(f(A,B),C),D),E),F);         \ | | | | |          
                                                          \| | | | |     
                                                          *\ | | | |    
                                                            \| | | |     
                                                            *\ | | |
                                                              \| | |
                                                              *\ | |
                                                                \| |
                                                                *\ |
                                                                  \| 
                               result of reduceLeft operation -->  * 
  • reduceRight
Code Block

{Type,Type => Type} f = ... // represented as *
sequence<Type> seq = new sequence<Type>{A,B,C,D,E,F}     A B C D E F
seq.reduceRight(f) == f(A,f(B,f(C,f(D,f(E,F))))));       | | | | | /   
                                                         | | | | |/    
                                                         | | | | /*   
                                                         | | | |/     
                                                         | | | /*
                                                         | | |/
                                                         | | /*
                                                         | |/
                                                         | /*
                                                         |/ 
                    result of reduceRight operation -->  * 

...

Operation foldLeft/foldRight behaves similarly to reduceLeft/reduceRight, with the difference that it also accepts a seed value. Also the combinator function is asymmetric (it takes a Type and a Z parameters and returns a Z value). The result of the operation is of type Z.

Code Block

sequence<int> int = ...
string concat = in.foldLeft("",{string s,~it => "" + s + it; });
  • foldLeft
Code Block

{Z,Type => Z} f = ... // represented as *       
Z z = ... // the "seed" value  
sequence<Type> seq = new sequence<Type>{A,B,C,D,E,F};    z A B C D E F     
seq.reduceLeft(f) == f(f(f(f(f(f(z,A),B),C),D),E),F);    \ | | | | | |         
                                                          \| | | | | |    
                                                          *\ | | | | |   
                                                            \| | | | |    
                                                            *\ | | | |
                                                              \| | | |
                                                              *\ | | |
                                                                \| | |
                                                                *\ | |
                                                                  \| | 
                                                                  *\ |
                                                                    \|
                                   result of foldLeft operation -->  * 
  • foldRight
Code Block

{Type,Z => Z} f = ... // represented as *    
Z z = ... // the "seed" value  
sequence<Type> seq = new sequence<Type>{A,B,C,D,E,F}     A B C D E F z
seq.foldRight(f) == f(A,f(B,f(C,f(D,f(E,f(F,z)))))));    | | | | | | / 
                                                         | | | | | |/ 
                                                         | | | | | /*   
                                                         | | | | |/    
                                                         | | | | /*   
                                                         | | | |/     
                                                         | | | /*
                                                         | | |/
                                                         | | /*
                                                         | |/
                                                         | /*
                                                         |/ 
                      result of foldRight operation -->  * 

...

This operation is only available on a sequence of strings. The result is a string that is produced by concatenating all elements with the optional separator. The default separator is " " (single space).

Code Block

seq.join ("; ");
toList

Operand type

Parameter type

Result type

sequence<Type>

none

list<Type>

Returns new list containing all the elements from the original sequence.

Code Block

seq.toList
toArray

Operand type

Parameter type

Result type

sequence<Type>

none

Type*[]*

Returns new array containing all the elements from the original sequence.

Code Block

seq.toArray