How to get the proper return type when using a filter based on type in Scala
Tag : scala , By : Ravenal
Date : March 29 2020, 07:55 AM
hope this fix your issue You're right thinking that somehow, there should be a mechanism that lets you avoid casting. Such a cast would be ugly and redundant, as it already appears in the filter anyway. find, however, does not care at all about the shape of the predicate it gets; it just applies it and returns an Option[A] if A is the static type of the collection's elements. What you need is the collect function: val boss = People.all.collect { case boss: Authority => boss }.head
val boss = People.all.view.collect { case boss: Authority => boss }.head
val bossOpt = People.all.view.collect { case boss: Authority => boss }.headOption
bossOpt.foreach(_.giveOrder) // happens only if a boss was found
|
Type selection, type projection. What is S#T in Scala ? Scala Language Specification based explanation is needed
Tag : scala , By : Tonci Grgin
Date : March 29 2020, 07:55 AM
may help you . I don't have a legalistic understanding of A#B (only a practical one) so I can't help with the first part, but I can answer your final question: per 3.2.3, the type o1.Inner is o1.type#Inner. Per 3.5.2, "A type projection T#t conforms to U#t if T conforms to U". Hopefully it's obvious that o1.type <: Outer; strictly this is because (again in 3.5.2) "A singleton type p.type conforms to the type of the path p".
|
scala filter by type
Tag : scala , By : user176691
Date : March 29 2020, 07:55 AM
To fix the issue you can do You need to provide a ClassTag, not a TypeTag, and use pattern matching. ClassTags work well with pattern matching. You can even use the collect method to perform the filter and map together: def filter[T, T2](data: Traversable[T2])(implicit ev: ClassTag[T]) = data collect {
case t: T => t
}
val data = Seq(new B, new B, new C, new B)
filter[B, A](data) //Traversable[B] with length 3
filter[C, A](data) //Traversable[C] with length 1
def filter[T : ClassTag](data: Traversable[_]) = data collect { case t: T => t }
filter[B](data)
val data = Vector(new B, new B, new C, new B)
data filter { _.isInstanceOf[B] } //Vector[A]
data filter { _.isInstanceOf[B] } map { _.asInstanceOf[B] } //Vector[B]
data collect { case t: B => t } //Vector[B]. Note that if you know the type at the calling point, this is pretty concise and might not need a helper method at all
//As opposed to:
filter[B](data) //Traversable[B], not a Vector!
implicit class RichTraversable[T2, Repr <: TraversableLike[T2, Repr], That](val trav: TraversableLike[T2, Repr]) extends AnyVal {
def withType[T : ClassTag](implicit bf: CanBuildFrom[Repr, T, That]) = trav.collect {
case t: T => t
}
}
data.withType[B] //Vector[B], as desired
|
Scala filter a list based on a max value
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have the following MAP: , Assuming a case class like case class Pet(animal: String, age: Int): list.groupBy(_.animal).values.map(_.maxBy(_.age)).toList
list.groupBy(_.getAs[String]("animal")).values.map(_.maxBy(_.getAs[Int]("age"))).toList
|
Scala implicit macros: Filter type members (tpe.decls) by sub-type
Date : March 29 2020, 07:55 AM
I wish this help you Let's say I have a simple impicit macro that gives me back a weakTypeSymbol: , This is doable with an API similar to reflection: class TestMacro(val c: blackbox.Context) {
import c.universe._
def filterMembers[
T : WeakTypeTag,
Filter : TypeTag
]: List[Symbol] = {
val tpe = weakTypeOf[T].typeSymbol.typeSignature
(for {
baseClass <- tpe.baseClasses.reverse
symbol <- baseClass.typeSignature.members.sorted
if symbol.typeSignature <:< typeOf[Filter]
} yield symbol)(collection.breakOut)
}
}
|