Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iteration in Scala vs. Kotlin

I’m migrating some code from Scala to Kotlin, I observe different behaviour:

Scala:

var i = 0

Iterator.continually{
  println(s"i=$i")
  i += 1
  i
}.takeWhile { 
  _ < 3
}.foreach { i =>
  println(s"I=$i")
}

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


Console (F3)

i=0
I=1
i=1
I=2
i=2

Kotlin equivalent:

fun <A> continousIterator(func: () -> A): Iterable<A> =
    object: Iterable<A> {
        override fun iterator() =
            object: Iterator<A> {
                override fun hasNext(): Boolean = true
                override fun next(): A = func()
            }
    }
    
var i = 0;
    
fun main() {
    continousIterator{
        println("i=$i")
        i += 1
        i
    }.takeWhile{
      it < 3
    }.forEach { 
      println("I=$it")
    }
}

Output:

i=0
i=1
i=2
I=1
I=2

When we have state, the result isn’t the same, the calling order of func() and the iterator are different.

I wonder why.

>Solution :

When takeWhile is called, takeWhile iterates through the sequence immediately and prints 0 to 2, and produces a List. Only after that does the forEach get run, printing 1 and 2.

To replicate the Scala behaviour, you need forEach to consume the sequence, instead of takeWhile. A Sequence can do this:

generateSequence {
    println("i=$i")
    i += 1
    i
}.takeWhile{it < 3}.forEach { println("I=$it")}

Operations on a Sequence are all lazy. They don’t consume the sequence unless absolutely necessary.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading