Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin sequence function unresolved reference

  • 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.

like image 309
melston Avatar asked Feb 28 '26 13:02

melston


1 Answers

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()
like image 155
Vladimir Mironov Avatar answered Mar 03 '26 04:03

Vladimir Mironov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!