V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
yazinnnn
V2EX  ›  程序员

请教一下函数式中 either monad 的使用方法

  •  
  •   yazinnnn · Jul 2, 2021 · 2009 views
    This topic created in 1760 days ago, the information mentioned may be changed or developed.
    //自定义异常
    sealed class MyE : RuntimeException()
    object AE : MyE()
    object BE : MyE()
    
    //业务方法签名
    fun a(): Either<AE, Int>
    fun b(i: Int): Either<BE, Int>
    
    方法调用
    val a: Either<AE, Int> = a()
    
    //a.map{} 之后的类型会变成 Either<AE, Either<BE, Int>>
    val either: Either<AE, Either<BE, Int>> = a.map { b(it) }
    
    

    请问有什么函数能返回 Either<MyE,Int> 吗?

    2 replies    2021-07-02 12:14:55 +08:00
    yazinnnn
        2
    yazinnnn  
    OP
       Jul 2, 2021
    @liaowang11 感谢帮助
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4019 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 10:25 · PVG 18:25 · LAX 03:25 · JFK 06:25
    ♥ Do have faith in what you're doing.