【Coroutines】Implement Python Generator by Kotlin Coroutines

Expected

a generator object that can generate sequence objects

and only generate object when consumer object request for it

kotlin 复制代码
fun main() {
    val generator = generator {
        yield(100)
        yield(200)
        yield(300)
    }
    while (generator.hasNext()) {
        val value = generator.await()
        println(value)
    }
}
Implementation
kotlin 复制代码
package com.code.kotlin

import kotlin.coroutines.*

@RestrictsSuspension
interface GeneratorScope<T> {

    suspend fun yield(value: T)
}

interface GeneratorIterator<T> : Iterator<T> {

    fun await(): T
}

internal interface Generator<T> : GeneratorScope<T>, GeneratorIterator<T>, Continuation<Any>

internal sealed class GeneratorState<T> {
    class WAITING<T>(val continuation: Continuation<Unit>) : GeneratorState<T>()
    class READY<T>(val continuation: Continuation<Unit>, val value: T) : GeneratorState<T>()
    class COMPLETED<T> : GeneratorState<T>()
}

internal class GeneratorImpl<T> : Generator<T> {

    override val context: CoroutineContext = EmptyCoroutineContext

    internal lateinit var state: GeneratorState<T>

    override suspend fun yield(value: T) {
        suspendCoroutine { continuation ->
            when (val _state = state) {
                is GeneratorState.WAITING -> {
                    state = GeneratorState.READY(continuation, value)
                }
                is GeneratorState.READY,
                is GeneratorState.COMPLETED -> throw IllegalStateException()
            }
        }
    }

    override fun resumeWith(result: Result<Any>) {
        state = GeneratorState.COMPLETED()
    }

    private fun resume() {
        when (val _state = state) {
            is GeneratorState.WAITING -> {
                _state.continuation.resume(Unit)
            }
            else -> {}
        }
    }

    override fun hasNext(): Boolean {
        resume()
        return state !is GeneratorState.COMPLETED
    }

    override fun next(): T {
        when (val _state = state) {
            is GeneratorState.READY -> {
                state = GeneratorState.WAITING(_state.continuation)
                return _state.value
            }
            is GeneratorState.WAITING,
            is GeneratorState.COMPLETED -> throw IllegalStateException()
        }
    }

    override fun await() = next()
}

fun <T> generator(
    block: suspend GeneratorScope<T>.() -> Unit
): GeneratorIterator<T> {
    val generator = GeneratorImpl<T>()
    val continuation = block.createCoroutine(generator, generator)
    generator.state = GeneratorState.WAITING(continuation)
    return generator
}
相关推荐
float_六七8 分钟前
Apache Commons Lang 3
开发语言·python·apache
栒U9 分钟前
从高层 PyTorch 到中层 CUDA Kernel 到底层硬件 Tensor Core
人工智能·pytorch·python
精灵vector44 分钟前
基于视觉的网页浏览Langraph Agent
python·aigc·ai编程
AiPy_极客团长1 小时前
AI解决生活小事系列——用AI给我的电脑做一次“深度体检”
人工智能·python·电脑维修·实战技巧
小王爱学人工智能1 小时前
神经网络模型搭建及手写数字识别案例
pytorch·python·神经网络
Go 鹏ya1 小时前
【Python学习笔记】whl包打包
笔记·python·学习
这里有鱼汤2 小时前
跟风还是反转?用 ADX 判断趋势强度,Python 教程全程带你飞
python
大模型真好玩2 小时前
深入浅出LangGraph AI Agent智能体开发教程(一)—全面认识LangGraph
人工智能·python·mcp
乘乘凉5 小时前
Python中函数的闭包和装饰器
前端·数据库·python
爱隐身的官人9 小时前
爬虫基础学习-爬取网页项目(二)
前端·爬虫·python·学习