Sequences

    When the processing of an Iterable includes multiple steps, they are executed eagerly: each processing step completes and returns its result – an intermediate collection. The following step executes on this collection. In turn, multi-step processing of sequences is executed lazily when possible: actual computing happens only when the result of the whole processing chain is requested.

    The order of operations execution is different as well: Sequence performs all the processing steps one-by-one for every single element. In turn, Iterable completes each step for the whole collection and then proceeds to the next step.

    So, the sequences let you avoid building results of intermediate steps, therefore improving the performance of the whole collection processing chain. However, the lazy nature of sequences adds some overhead which may be significant when processing smaller collections or doing simpler computations. Hence, you should consider both Sequence and Iterable and decide which one is better for your case.

    To create a sequence, call the sequenceOf() function listing the elements as its arguments.

    From Iterable

    If you already have an Iterable object (such as a List or a Set), you can create a sequence from it by calling asSequence().

    1. val numbersSequence = numbers.asSequence()

    One more way to create a sequence is by building it with a function that calculates its elements. To build a sequence based on a function, call with this function as an argument. Optionally, you can specify the first element as an explicit value or a result of a function call. The sequence generation stops when the provided function returns null. So, the sequence in the example below is infinite.

    1. fun main() {
    2. val oddNumbersLessThan10 = generateSequence(1) { if (it + 2 < 10) it + 2 else null }
    3. println(oddNumbersLessThan10.count())
    4. //sampleEnd
    5. }

    From chunks

    Finally, there is a function that lets you produce sequence elements one by one or by chunks of arbitrary sizes – the function. This function takes a lambda expression containing calls of yield() and functions. They return an element to the sequence consumer and suspend the execution of sequence() until the next element is requested by the consumer. yield() takes a single element as an argument; yieldAll() can take an Iterable object, an Iterator, or another Sequence. A Sequence argument of yieldAll() can be infinite. However, such a call must be the last: all subsequent calls will never be executed.

    The sequence operations can be classified into the following groups regarding their state requirements:

    • Stateless operations require no state and process each element independently, for example, map() or . Stateless operations can also require a small constant amount of state to process an element, for example, take() or drop().
    • Stateful operations require a significant amount of state, usually proportional to the number of elements in a sequence.

    If a sequence operation returns another sequence, which is produced lazily, it’s called intermediate. Otherwise, the operation is terminal. Examples of terminal operations are or . Sequence elements can be retrieved only with terminal operations.

    Sequences can be iterated multiple times; however some sequence implementations might constrain themselves to be iterated only once. That is mentioned specifically in their documentation.

    Let’s take a look at the difference between Iterable and Sequence with an example.

    Assume that you have a list of words. The code below filters the words longer than three characters and prints the lengths of first four such words.

    1. //sampleStart
    2. val words = "The quick brown fox jumps over the lazy dog".split(" ")
    3. val lengthsList = words.filter { println("filter: $it"); it.length > 3 }
    4. .map { println("length: ${it.length}"); it.length }
    5. .take(4)
    6. println("Lengths of first 4 words longer than 3 chars:")
    7. println(lengthsList)
    8. //sampleEnd

    Sequence

    Now let’s write the same with sequences:

    The output of this code shows that the filter() and map() functions are called only when building the result list. So, you first see the line of text “Lengths of..” and then the sequence processing starts. Note that for elements left after filtering, the map executes before filtering the next element. When the result size reaches 4, the processing stops because it’s the largest possible size that can return.

    The sequence processing goes like this:

    Sequences processing

    In this example, the sequence processing takes 18 steps instead of 23 steps for doing the same with lists.