Maa*_*mon 1 scala scala-cats cats-effect
我知道,一般来说,关于决定要建模什么效果,有很多话要说。这个讨论是在 Scala 函数式编程中关于 IO 的章节中介绍的。
\n尽管如此,我还没有读完这一章,我只是从头到尾地浏览了一遍,然后才和 Cats IO 一起阅读。
\n与此同时,我在工作中遇到了一些需要很快交付的代码的情况。\n它依赖于一个与突变有关的 Java 库。该库是很久以前开始的,由于遗留原因,我没有看到它们发生变化。
\n无论如何,长话短说。实际上,将任何变异函数建模为 IO 是封装变异 Java 库的可行方法吗?
\n准备好建立一个模型,改变模型而不是创建一个新模型。例如,我会将 jena 与 gremlin 进行对比,gremlin 是一个基于图形数据的功能库。
\ndef loadModel(paths: String*): Model =\n paths.foldLeft(ModelFactory.createOntologyModel(new OntModelSpec(OntModelSpec.OWL_MEM)).asInstanceOf[Model]) {\n case (model, path) \xe2\x87\x92\n val input = getClass.getClassLoader.getResourceAsStream(path)\n val lang = RDFLanguages.filenameToLang(path).getName\n model.read(input, "", lang)\n }\nRun Code Online (Sandbox Code Playgroud)\n那是我的 scala 代码,但是网站中记录的 java api 看起来像这样。
\n// create the resource\nResource r = model.createResource();\n\n// add the property\nr.addProperty(RDFS.label, model.createLiteral("chat", "en"))\n .addProperty(RDFS.label, model.createLiteral("chat", "fr"))\n .addProperty(RDFS.label, model.createLiteral("<em>chat</em>", true));\n\n// write out the Model\nmodel.write(system.out);\nRun Code Online (Sandbox Code Playgroud)\n// create a bag\nBag smiths = model.createBag();\n\n// select all the resources with a VCARD.FN property\n// whose value ends with "Smith"\nStmtIterator iter = model.listStatements(\n new SimpleSelector(null, VCARD.FN, (RDFNode) null) {\n public boolean selects(Statement s) {\n return s.getString().endsWith("Smith");\n }\n });\n// add the Smith's to the bag\nwhile (iter.hasNext()) {\n smiths.add(iter.nextStatement().getSubject());\n}\nRun Code Online (Sandbox Code Playgroud)\n
因此,这个问题有三种解决方案。
如果不纯 API 的所有使用都包含在代码库的单个/一小部分中,您可能只是“作弊”并执行以下操作:
def useBadJavaAPI(args): IO[Foo] = IO {
// Everything inside this block can be imperative and mutable.
}
Run Code Online (Sandbox Code Playgroud)
我说“作弊”是因为这个想法IO是组合,而一大块IO并不是真正的组合。但是,有时您只想封装遗留部分而不关心它。
基本上与上面相同,但flatMaps中间去掉了一些:
// Instead of:
def useBadJavaAPI(args): IO[Foo] = IO {
val a = createMutableThing()
mutableThing.add(args)
val b = a.bar()
b.computeFoo()
}
// You do something like this:
def useBadJavaAPI(args): IO[Foo] =
for {
a <- IO(createMutableThing())
_ <- IO(mutableThing.add(args))
b <- IO(a.bar())
result <- IO(b.computeFoo())
} yield result
Run Code Online (Sandbox Code Playgroud)
这样做有几个原因:
这与许多第三方库(例如Doobie、fs2-blobstore、neotypes)的做法基本相同。将Java库包装在纯接口上。
请注意,因此,必须完成的工作量比前两个解决方案要多得多。因此,如果可变 API “感染”了代码库的许多地方,或者在多个项目中情况更糟,那么这是值得的;如果是这样,那么这样做是有意义的,并且作为独立模块发布。
(将该模块作为开源库发布也可能是值得的,您最终可能会帮助其他人并获得其他人的帮助)
由于这是一项更大的任务,仅提供您必须做的所有事情的完整答案并不容易,因此了解这些库的实现方式并在此处或在 gitter 频道中提出更多问题可能会有所帮助。
但是,我可以给您简要介绍一下它的样子:
// First define a pure interface of the operations you want to provide
trait PureModel[F[_]] { // You may forget about the abstract F and just use IO instead.
def op1: F[Int]
def op2(data: List[String]): F[Unit]
}
// Then in the companion object you define factories.
object PureModel {
// If the underlying java object has a close or release action,
// use a Resource[F, PureModel[F]] instead.
def apply[F[_]](args)(implicit F: Sync[F]): F[PureModel[F]] = ???
}
Run Code Online (Sandbox Code Playgroud)
现在,如何创建实现是棘手的部分。也许您可以使用类似的方法Sync来初始化可变状态。
def apply[F[_]](args)(implicit F: Sync[F]): F[PureModel[F]] =
F.delay(createMutableState()).map { mutableThing =>
new PureModel[F] {
override def op1: F[Int] = F.delay(mutableThing.foo())
override def op2(data: List[String]): F[Unit] = F.delay(mutableThing.bar(data))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
665 次 |
| 最近记录: |