我想要实现的是可序列化和可组合的过程描述符。基本上我将创建一些原始CREATE TABLE tbl_Customer_Vehicle
(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT fk_vehicle_id_PlateNo FOREIGN KEY(vehicle_id,PlateNo)
references tbl_Vehicle(vehicle_id,PlateNo)
);
(可序列化),然后我希望能够将它们组合成更高阶Processor
,然后整个事物应该保持自动序列化。这是我当前的实现,但我怀疑有一些更优雅的方式来做一些猫类型类/数据结构。我感到愚蠢,我无法想办法利用Free,Kleisli或State这些强大的工具。我的挑战是我的状态类型,即Processor
中的数据字段,不断变化。
但必须有办法克服这不是吗?
DataWithContext
答案 0 :(得分:3)
我有:
Since in **Carving**, edges are allowed to go to a single vertex in the grapdhparenet node as well.
Processor.process
。Kleisli
和zip
方法移至>>
特征本身。结果是:
Processor
可以用作:
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import cats.implicits._
import cats.data.{XorT, Kleisli}
import cats.Apply
type Cause = String
type Result[A] = XorT[Future, Cause, A]
type Process[A, B] = Kleisli[Result, DataWithContext[A], B]
case class DataWithContext[+A](data: A, context: List[String])
implicit class ContextOps[A](a: A) {
def withContext(ctx: List[String]) = DataWithContext(a, ctx)
}
trait Processor[A, B] extends Serializable { self =>
val process: Process[A, B]
def andThen[C](that: Processor[B, C]): Processor[A, C] =
Processor.instance(Kleisli { dc =>
(process.map(_.withContext(dc.context)) andThen that.process).run(dc)
})
// alias for andThen
def >>[C](that: Processor[B, C]) = this andThen that
def zip[C](that: Processor[A, C]): Processor[A, (B, C)] =
Processor.instance(Kleisli { dc =>
Apply[Result].tuple2(self.process.run(dc), that.process.run(dc))
})
}
object Processor {
// create a Processor from a Process
def instance[A, B](p: Process[A, B]) = new Processor[A, B] {
val process = p
}
}