A nonempty, immutable sequence of values. The type Sequence<Element> may be abbreviated [Element+].

Given a possibly-empty sequence of type [Element*], the if (nonempty ...) construct, or, alternatively, assert (nonempty ...), may be used to narrow to a sequence type to a nonempty sequence type:

[Integer*] nums = ... ;
if (nonempty nums) {
    Integer first = nums.first;
    Integer max = max(nums);
    [Integer+] squares = nums.collect((Integer i) => i**2));
    [Integer+] sorted = nums.sort(byIncreasing((Integer i) => i));
}

Operations like first, max(), collect(), and sort(), which polymorphically produce a nonempty or non-null output when given a nonempty input are called emptiness-preserving.

Sequence has the following subtypes:

By: Gavin

no type hierarchy

Attributes
emptySource Codeshared actual Boolean empty

Returns false, since every Sequence contains at least one element.

firstSource Codeshared formal Element first

The first element of the sequence, that is, the element with index 0.

Refines List.first ultimately refines Iterable.first
keysSource Codeshared actual default [Integer+] keys

A nonempty sequence containing all indexes of this sequence.

Refines Sequential.keys ultimately refines Correspondence.keys
lastSource Codeshared formal Element last

The last element of the sequence, that is, the element with index sequence.lastIndex.

Refines List.last ultimately refines Iterable.last
lastIndexSource Codeshared actual default Integer lastIndex

The index of the last element of the sequence.

See also size
restSource Codeshared formal Element[] rest

The rest of the sequence, without the first element.

Refines Sequential.rest ultimately refines Iterable.rest
reversedSource Codeshared actual default [Element+] reversed

A list containing the elements of this list in reverse order to the order in which they occur in this list. For every index of a reversed list:

list.reversed[index]==list[size-1-index]

This is a lazy operation returning a view of this list.

Refines Sequential.reversed ultimately refines List.reversed
sizeSource Codeshared formal Integer size

The number of elements returned by the Iterable.iterator() of this stream, if the iterator terminates. In the case of an infinite stream, this operation never terminates.

Refines List.size ultimately refines Iterable.size
stringSource Codeshared actual default String string

A developer-friendly string representing the instance. Concatenates the name of the concrete class of the instance with the hash of the instance. Subclasses are encouraged to refine this implementation to produce a more meaningful representation.

Refines Sequential.string ultimately refines Object.string
Inherited Attributes
Attributes inherited from: Object
Attributes inherited from: Collection<Element>
Attributes inherited from: Correspondence<Key,Item>
Attributes inherited from: Iterable<Element,Absent>
Attributes inherited from: List<Element>
Attributes inherited from: Element[]
Methods
appendSource Codeshared actual default [<Element|Other>+] append<Other>(Other[] elements)

Return a nonempty sequence containing the elements of this sequence, followed by the given elements.

cloneSource Codeshared actual default [Element+] clone()

This nonempty sequence.

Refines Sequential.clone ultimately refines Collection.clone
collectSource Codeshared actual default [Result+] collect<Result>(Result collecting(Element element))

A nonempty sequence containing the results of applying the given mapping to the elements of this sequence.

Parameters:
  • collecting

    The transformation applied to the elements.

Refines List.collect ultimately refines Iterable.collect
containsSource Codeshared actual default Boolean contains(Object element)

Returns true if the given value belongs to this Category, that is, if it is an element of this Category, or false otherwise.

For most Categorys the following relationship is satisfied by every pair of elements x and y:

  • if x==y, then x in category == y in category

However, it is possible to form a useful Category consistent with some other equivalence relation, for example ===. Therefore implementations of contains() which do not satisfy this relationship are tolerated.

Refines Iterable.contains ultimately refines Category.contains
findSource Codeshared actual default Element? find(Boolean selecting(Element&Object elem))

The first element of this stream which satisfies the given predicate function, if any, or null otherwise. For an infinite stream, this method might not terminate.

For example, the expression

(-10..10).find(Integer.positive)

evaluates to 1.

Refines List.find ultimately refines Iterable.find
findLastSource Codeshared actual default Element? findLast(Boolean selecting(Element&Object elem))

The last element of this stream which satisfies the given predicate function, if any, or null otherwise. For an infinite stream, this method might not terminate.

For example, the expression

(-10..10).findLast(3.divides)

evaluates to 9.

Refines List.findLast ultimately refines Iterable.findLast
longerThanSource Codeshared actual default Boolean longerThan(Integer length)

