3
  • Kotlin 1.0.0
  • IDEA 2016.1

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.

melston
  • 2,198
  • 22
  • 39

1 Answers1

6

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()
Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62