I have found a couple of references to the new sequence function used to create a sequence (no longer called stream). The JetBrains blog gives the following examples:
val elements = sequence(1, { x -> x + 1})
val elements = listOf(1, 2, 3, 4).sequence()
The AgileWombat blog gives similar examples.
val squares = sequence(1) {it + 1}.map {it * it}
However, when I try any of these examples, either in the REPL or in the IDE (IDEA 2016.1), I get the following:
>>> val squares = sequence(1) {it + 1}.map {it * it}
error: unresolved reference: sequence
val squares = sequence(1) {it + 1}.map {it * it}
^
error: unresolved reference: it
val squares = sequence(1) {it + 1}.map {it * it}
^
I have the latest plugin for the IDE and the latest kotlin package downloaded. So I must be doing something wrong.
This function used to be named sequence but the name was changed to generateSequence starting from 1.0.0 release. Iterable<T>.sequence was renamed to Iterable<T>.asSequence as well:
val elements = generateSequence(1) { x -> x + 1 }
val elements = listOf(1, 2, 3, 4).asSequence()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With