Determines if this stream has more elements than the given length. This is an efficient operation for streams with many elements.

Refines List.longerThan ultimately refines Iterable.longerThan
measureSource Codeshared actual default Element[] measure(Integer from, Integer length)

Obtain a measure containing the mapped values starting from the given starting index, with the given length. If length<=0, the resulting measure is empty.

The measure should contain the given number of elements of this stream, starting from the element at the given starting index, in the same order as they are produced by the Iterable.iterator() of the stream. In the case where the iterator would be exhausted before length elements are produced, the resulting measure contains only those elements which were produced before the iterator was exhausted, and the length of the measure is less then the given length.

When the given index does not belong to this ranged object, the behavior is implementation dependent.

Refines List.measure ultimately refines Ranged.measure
prependSource Codeshared actual default [<Element|Other>+] prepend<Other>(Other[] elements)

Return a nonempty sequence containing the given elements, followed by the elements of this sequence.

repeatSource Codeshared actual default Element[] repeat(Integer times)

Produces a stream formed by repeating the elements of this stream the given number of times, or an empty stream if times<=0.

For example, the expression

{ 1, 2 }.repeat(3)

evaluates to the stream { 1, 2, 1, 2, 1, 2 }.

Refines Sequential.repeat ultimately refines Iterable.repeat
sequenceSource Codeshared actual default [Element+] sequence()

This nonempty sequence.

Refines Sequential.sequence ultimately refines Iterable.sequence
shorterThanSource Codeshared actual default Boolean shorterThan(Integer length)

Determines if this stream has fewer elements than the given length. This is an efficient operation for streams with many elements.

Refines List.shorterThan ultimately refines Iterable.shorterThan
sliceSource Codeshared actual default [Element[], Element[]] slice(Integer index)

Return two lists, the first containing the elements that occur before the given index, the second with the elements that occur after the given index. If the given index is outside the range of indices of this list, one of the returned lists will be empty.

For any list, and for any integer index:

list.slice(index) == [list[...index-1], list[index...]]

This is an eager operation.

Refines Sequential.slice ultimately refines List.slice
sortSource Codeshared actual default [Element+] sort(Comparison comparing(Element x, Element y))

A nonempty sequence containing the elements of this container, sorted according to a function imposing a partial order upon the elements.

Parameters:
  • comparing

    The function comparing pairs of elements.

spanSource Codeshared actual default Element[] span(Integer from, Integer to)

Obtain a span containing the elements between the two given indices.

The span should contain elements of this stream, starting from the element at the given starting index, and ending with the element at the given ending index, in the same order as they are produced by the Iterable.iterator() of the stream, except when the ending index occurs earlier than the starting index, in which case they occur in the opposite order.

When one or both of the given indices does not belong to this ranged stream, the behavior is implementation dependent.

Refines List.span ultimately refines Ranged.span
spanFromSource Codeshared actual default Element[] spanFrom(Integer from)

Obtain a span containing the elements between the given starting index and the last index of this ranged object.

The span should contain elements of this stream, starting from the element at the given starting index, in the same order as they are produced by the Iterable.iterator() of the stream.

When the given index does not belong to this ranged stream, the behavior is implementation dependent.

Refines List.spanFrom ultimately refines Ranged.spanFrom
spanToSource Codeshared actual default Element[] spanTo(Integer to)

Obtain a span containing the elements between the first index of this ranged stream and given end index.

The span should contain elements of this stream, up to the element at the given ending index, in the same order as they are produced by the Iterable.iterator() of the stream.

When the given index does not belong to this ranged stream, the behavior is implementation dependent.

Refines List.spanTo ultimately refines Ranged.spanTo
withLeadingSource Codeshared actual default [Other, Element+] withLeading<Other>(Other element)

Return a nonempty sequence containing the given element, followed by the elements of this sequence.

withTrailingSource Codeshared actual default [<Element|Other>+] withTrailing<Other>(Other element)

Return a nonempty sequence containing the elements of this sequence, followed by the given element.

Inherited Methods
Methods inherited from: Object
Methods inherited from: Category<Element>
Methods inherited from: Collection<Element>
Methods inherited from: Correspondence<Key,Item>
Methods inherited from: Iterable<Element,Absent>
Methods inherited from: List<Element>
Methods inherited from: Ranged<Index,Element,Subrange>
Methods inherited from: Element[]