我試圖讓新defprotocol
,reify
手柄等轉換一個org.w3c.dom.NodeList到Clojure的ISEQ
我有一個org.w3c.dom.NodeList
從一個XPath調用返回,我想將其轉換爲ISeq。
在Scala中,我實現的隱式轉換方法:
implicit def nodeList2Traversable(nodeList: NodeList): Traversable[Node] = {
new Traversable[Node] {
def foreach[A](process: (Node) => A) {
for (index <- 0 until nodeList.getLength) {
process(nodeList.item(index))
}
}
}
}
NodeList
包括這樣的方法和int getLength()
Node item(int index)
。
我該怎麼做Clojure中的等價物?我期望我將需要使用defprotocol
。我需要定義哪些功能來創建seq
?
如果我使用loop
和recur
做一個簡單的,天真的轉換爲列表,我將最終得到一個非惰性結構。
我們的崗位交叉,否則我不會打擾你了。 'for'在這裏看起來很不錯。 – Chouser 2011-05-05 14:09:56