В продолжение этого вопроса Мне нужно обработать все результаты, собранные в processResult
. Но когда я запускаю код, я получаю <function1>
из println(finalBusinessResult)
Редактировать: Кажется, обрабатывает шаг 1 один и ничего больше
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)
}