3

我有anti-xml Elem,像这样(自动生成的数据):

<library>
  <bookshelf number="0">
    <book year="1997" title="Puzzled Coordinators" author="Lily Williams"></book>
    <book year="2005" title="Jittery Fare" author="Lucy Taylor"></book>
    <book year="2001" title="Dizzy Jurisdiction" author="Lucy Robinson"></book>
  </bookshelf>
  <bookshelf number="1">
    <book year="1997" title="Bashful Trusts" author="Lucas Wilson"></book>
    <book year="2003" title="Outrageous Consequences" author="Byron White"></book>
    <book year="1992" title="Irritated Accusations" author="Anne Roberts"></book>
  </bookshelf>
</library>

我想对其应用几个转换,例如:

val transforms: Seq[...] = ...
val result = transforms.foldLeft(library)(l,t) => t(l))

但我只得到了这个解决方案:

val transforms: Seq[Elem => Zipper[Node]] = Seq(
  x => x \\ "book" filter (_.attrs("year").toInt > 2000) unselect,
  x => x \\ "book" filter (_.attrs("title").contains("J")) unselect
)
val result = transforms.foldLeft(lib)((l,t) => t(l).head.asInstanceOf[Elem])

有没有办法获得更好的转换类型(Elem => Elem)并避免那些丑陋的演员表?

4

1 回答 1

1

鉴于unselect目前只返回 a Zipper[Node],我看不到在不控制类型系统的情况下获得你想要的东西的方法,这将需要像演员一样的东西。

在这种情况下,鉴于 Anti-XML 库的当前状态,您确实知道类型系统无法做到的事情:您知道转换产生的 zipper 的父级是 a Zipper[Elem],因此unselect实际上给您的是a Zipper[Elem],即使它被键入为 a Zipper[Node]

所以我认为你能做的最好的就是把不愉快的东西包装得更干净一点:

def toEndo(t: Elem => Zipper[Elem]): Elem => Elem =
  t andThen (_.unselect.head.asInstanceOf[Elem])

或者,为了让您的意图更加明显:

def toEndo(t: Elem => Zipper[Elem]) = t andThen (_.unselect.head match {
  case e: Elem => e
  case _ => throw new RuntimeException("Aaaaah! This was never meant to happen!")
})

然后您可以编写以下内容:

val transforms: Seq[Elem => Zipper[Elem]] = Seq(
  _ \\ "book" filter (_.attrs("year").toInt > 2000),
  _ \\ "book" filter (_.attrs("title").contains("J"))
)

val result = Function.chain(transforms map toEndo)(lib)

请注意,我已将 移动unselect到帮助程序中,以便为我们提供更多的类型安全性。

于 2012-08-19T17:17:36.583 回答