Type based collection partitioning in Scala
问题 Given the following data model: sealed trait Fruit case class Apple(id: Int, sweetness: Int) extends Fruit case class Pear(id: Int, color: String) extends Fruit I've been looking to implement a segregate basket function which for the given basket of fruits will return separate baskets of apples and pears: def segregateBasket(fruitBasket: Set[Fruit]): (Set[Apple], Set[Pear]) I've attempted a couple of approaches, but none of them seems to be fitting the bill perfectly. Below are my attempts: