News for hackers. Stuff that matters.
Re:Kotlin for Data Analysis by rbehrends on Wednesday August 28, 2024 @11:16PM (#41386922)
> the 'yield' keyword

Am I missing something here?

  $ cat fib.kts
  fun fib() = sequence {
    var a = 1; var b = 1
    while (true) {
      yield(a)
      a = b.also { b += a }
    }
  }

  println(fib().take(10).joinToString(" -> "))
  println(fib().first { it >= 50 })
  $ kotlin fib.kts
  1 -> 1 -> 2 -> 3 -> 5 -> 8 -> 13 -> 21 -> 34 -> 55
  55
Of course, yield() is a function in Kotlin, not a keyword, but the same functionality is there.
Re:Kotlin for Data Analysis by yen223 on Thursday August 29, 2024 @01:03AM (#41387517)
The mechanism in Kotlin that allows them to limit yield() to a sequence{} block , without introducing a new keyword, is pretty dang cool.
Re:Kotlin for Data Analysis by rbehrends on Thursday August 29, 2024 @04:51AM (#41388711)
What happens under the hood is that a `sequence {}` call creates an instance of `SequenceScope`, which has `yield()` and `yieldAll()` methods. When executing the block, `this` will reference that particular instance and `yield()` is essentially `this.yield()` and will call the method on the scope instance.

The actual functionality is then provided by the coroutine system, though a lot of the heavy lifting is done by the optimizer to eliminate all or most of the runtime overhead.

Re:Kotlin for Data Analysis by poikroequ on Thursday August 29, 2024 @07:16AM (#41389601)
I had no idea Kotlin could do this. I never thought to check. I guess with Kotlin being a JVM language, I simply assumed it wasn't possible.

Thank you for providing an example because I was a little confused by a couple other comments mentioning yield...