V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yazinnnn
V2EX  ›  程序员

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

  •  
  •   yazinnnn · 2021-07-02 11:21:25 +08:00 · 1206 次点击
    这是一个创建于 1000 天前的主题,其中的信息可能已经有所发展或是发生改变。
    //自定义异常
    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 条回复    2021-07-02 12:14:55 +08:00
    yazinnnn
        2
    yazinnnn  
    OP
       2021-07-02 12:14:55 +08:00
    @liaowang11 感谢帮助
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   959 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:42 · PVG 05:42 · LAX 14:42 · JFK 17:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.