0

我正在尝试学习如何使用 FreeMonads 为我的服务实现解释器。

假设我有

sealed trait ServiceAction[T] extends Product with Serializable
case class ConsumeCommand(cmd: AccruePoints) extends ServiceAction[AccruePointModel]
case class CreateEvent(evt: PointsAccruedEvent) extends ServiceAction[PointsAccruedEvent]

sealed trait LogAction[T] extends Product with Serializable
case class Info(msg: String) extends LogAction[Unit]
case class Error(msg: String) extends LogAction[Unit]

和一个动作的单子

type LogActionF[A] = Free[LogAction, A]
type ServiceActionF[A] = Free[ServiceAction, A]

接下来,我这样定义我的服务:

trait PointAccrualService {
    def consume(cmd: AccruePoints): ServiceActionF[AccruePointModel] = Free.liftF(ConsumeCommand(cmd))
    def emit(evt: PointsAccruedEvent) : ServiceActionF[PointsAccruedEvent] = Free.liftF(CreateEvent(evt))
}

trait LogService {
  def info(msg: String) : LogActionF[Unit] = Free.liftF(Info(msg))
  def error(msg: String) : LogActionF[Unit] = Free.liftF(Error(msg))
}

与每个对象

object LogService extends LogService
object PointAccrualService extends PointAccrualService

LogServiceInterpreter的是这样的:

case class LogServiceConsoleInterpreter() extends LogServiceInterpreter {
  def apply[A](action: LogActionF[A]): Task[A] = action.foldMap(handler)             

    protected def handler = new (LogAction ~> Task) {
    override def apply[A](fa: LogAction[A]) = fa match {
      case Info(m) =>
        now(info(m))
      case Error(m) =>
        now(error(m))
    }
  }

  def info(msg: String): Unit = {
    println(s"INFO: $msg")
  }

  def error(msg: String): Unit = {
    println(s"ERROR: $msg")
  }
}

同样,我PointAccuralServiceInterpreter的也是这样的:

case class PointAccuralServiceInterpreter() {
  def apply[A] (action: ServiceActionF[A]) : Task[A] = action.foldMap(handler)
  protected def handler = new (ServiceAction ~> Task) {
    override def apply[A](fa: ServiceAction[A]): Task[A] = fa match {
      case ConsumeCommand(cmd) => {
        println("Service ConsumeCommand:" + cmd)
        now(cmd)
      }
      case CreateEvent(evt) => {
        println("Service CreateEvent:" + evt)
        now(evt)
      }
    }
  }
}

我的逻辑很简单,我想记录并使用我的命令,然后创建一个事件,有点像事件源:

val ret = for {
  _ <- logService.info("Command: " + cmd)
  model <- service.consume(cmd)
  _ <- logService.info("Model: " + model)
  evt <- service.emit(model.toEvent("200", "Event Sent"))
  _ <- logService.info("Event:" + evt)
} yield evt

这段代码实际上甚至没有编译。

我应该从这里做什么?我想我应该使用 Coproduct 链接它们并通过提供我的解释器来执行这段逻辑。

我在这里找到了一些东西 https://groups.google.com/forum/#!topic/scalaz/sHxFsFpE86c

或者据说我可以使用 Shapeless 来做到这 一点在 Scala 中使用 Shapeless 折叠不同类型的列表

它们都太复杂了。我想要的是,在我定义了我的逻辑之后,我该如何执行它?

希望我在这里提供足够的详细信息以获得答案。我真的很想学这个。谢谢

4

1 回答 1

1

我稍微修改了您的代码以创建一个独立的运行示例。我还为您的问题添加了一个可能的答案,即如何按照Rúnar Bjarnason 的想法,使用 Scalaz 7.2 执行您的程序。(我在 Scalaz 中没有找到or自然变换的运算符,所以我在这里添加了它。)

我还添加了一些存根来为您的操作提供一些可以摆弄的东西,并简化您对内部处理程序的服务(因为我必须为两种语言组合创建一个新服务)。此外,我将您更改Task.now{...}Task{...}创建一个异步任务,该任务在最后一行代码中执行。

这是完整的代码:

import scala.language.{higherKinds, implicitConversions}

import scalaz._
import scalaz.concurrent.Task

/* Stubs */

case class AccruePoints()
case class AccruePointModel(cmd: AccruePoints) {
  def toEvent(code: String, description: String): PointsAccruedEvent = PointsAccruedEvent(code, description)
}
case class PointsAccruedEvent(code: String, description: String)

/* Actions */

sealed trait ServiceAction[T] extends Product with Serializable

case class ConsumeCommand(cmd: AccruePoints) extends ServiceAction[AccruePointModel]
case class CreateEvent(evt: PointsAccruedEvent) extends ServiceAction[PointsAccruedEvent]

sealed trait LogAction[T] extends Product with Serializable

case class Info(msg: String) extends LogAction[Unit]
case class Error(msg: String) extends LogAction[Unit]

/* Handlers */

object PointAccuralServiceHandler extends (ServiceAction ~> Task) {
  override def apply[A](fa: ServiceAction[A]): Task[A] = fa match {
    case ConsumeCommand(cmd) => {
      println("Service ConsumeCommand:" + cmd)
      Task(consume(cmd))
    }
    case CreateEvent(evt) => {
      println("Service CreateEvent:" + evt)
      Task(evt)
    }
  }

  def consume(cmd: AccruePoints): AccruePointModel =
    AccruePointModel(cmd)
}

case object LogServiceConsoleHandler extends (LogAction ~> Task) {
  override def apply[A](fa: LogAction[A]): Task[A] = fa match {
    case Info(m) =>
      Task(info(m))
    case Error(m) =>
      Task(error(m))
  }

  def info(msg: String): Unit = {
    println(s"INFO: $msg")
  }

  def error(msg: String): Unit = {
    println(s"ERROR: $msg")
  }
}

/* Execution */

class Service[F[_]](implicit I1: Inject[ServiceAction, F], I2: Inject[LogAction, F]) {
  def consume(cmd: AccruePoints): Free[F, AccruePointModel] = Free.liftF(I1(ConsumeCommand(cmd)))

  def emit(evt: PointsAccruedEvent): Free[F, PointsAccruedEvent] = Free.liftF(I1(CreateEvent(evt)))

  def info(msg: String): Free[F, Unit] = Free.liftF(I2(Info(msg)))

  def error(msg: String): Free[F, Unit] = Free.liftF(I2(Error(msg)))
}

object Service {
  implicit def instance[F[_]](implicit I1: Inject[ServiceAction, F], I2: Inject[LogAction, F]) = new Service[F]
}

def prg[F[_]](implicit service: Service[F]) = {
  val cmd = AccruePoints()
  for {
    _ <- service.info("Command: " + cmd)
    model <- service.consume(cmd)
    _ <- service.info("Model: " + model)
    evt <- service.emit(model.toEvent("200", "Event Sent"))
    _ <- service.info("Event:" + evt)
  } yield evt
}

type App[A] = Coproduct[ServiceAction, LogAction, A]

def or[F[_], G[_], H[_]](f: F ~> H, g: G ~> H) =
  new (({type t[x] = Coproduct[F, G, x]})#t ~> H) {
    override def apply[A](c: Coproduct[F, G, A]): H[A] = c.run match {
      case -\/(fa) => f(fa)
      case \/-(ga) => g(ga)
    }
  }

val app = prg[App]

val ret = app.foldMap(or(PointAccuralServiceHandler, LogServiceConsoleHandler))
ret.unsafePerformSync
于 2017-01-04T20:13:48.483 回答