作为对此question的跟踪,我需要处理processResult
中收集的所有结果。但是,当我运行代码时,我从<function1>
那里得到了println(finalBusinessResult)
编辑:似乎只对step1进行了处理
Q:如何从processResult
返回结果
case class BusinessState()
trait BusinessResult
case object KeepGoing extends BusinessResult
case object StopNow extends BusinessResult
type IOState[S, A] = StateT[IO, S, A]
type BusinessIOState[A] = IOState[BusinessState, A]
trait SomeSteps {
def step1: BusinessIOState[Unit]
def step2: BusinessIOState[BusinessState]
def decisionStep: BusinessIOState[BusinessResult]
def step3: BusinessIOState[BusinessResult]
def step4: BusinessIOState[BusinessResult]
def processResult(businessResult: BusinessResult*): BusinessIOState[BusinessResult]
def toOpt(a: BusinessIOState[BusinessResult])
: OptionT[BusinessIOState, BusinessResult] = {
OptionT.liftF(a).filter(_ == KeepGoing)
}
def program: BusinessIOState[Unit] = (for {
_ <- step1
_ <- step2
businessResult <- (for {
businessResult1 <- toOpt(decisionStep)
businessResult2 <- toOpt(step3)
businessResult3 <- toOpt(step4)
} yield processResult(businessResult1, businessResult2, businessResult3)).getOrElse(())
} yield businessResult)
}
object Impl extends SomeSteps {
def step1 = Monad[BusinessIOState].unit
def step2 = Monad[BusinessIOState].pure(BusinessState())
def decisionStep = StateT.liftF(IO { println("dS"); KeepGoing })
def step3 = StateT.liftF(IO { println("3"); KeepGoing })
def step4 = StateT.liftF(IO { println("4"); KeepGoing })
def processResult(businessResult: BusinessResult*): BusinessIOState[BusinessResult] =
StateT.liftF(IO { println("5"); KeepGoing })
}
def run(args: List[String]) = {
val finalBusinessResult = for {
businessResult <- Impl.program.runA(BusinessState())
} yield businessResult
println(finalBusinessResult)
IO.pure(ExitCode.Success)
}
答案 0 :(得分:2)
借助这些响应,我设法使其正常工作。
object StopIO extends IOApp {
case class BusinessState()
trait BusinessResult
case object KeepGoing extends BusinessResult
case object StopNow extends BusinessResult
type IOState[S, A] = StateT[IO, S, A]
type BusinessIOState[A] = IOState[BusinessState, A]
trait SomeSteps {
def step1: BusinessIOState[Unit]
def step2: BusinessIOState[Unit]
def decisionStep: BusinessIOState[BusinessResult]
def step3: BusinessIOState[BusinessResult]
def step4: BusinessIOState[BusinessResult]
def processResult(businessResult: BusinessResult*): BusinessIOState[BusinessResult]
def toOpt(a: BusinessIOState[BusinessResult]): OptionT[BusinessIOState, BusinessResult] = {
OptionT.liftF(a).filter(_ == KeepGoing)
}
def program = (for {
_ <- step1
_ <- step2
businessResult <- (for {
businessResult1 <- toOpt(decisionStep)
businessResult2 <- toOpt(step3)
businessResult3 <- toOpt(step4)
businessResult4 <- toOpt(processResult(businessResult1, businessResult2, businessResult3))
} yield businessResult4).getOrElse(KeepGoing)
} yield businessResult)
}
object Impl extends SomeSteps {
def step1 = Monad[BusinessIOState].unit
def step2 = Monad[BusinessIOState].pure(KeepGoing)
def decisionStep = StateT.liftF(IO { println("dS"); KeepGoing })
def step3 = StateT.liftF(IO { println("3"); KeepGoing })
def step4 = StateT.liftF(IO { println("4"); KeepGoing })
def processResult(businessResult: BusinessResult*) = StateT.liftF(IO { println("5"); KeepGoing })
}
def run(args: List[String]) = {
val ioResult = for {
businessResult <- Impl.program.runA(BusinessState())
} yield businessResult
println(ioResult.unsafeRunSync())
IO.pure(ExitCode.Success)
}
}