Samstag, 27. Februar 2010

Using case classes to filter matching cases in Scala


abstract class A0

case class A() extends A0

case class B() extends A0

case class C() extends A0

val list = List( A(), A(), B(), C(), C(), C() )

val len1 = list filter ( _ match {case A() => true; case _ => false} ) length

val len2 = list filter ( _ match {case _:A => true; case _ => false} ) length

val len3 = list filter ( x => x match {case A() => true; case _ => false} ) length

printf("=> len1: %d, len2: %d, len3: %d \n",len1,len2,len3)

Keine Kommentare:

Kommentar veröffentlichen