Converting a org.w3c.dom.NodeList to a Clojure ISeq

I am trying to get a handle on the new defprotocol, reify, etc.

I have a org.w3c.dom.NodeList returned from an XPath call and I would like to "convert" it to an ISeq.

In Scala, I implemented an implicit conversion method:

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 includes methods int getLength() and Node item(int index).

How do I do the equivalent in Clojure? I expect that I will need to use defprotocol. What functions do I need to define to create a seq?

If I do a simple, naive, conversion to a list using loop and recur, I will end up with a non-lazy structure.

6
задан Ralph 5 May 2011 в 13:25
поделиться