- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
"collect" is not collecting
Mon, 2011-05-09, 05:30
i have the following:
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
object XmlTest {
import com.codecommit.antixml._
implicit def toPicker(nodes: Group[Node]): Picker = new Picker(nodes)
class Picker(nodes: Group[Node]) { def pick[A <: Node : ClassManifest]: Group[A] = nodes collect {
case a if implicitly[ClassManifest[A]].erasure.isInstance(a) => a.asInstanceOf[A]
}
}
def testCollect(elems: Group[Elem]) {
println("size before collect = " + elems.size)
val es = elems collect {
case e if e.name == "c" => println("element name is " + e.name); e
}
println("size after collect = " + es.size)
}
def main(args: Array[String]) {
val xml = XML.fromString("<a><b/><c/><d/></a>")
// this works because <a> has only elements as children
testCollect(xml.children.asInstanceOf[Group[Elem]])
// pick filters collection by type
testCollect(xml.children.pick[Elem])
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
when i ran it, it printed the following:
[info] size before collect = 3 // size is 3
[info] element name is c // element c matches
[info] size after collect = 1 // this is correct
[info] size before collect = 3 // size is the same as the previous case
[info] element name is c // element "c" is matched as well
[info] size after collect = 0 // this should be 1
i am at a lost here. what is going on?
regards,
walter
--
.......__o
.......\<,
....( )/ ( )...
--
.......__o
.......\<,
....( )/ ( )...
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
object XmlTest {
import com.codecommit.antixml._
implicit def toPicker(nodes: Group[Node]): Picker = new Picker(nodes)
class Picker(nodes: Group[Node]) { def pick[A <: Node : ClassManifest]: Group[A] = nodes collect {
case a if implicitly[ClassManifest[A]].erasure.isInstance(a) => a.asInstanceOf[A]
}
}
def testCollect(elems: Group[Elem]) {
println("size before collect = " + elems.size)
val es = elems collect {
case e if e.name == "c" => println("element name is " + e.name); e
}
println("size after collect = " + es.size)
}
def main(args: Array[String]) {
val xml = XML.fromString("<a><b/><c/><d/></a>")
// this works because <a> has only elements as children
testCollect(xml.children.asInstanceOf[Group[Elem]])
// pick filters collection by type
testCollect(xml.children.pick[Elem])
}
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
when i ran it, it printed the following:
[info] size before collect = 3 // size is 3
[info] element name is c // element c matches
[info] size after collect = 1 // this is correct
[info] size before collect = 3 // size is the same as the previous case
[info] element name is c // element "c" is matched as well
[info] size after collect = 0 // this should be 1
i am at a lost here. what is going on?
regards,
walter
--
.......__o
.......\<,
....( )/ ( )...
--
.......__o
.......\<,
....( )/ ( )...
Answer given here: http://stackoverflow.com/questions/5951831/collect-is-not-collecting/595...
tl;dr (on a link??): It's a bug in Anti-XML. Specifically,
Zipper#flatMap. Thanks for the test case! Should have a fix posted
soon…
Daniel
On May 8, 11:30 pm, Walter Chang wrote:
> i have the following:
>
> vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
>
> object XmlTest {
>
> import com.codecommit.antixml._
>
> implicit def toPicker(nodes: Group[Node]): Picker = new Picker(nodes)
>
> class Picker(nodes: Group[Node]) {
> def pick[A <: Node : ClassManifest]: Group[A] = nodes collect {
> case a if implicitly[ClassManifest[A]].erasure.isInstance(a) =>
> a.asInstanceOf[A]
> }
> }
>
> def testCollect(elems: Group[Elem]) {
> println("size before collect = " + elems.size)
> val es = elems collect {
> case e if e.name == "c" => println("element name is " + e.name); e
> }
> println("size after collect = " + es.size)
> }
>
> def main(args: Array[String]) {
> val xml = XML.fromString("")
>
> // this works because has only elements as children
> testCollect(xml.children.asInstanceOf[Group[Elem]])
>
> // pick filters collection by type
> testCollect(xml.children.pick[Elem])
> }
>
> }
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> when i ran it, it printed the following:
>
> [info] size before collect = 3 // size is 3
> [info] element name is c // element c matches
> [info] size after collect = 1 // this is correct
> [info] size before collect = 3 // size is the same as the previous
> case
> [info] element name is c // element "c" is matched as well
> [info] size after collect = 0 // this should be 1
>
> i am at a lost here. what is going on?
>
> regards,
> walter
>
> --
> .......__o
> .......\<,
> ....( )/ ( )...
>
> --
> .......__o
> .......\<,
> ....( )/ ( )...