如何将 Array[Node] 转换为 NodeSeq?
我正在尝试将 Lift 应用程序集成到一些现有的 Java 代码中。在我的一个片段中,我有一个 Java 对象数组,我需要将其映射到 NodeSeq 中。我可以获得 Node 数组,但不能获得 NodeSeq。 (至少,不是以非常实用的方式)。
import scala.xml.NodeSeq
// pretend this is code I can't do anything about
val data = Array("one", "two", "three")
// this is the function I need to write
def foo: NodeSeq = data.map { s => <x>{s}</x> }
// ^
// error: type mismatch;
// found : Array[scala.xml.Elem]
// required: scala.xml.NodeSeq
最干净的方法是什么?
I'm trying to integrate a Lift application into some existing Java code. In one of my snippets, I have an Array of Java objects that I need to map that into a NodeSeq. I can get an Array of Node's, but not a NodeSeq. (At least, not in very functional-looking way).
import scala.xml.NodeSeq
// pretend this is code I can't do anything about
val data = Array("one", "two", "three")
// this is the function I need to write
def foo: NodeSeq = data.map { s => <x>{s}</x> }
// ^
// error: type mismatch;
// found : Array[scala.xml.Elem]
// required: scala.xml.NodeSeq
What's the cleanest way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方法映射实际上有两个参数列表。第一个接受您传递的函数。第二个接受 CanBuildFrom 对象,该对象用于创建一个构建器,然后构建返回序列。该参数是隐式的,因此通常编译器会为您填充它。它接受 3 个类型参数:From、T、To。有几个预定义隐式(包括在对象 NodeSeq 中),但它们都不匹配 From=Array、T=Node、To=NodeSeq。
breakOut 解决了这个问题:它是一个通用方法,通过搜索隐式 CanBuildFrom[Nothing, T, To] 返回 CanBuildFrom 实例。根据隐式搜索规则,任何与 T、To 匹配且 From > 的 CanBuildFrom没有什么是可以接受的。在本例中:对象数组中的 canBuildFrom
The method map actually has two argument lists. The first accepts a function, which you passed. The second accepts a CanBuildFrom object which is used to create a builder that then builds the returning sequence. This argument is implicit, so usually the compiler fills it for you. It accepts 3 type parameters: From, T, To. There are several predef implicits (including in object NodeSeq), but none of them matches From=Array, T=Node, To=NodeSeq.
breakOut solves this: it is a generic method that returns a CanBuildFrom instance by searching for an implicit CanBuildFrom[Nothing, T, To]. According to the implicit search rules, any CanBuildFrom that matches T, To and has From > Nothing is acceptable. In this case: canBuildFrom in object Array
我只需将
map
输出转换为序列(假设Seq[Node]
是NodeSeq
的超类)或使用
foldLeft
而不是地图
I would simply convert
map
output to sequence (given thatSeq[Node]
is a super-class ofNodeSeq
)or use
foldLeft
instead ofmap
您正在 NodeSeq 伴随对象上寻找此方法。
You are looking for this method on the NodeSeq companion object.