diff options
author | buraq <buraq@epfl.ch> | 2004-06-08 12:11:59 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2004-06-08 12:11:59 +0000 |
commit | 368d511247d5be7bd3ae8a1ce4a5be45fd3dcceb (patch) | |
tree | 0d38ddc92305cabe1943b8d6c9a66458aee2e599 | |
parent | b0b5b5fc12aab119453520185414ebf31dfdcc66 (diff) | |
download | scala-368d511247d5be7bd3ae8a1ce4a5be45fd3dcceb.tar.gz scala-368d511247d5be7bd3ae8a1ce4a5be45fd3dcceb.tar.bz2 scala-368d511247d5be7bd3ae8a1ce4a5be45fd3dcceb.zip |
NodeSeq comprehension methods
-rw-r--r-- | sources/scala/xml/NodeSeq.scala | 15 | ||||
-rw-r--r-- | test/files/jvm/xmlstuff.check | 2 | ||||
-rw-r--r-- | test/files/jvm/xmlstuff.scala | 14 |
3 files changed, 31 insertions, 0 deletions
diff --git a/sources/scala/xml/NodeSeq.scala b/sources/scala/xml/NodeSeq.scala index 9909167aeb..73d15843ea 100644 --- a/sources/scala/xml/NodeSeq.scala +++ b/sources/scala/xml/NodeSeq.scala @@ -42,4 +42,19 @@ class NodeSeq( theSeq:Seq[Node] ) extends Seq[Node] { override def toString() = theSeq.elements.foldLeft ("") { (s:String,x:Node) => s + x.toString() } + + private var _asList:List[Node] = null; + def asList = { + if (_asList == null ) _asList = elements.toList; + _asList + } + + def map( f:Node => Node ):NodeSeq = { + new NodeSeq( asList map f ) + } + + def flatMap( f:Node => NodeSeq ):NodeSeq = { + new NodeSeq( asList flatMap { x => f(x).asList }) + } + } diff --git a/test/files/jvm/xmlstuff.check b/test/files/jvm/xmlstuff.check index 57a788c6a1..afe3a6839f 100644 --- a/test/files/jvm/xmlstuff.check +++ b/test/files/jvm/xmlstuff.check @@ -18,3 +18,5 @@ xpath \\ DESCENDANTS passed ok passed ok passed ok +NodeSeq +passed ok diff --git a/test/files/jvm/xmlstuff.scala b/test/files/jvm/xmlstuff.scala index c7c0e5da93..ff6c2e03ed 100644 --- a/test/files/jvm/xmlstuff.scala +++ b/test/files/jvm/xmlstuff.scala @@ -180,4 +180,18 @@ object Test with Application { Elem("title",e,Text("Data on ze web")), Elem("title",e,Text("Foundations of Programming Languages"))) ); + + + Console.println("NodeSeq"); + import scala.xml.Utility.view ; + + val p = <foo><bar value="3"/><baz bazValue="8"/><bar value="5"/></foo>; + + assertSameElements( + for( val x <- p \ "bar"; val y <- p \ "baz" ) yield { + x.attribute("value") + y.attribute("bazValue")+ "!" + }, + new NodeSeq(List(Text("38!"),Text("58!"))) + ) + } |