代码之家  ›  专栏  ›  技术社区  ›  Wonay

Scalaz:绕过IO管道?

  •  0
  • Wonay  · 技术社区  · 7 年前

     output1 <- step1(...)
     output2 <- step2(....)
     ....
    

    我想做的是为了一个特定的目标 output1 ,我不想执行 step2 就这么过去吧。是否有一个内置的功能部分 scalaz.zio.IO ?

    除了我,我什么也找不到 flatMap :

     step1(...).flatMap {
          case ... => step2(....)
          case ... => step3(...)
     }
    

    步骤二

    我也可以使用 Left 部分 IO Exception 和错误跟踪。

    将 IO[Error, IO[ByPassCondition, ResultForStep2]] ResultForStep2 如果 ByPassCondition 设置为。。。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Wonay    7 年前

    我找到了一个解决方案:

    implicit class JumpIO[A, B](io: IO[A, (B, Boolean)]) {
      def mightJumpNext(next: B => IO[A, B]): IO[A, B] = {
        io.flatMap { case (b, test) =>
          if (test) {
            io.map { case (bb, _) => bb }
          } else {
            io.flatMap { case (bb, _) => next(bb) }
          }
        }
      }
    
      def mightJumpNextChain(next: B => IO[A, (B, Boolean)]): IO[A, (B, Boolean)] = {
        io.flatMap { case (_, test) =>
          if (test) {
            io.map { case (bb, _) => (bb, false) }
          } else {
            io.flatMap { case (bb, _) => next(bb) }
          }
        }
      }
    }
    

    然后你可以做:

      io
        .mightJumpNext(b=> step2(b))
        .mightJumpNext(b => step3(b))
        ...