- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
collect/filter on type
Wed, 2011-06-22, 17:33
hello
has this been implemented yet in some way:
scala> class Foo
defined class Foo
scala> class Bar extends Foo
defined class Bar
scala> case object foo extends Foo
defined module foo
scala> case object bar extends Bar
defined module bar
scala> val l = List[Foo](foo,bar,foo,bar,foo)
l: List[Foo] = List(foo, bar, foo, bar, foo)
scala> l.collect[Bar]
... to get only certain subtypes of a collection?
best regards
wookietreiber
Wed, 2011-06-22, 18:17
#2
Re: collect/filter on type
Do you mean this?
scala> l.collect {case b:Bar => b}
res4: List[Bar] = List(bar, bar)
Wed, 2011-06-22, 19:57
#3
Re: Re: collect/filter on type
thx for the replies, I toyed with it a little and had similar solutions, like:
On Wed, Jun 22, 2011 at 10:04:15AM -0700, Dave wrote:
> Do you mean this?
>
> scala> l.collect {case b:Bar => b}
> res4: List[Bar] = List(bar, bar)
it just would be nice to have the even more concise syntax:
l.collect[Bar]
directly in the collection library for something I guess is needed often, this
would be even shorter and more expressive :) even for DSLs if written like:
list filter [Bar] or
list collect [Bar]
best regards
wookietreiber
Here's mine, but it doesn't work with boxed primitives--I haven't bothered to figure out how to do that yet. :)
-0xe1a