https://github.com/y9san9/kotlin-sequence-builder
Kotlin Sequence Builder oversimplified implementation
https://github.com/y9san9/kotlin-sequence-builder
Last synced: about 2 months ago
JSON representation
Kotlin Sequence Builder oversimplified implementation
- Host: GitHub
- URL: https://github.com/y9san9/kotlin-sequence-builder
- Owner: y9san9
- Created: 2025-04-04T16:26:26.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-04-04T16:27:44.000Z (about 2 months ago)
- Last Synced: 2025-04-04T17:30:11.008Z (about 2 months ago)
- Language: Kotlin
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Kotlin Sequence Builder
## Why?
Kotlin Sequences use `kotlin.coroutines` inside sequence builder:
```kotlin
val sequence = sequence {
var n = 0
while (true) {
yield(i++) // This is SUSPEND function
}
}
```However, you can easily access sequence itself without any suspence:
```kotlin
fun main() {
val sequence = /* ... */
sequence.forEach { element -> // forEach is NOT SUSPEND function
println(element)
}
}
```So, this breaks Kotlin, doesn't it? How can we call suspend function `emit`
from non-suspend function `forEach`?And we can't even use `launch` to start coroutine since it lives inside
`kotlinx.coroutines`, while sequence is inside the kotlin stdlib.How can we even use coroutines in Kotlin **without** `kotlinx.coroutines`?
This repo is the answer to this question. It implements `MySequence` class which
is a simplified implementation of how the real sequence works.## Where?
Basically this file: [MySequence.kt](src/main/kotlin/MySequence.kt)
## Run
Use Gradle for this: `./gradlew run`
This will run [Main.kt](src/main/kotlin/Main.kt)