1

我有一个免费的 monad 可以满足我的要求:

type FreeOperation[F] = Free[Operation, F]

sealed trait Operation[O]
case object IdentityOperation extends Operation[GraphTraversal[_, _]]
case class LabelOperation(label: String) extends Operation[GraphTraversal[_, Vertex]]
case class HasOperation(has: String, within: List[_]) extends Operation[GraphTraversal[_, Vertex]]
case class InOperation(in: String) extends Operation[GraphTraversal[_, Vertex]]
case class OutOperation(out: String) extends Operation[GraphTraversal[_, Vertex]]
case class InEdgeOperation(inEdge: String) extends Operation[GraphTraversal[_, Edge]]
case class OutEdgeOperation(outEdge: String) extends Operation[GraphTraversal[_, Edge]]
case class InVertexOperation(inVertex: String) extends Operation[GraphTraversal[_, Vertex]]
case class OutVertexOperation(outVertex: String) extends Operation[GraphTraversal[_, Vertex]]
case class AsOperation(as: String) extends Operation[GraphTraversal[_, _]]
case class SelectOperation(select: List[String]) extends Operation[GraphTraversal[_, _]]

object Operation {
  def identity: FreeOperation[GraphTraversal[_, _]] = Free.liftF(IdentityOperation)
  def label(v: String): FreeOperation[GraphTraversal[_, Vertex]] = Free.liftF(LabelOperation(v))
  def has(h: String, w: List[_]): FreeOperation[GraphTraversal[_, Vertex]] = Free.liftF(HasOperation(h, w))
  def in(i: String): FreeOperation[GraphTraversal[_, Vertex]] = Free.liftF(InOperation(i))
  def out(o: String): FreeOperation[GraphTraversal[_, Vertex]] = Free.liftF(OutOperation(o))
  def inEdge(ie: String): FreeOperation[GraphTraversal[_, Edge]] = Free.liftF(InEdgeOperation(ie))
  def outEdge(oe: String): FreeOperation[GraphTraversal[_, Edge]] = Free.liftF(OutEdgeOperation(oe))
  def inVertex(iv: String): FreeOperation[GraphTraversal[_, Vertex]] = Free.liftF(InVertexOperation(iv))
  def outVertex(ov: String): FreeOperation[GraphTraversal[_, Vertex]] = Free.liftF(OutVertexOperation(ov))
  def as(a: String): FreeOperation[GraphTraversal[_, _]] = Free.liftF(AsOperation(a))
  def select(s: List[String]): FreeOperation[GraphTraversal[_, _]] = Free.liftF(SelectOperation(s))
}

def operationInterpreter(traversal: GraphTraversal[_, _]): (Operation ~> Id) =
  new (Operation ~> Id) {
    def apply[A](input: Operation[A]): Id[A] =
      input match {
        case IdentityOperation => traversal.asInstanceOf[A]
        case LabelOperation(label) => traversal.hasLabel(label).asInstanceOf[A]
        case HasOperation(has, within) => traversal.has(has, P.within(within: _*)).asInstanceOf[A]
        case InOperation(in) => traversal.in(in).asInstanceOf[A]
        case OutOperation(out) => traversal.out(out).asInstanceOf[A]
        case InEdgeOperation(inEdge) => traversal.inE(inEdge).asInstanceOf[A]
        case OutEdgeOperation(outEdge) => traversal.outE(outEdge).asInstanceOf[A]
        case InVertexOperation(inVertex) => traversal.inV().asInstanceOf[A]
        case OutVertexOperation(outVertex) => traversal.outV().asInstanceOf[A]
        case AsOperation(as) => traversal.as(as).asInstanceOf[A]
        case SelectOperation(select) => {
          if (select.isEmpty) {
            traversal
          } else if (select.size == 1) {
            traversal.select[Any](select.head).asInstanceOf[A]
          } else {
            traversal.select[Any](select.head, select.tail.head, select.tail.tail: _*)
          }
        }
      }
  }

我可以这样称呼它来创建一个程序:

def selectQuery: Free[Operation, GraphTraversal[_, _]] =
  for {
    _ <- label("person")
    _ <- as("people")
    _ <- outEdge("created")
    _ <- has("weight", List(1.0))
    _ <- inVertex("software")
    _ <- as("software")
    x <- select(List("people", "software"))
  } yield x

val traversal = graph.traversal.V()
val result = selectQuery.foldMap(operationInterpreter(traversal))

凉爽的!但问题是:

我有一个List[Operation]我想翻译成这种单子结构的东西……我该怎么做?

def composeQuery(query: List[Operation[_]]): FreeOperation[GraphTraversal[_, _]] = {
  query.foldLeft(???) (??????)
}

List将我的 of翻译Operations成我可以通过我的解释器的自由单子的正确方法是什么?

4

1 回答 1

1

映射Free.liftF集合,创建一个单子列表,然后使用单子sequence函数(实际上sequenceU正如您所指出的那样)按顺序组成单子。

于 2016-12-04T20:25:53.957 回答