我想你应该写两个口译员
PathOps ~> Free[Fs, ?]
和
Fs ~> IO
,然后将它们组合成单个解释器
PathOps ~> IO
。
下面是一个可编译的示例。以下是我在本例中使用的所有导入:
import cats.~>
import cats.free.Free
import cats.free.Free.liftF
下面是的模拟实现
IO
还有你的代数:
// just for this example
type IO[X] = X
object IO {
def apply[A](a: A): IO[A] = a
}
sealed trait Fs[A]
case class Ls(path: String) extends Fs[Seq[String]]
case class Cp(from: String, to: String) extends Fs[Unit]
type FreeFs[A] = Free[Fs, A]
def ls(path: String) = Free.liftF(Ls(path))
def cp(from: String, to: String) = Free.liftF(Cp(from, to))
这是翻译
Fs ~>IO
从代码中复制:
def fsToIoInterpreter = new (Fs ~> IO) {
def apply[A](fa: Fs[A]) = fa match {
case Ls(path) => IO(Seq(path))
case Cp(from, to) => IO(())
}
}
sealed trait PathOps[A]
case class SourcePath(template: String) extends PathOps[String]
def sourcePath(template: String) = Free.liftF(SourcePath(template))
这是你的
for
-理解转换为
PathOps ~>免费[Fs,?]
-口译员:
val pathToFsInterpreter = new (PathOps ~> FreeFs) {
def apply[A](p: PathOps[A]): FreeFs[A] = p match {
case SourcePath(template) => {
for {
paths <- ls(template)
} yield paths.head
}
}
}
现在您可以提起
Fs ~>IO
变成一个
Free[Fs, ?] ~> IO
使用
Free.foldMap
,并用
PathOps ~>免费[Fs,?]
-解释器使用
andThen
:
val pathToIo: PathOps ~> IO =
pathToFsInterpreter andThen
Free.foldMap(fsToIoInterpreter)
这将为您提供一个来自
PathOps ~>IO
由两个可单独测试的独立层组成。