W3cubDocs

/Kotlin

generateSequence

fun <T : Any> generateSequence(
    nextFunction: () -> T?
): Sequence<T>

Returns a sequence which invokes the function to calculate the next value on each iteration until the function returns null.

The returned sequence is constrained to be iterated only once.

See Also

constrainOnce

fun <T : Any> generateSequence(
    seed: T?, 
    nextFunction: (T) -> T?
): Sequence<T>

Returns a sequence defined by the starting value seed and the function nextFunction, which is invoked to calculate the next value based on the previous one on each iteration.

The sequence produces values until it encounters first null value. If seed is null, an empty sequence is produced.

The sequence can be iterated multiple times, each time starting with seed.

fun <T : Any> generateSequence(
    seedFunction: () -> T?, 
    nextFunction: (T) -> T?
): Sequence<T>

Returns a sequence defined by the function seedFunction, which is invoked to produce the starting value, and the nextFunction, which is invoked to calculate the next value based on the previous one on each iteration.

The sequence produces values until it encounters first null value. If seedFunction returns null, an empty sequence is produced.

The sequence can be iterated multiple times.

© 2010–2017 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/generate-sequence.html