You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arrow Kotlin 1.2.0中sequence()废弃后的替代方案示例请求

Arrow Kotlin 1.2.0 废弃.sequence()后的替代实现

针对你遇到的sequence()方法废弃问题,用Arrow的Either DSL结合bind()可以轻松实现等价逻辑,以下是适配你场景的可行代码:

必要导入

import arrow.core.Either
import arrow.core.NonEmptyList
import arrow.core.raise.either

原代码的替代实现

val map: List<Either<NonEmptyList<HeroIdError>, HeroId>> = TODO()

val postSequence: Either<NonEmptyList<HeroIdError>, List<HeroId>> = either {
    map.map { it.bind() }
}

逻辑说明

  • either { ... } 启动Arrow的Either上下文,允许在块内使用bind()函数处理Either类型
  • it.bind() 会自动解包Either:
    • 若为Right(HeroId),则取出内部的HeroId继续执行
    • 若为Left(NonEmptyList<HeroIdError>),则直接将该错误作为整个Either的失败结果返回,终止后续操作
  • map.map { ... } 会将所有成功解包的HeroId收集为List<HeroId>,最终包裹在Right中返回

针对NonEmptyList输入的场景(对应官方示例)

如果你的输入是NonEmptyList类型,写法类似:

val nelMap: NonEmptyList<Either<NonEmptyList<HeroIdError>, HeroId>> = TODO()

val nelPostSequence: Either<NonEmptyList<HeroIdError>, NonEmptyList<HeroId>> = either {
    nelMap.map { it.bind() }
}

内容的提问来源于stack exchange,提问作者jpaMM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 07:35:01