Возврат окончательного результата из вложенной обработки моно-состояний State-IO и IO - PullRequest
0 голосов
/ 17 июня 2019

В продолжение этого вопроса Мне нужно обработать все результаты, собранные в 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)
  }

1 Ответ

2 голосов
/ 18 июня 2019

С помощью этих ответов мне удалось заставить его работать правильно.

  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)
  }
}
...